From 5790a4b57f050e4a3f295a34dbddb09138ef1da6 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 11 Apr 2017 08:05:20 -0700 Subject: [PATCH] packager: Module.js: fix Haste name code Summary: The original code wasn't very readable. Better with this change, I think. Reviewed By: davidaurelio Differential Revision: D4851335 fbshipit-source-id: 210309d4b727aff58bea48d0ab324256234cd941 --- .../metro-bundler/src/node-haste/Module.js | 74 ++++++++++--------- .../src/node-haste/__tests__/Module-test.js | 1 + 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/packages/metro-bundler/src/node-haste/Module.js b/packages/metro-bundler/src/node-haste/Module.js index 474d2c36..c3b0a230 100644 --- a/packages/metro-bundler/src/node-haste/Module.js +++ b/packages/metro-bundler/src/node-haste/Module.js @@ -210,46 +210,48 @@ class Module { } _getHasteName(): ?string { - if (this._hasteNameCache != null) { - return this._hasteNameCache.hasteName; - } - const hasteImpl = this._options.hasteImpl; - if (hasteImpl === undefined || hasteImpl.enforceHasteNameMatches) { - const moduleDocBlock = this._readDocBlock(); - const {providesModule} = moduleDocBlock; - this._hasteNameCache = { - hasteName: providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path) - ? /^\S+/.exec(providesModule)[0] - : undefined, - }; - } - if (hasteImpl !== undefined) { - const {enforceHasteNameMatches} = hasteImpl; - if (enforceHasteNameMatches) { - /* $FlowFixMe: this rely on the above if being executed, that is fragile. - * Rework the algo. */ - enforceHasteNameMatches(this.path, this._hasteNameCache.hasteName); - } - this._hasteNameCache = {hasteName: hasteImpl.getHasteName(this.path)}; - } else { - // Extract an id for the module if it's using @providesModule syntax - // and if it's NOT in node_modules (and not a whitelisted node_module). - // This handles the case where a project may have a dep that has @providesModule - // docblock comments, but doesn't want it to conflict with whitelisted @providesModule - // modules, such as react-haste, fbjs-haste, or react-native or with non-dependency, - // project-specific code that is using @providesModule. - const moduleDocBlock = this._readDocBlock(); - const {providesModule} = moduleDocBlock; - this._hasteNameCache = { - hasteName: - providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path) - ? /^\S+/.exec(providesModule)[0] - : undefined, - }; + if (this._hasteNameCache == null) { + this._hasteNameCache = {hasteName: this._readHasteName()}; } return this._hasteNameCache.hasteName; } + /** + * If a custom Haste implementation is provided, then we use it to determine + * the actual Haste name instead of "@providesModule". + * `enforceHasteNameMatches` has been added to that it is easier to + * transition from a system using "@providesModule" to a system using another + * custom system, by throwing if inconsistencies are detected. For example, + * we could verify that the file's basename (ex. "bar/foo.js") is the same as + * the "@providesModule" name (ex. "foo"). + */ + _readHasteName(): ?string { + const hasteImpl = this._options.hasteImpl; + if (hasteImpl == null) { + return this._readHasteNameFromDocBlock(); + } + const {enforceHasteNameMatches} = hasteImpl; + if (enforceHasteNameMatches != null) { + const name = this._readHasteNameFromDocBlock(); + enforceHasteNameMatches(this.path, name || undefined); + } + return hasteImpl.getHasteName(this.path); + } + + /** + * We extract the Haste name from the `@providesModule` docbloc field. This is + * not allowed for modules living in `node_modules`, except if they are + * whitelisted. + */ + _readHasteNameFromDocBlock(): ?string { + const moduleDocBlock = this._readDocBlock(); + const {providesModule} = moduleDocBlock; + if (providesModule && !this._depGraphHelpers.isNodeModulesDir(this.path)) { + return /^\S+/.exec(providesModule)[0]; + } + return null; + } + /** * To what we read from the cache or worker, we need to add id and source. */ diff --git a/packages/metro-bundler/src/node-haste/__tests__/Module-test.js b/packages/metro-bundler/src/node-haste/__tests__/Module-test.js index e8d38b3d..5e5e6e88 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/Module-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/Module-test.js @@ -315,6 +315,7 @@ describe('Module', () => { arbitrary: 'arbitrary', dependencies: ['foo', 'bar'], dependencyOffsets: [12, 764], + id: null, map: {version: 3}, subObject: {foo: 'bar'}, };