diff --git a/packages/metro-bundler/src/DeltaBundler/DeltaCalculator.js b/packages/metro-bundler/src/DeltaBundler/DeltaCalculator.js index deed6d66..2fbf2e66 100644 --- a/packages/metro-bundler/src/DeltaBundler/DeltaCalculator.js +++ b/packages/metro-bundler/src/DeltaBundler/DeltaCalculator.js @@ -159,18 +159,14 @@ class DeltaCalculator extends EventEmitter { } = this._bundler.getGlobalTransformOptions(); const transformOptionsForBlacklist = { + enableBabelRCLookup, dev: this._options.dev, + generateSourceMaps: this._options.generateSourceMaps, + hot: this._options.hot, + inlineRequires: false, minify: this._options.minify, platform: this._options.platform, - transform: { - enableBabelRCLookup, - dev: this._options.dev, - generateSourceMaps: this._options.generateSourceMaps, - hot: this._options.hot, - inlineRequires: false, - platform: this._options.platform, - projectRoot, - }, + projectRoot, }; const { @@ -193,10 +189,7 @@ class DeltaCalculator extends EventEmitter { // $FlowFixMe flow does not recognize well Object.assign() return types. return { ...transformOptionsForBlacklist, - transform: { - ...transformOptionsForBlacklist.transform, - inlineRequires: inlineRequires || false, - }, + inlineRequires: inlineRequires || false, }; } diff --git a/packages/metro-bundler/src/DeltaBundler/__tests__/DeltaCalculator-test.js b/packages/metro-bundler/src/DeltaBundler/__tests__/DeltaCalculator-test.js index 1cbe089e..01e5202c 100644 --- a/packages/metro-bundler/src/DeltaBundler/__tests__/DeltaCalculator-test.js +++ b/packages/metro-bundler/src/DeltaBundler/__tests__/DeltaCalculator-test.js @@ -303,17 +303,13 @@ describe('DeltaCalculator', () => { it('should calculate the transform options correctly', async () => { expect(await deltaCalculator.getTransformerOptions()).toEqual({ dev: true, + enableBabelRCLookup: false, + generateSourceMaps: false, + hot: true, + inlineRequires: false, minify: false, platform: 'ios', - transform: { - dev: true, - enableBabelRCLookup: false, - generateSourceMaps: false, - hot: true, - inlineRequires: false, - platform: 'ios', - projectRoot: '/foo', - }, + projectRoot: '/foo', }); }); @@ -326,17 +322,13 @@ describe('DeltaCalculator', () => { expect(await deltaCalculator.getTransformerOptions()).toEqual({ dev: true, + enableBabelRCLookup: false, + generateSourceMaps: false, + hot: true, + inlineRequires: true, minify: false, platform: 'ios', - transform: { - dev: true, - enableBabelRCLookup: false, - generateSourceMaps: false, - hot: true, - inlineRequires: true, - platform: 'ios', - projectRoot: '/foo', - }, + projectRoot: '/foo', }); }); @@ -349,17 +341,13 @@ describe('DeltaCalculator', () => { expect(await deltaCalculator.getTransformerOptions()).toEqual({ dev: true, + enableBabelRCLookup: false, + generateSourceMaps: false, + hot: true, + inlineRequires: {blacklist: {'/bar': true, '/baz': true}}, minify: false, platform: 'ios', - transform: { - dev: true, - enableBabelRCLookup: false, - generateSourceMaps: false, - hot: true, - inlineRequires: {blacklist: {'/bar': true, '/baz': true}}, - platform: 'ios', - projectRoot: '/foo', - }, + projectRoot: '/foo', }); }); }); diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index f4fe15b9..122a48a2 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -61,6 +61,7 @@ export type TransformOptionsStrict = {| +generateSourceMaps: boolean, +hot: boolean, +inlineRequires: {+blacklist: {[string]: true}} | boolean, + +minify: boolean, +platform: ?string, +projectRoot: string, |}; @@ -71,16 +72,12 @@ export type TransformOptions = { +generateSourceMaps?: boolean, +hot?: boolean, +inlineRequires?: {+blacklist: {[string]: true}} | boolean, + +minify: boolean, +platform: ?string, +projectRoot: string, }; -export type Options = {| - +dev: boolean, - +minify: boolean, - +platform: ?string, - +transform: TransformOptionsStrict, -|}; +export type Options = TransformOptionsStrict; export type Data = { result: TransformedCode, @@ -117,7 +114,7 @@ async function transformCode( const {ast} = await transformer.transform({ filename, localPath, - options: options.transform, + options, plugins, src: sourceCode, }); diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js index 16b9fcc6..0ece169f 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -90,6 +90,7 @@ describe('transforming JS modules:', () => { generateSourceMaps: true, hot: false, inlineRequires: false, + minify: false, platform: '', projectRoot: '', }; diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index 23c7cc22..8923d00b 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -52,6 +52,7 @@ const defaultTransformOptions = { generateSourceMaps: true, hot: false, inlineRequires: false, + minify: false, platform: '', projectRoot: '', }; diff --git a/packages/metro-bundler/src/lib/GlobalTransformCache.js b/packages/metro-bundler/src/lib/GlobalTransformCache.js index 63d76de3..e1d607ae 100644 --- a/packages/metro-bundler/src/lib/GlobalTransformCache.js +++ b/packages/metro-bundler/src/lib/GlobalTransformCache.js @@ -147,8 +147,8 @@ export type TransformProfile = { +platform: ?string, }; -function profileKey({dev, minify, platform}: TransformProfile): string { - return jsonStableStringify({dev, minify, platform}); +function profileKey({dev, platform}: TransformProfile): string { + return jsonStableStringify({dev, platform}); } /** @@ -403,19 +403,8 @@ class OptionsHasher { */ hashTransformWorkerOptions( hash: crypto$Hash, - options: TransformWorkerOptions, + transform: TransformWorkerOptions, ): crypto$Hash { - const {dev, minify, platform, transform, ...unknowns} = options; - const unknownKeys = Object.keys(unknowns); - if (unknownKeys.length > 0) { - const message = `these worker option fields are unknown: ${JSON.stringify( - unknownKeys, - )}`; - throw new CannotHashOptionsError(message); - } - // eslint-disable-next-line no-bitwise - hash.update(new Buffer([+dev | (+minify << 1)])); - hash.update(JSON.stringify(platform)); return this.hashTransformOptions(hash, transform); } @@ -437,6 +426,7 @@ class OptionsHasher { dev, hot, inlineRequires, + minify, platform, projectRoot, ...unknowns @@ -449,20 +439,19 @@ class OptionsHasher { throw new CannotHashOptionsError(message); } + /* eslint-disable no-bitwise */ hash.update( new Buffer([ - // eslint-disable-next-line no-bitwise +dev | - // eslint-disable-next-line no-bitwise (+generateSourceMaps << 1) | - // eslint-disable-next-line no-bitwise (+hot << 2) | - // eslint-disable-next-line no-bitwise (+!!inlineRequires << 3) | - // eslint-disable-next-line no-bitwise - (+enableBabelRCLookup << 4), + (+enableBabelRCLookup << 4) | + (+minify << 5), ]), ); + /* eslint-enable no-bitwise */ + hash.update(JSON.stringify(platform)); let blacklistWithLocalPaths = []; if (typeof inlineRequires === 'object') { diff --git a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js index 819ec365..2a5d962d 100644 --- a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js +++ b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js @@ -45,17 +45,13 @@ describe('GlobalTransformCache', () => { storeResults: null, }); const transformOptions = { - dev: true, + generateSourceMaps: false, + dev: false, + hot: false, + inlineRequires: false, minify: false, platform: 'ios', - transform: { - generateSourceMaps: false, - dev: false, - hot: false, - inlineRequires: false, - platform: 'ios', - projectRoot: path.join(__dirname, 'root'), - }, + projectRoot: path.join(__dirname, 'root'), }; const result = await Promise.all([ cache.fetch({ diff --git a/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap index 88ce1547..bae2e335 100644 --- a/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap +++ b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap @@ -19,12 +19,12 @@ Object { exports[`GlobalTransformCache fetches results 1`] = ` Array [ Object { - "code": "/* code from http://globalcache.com/b23da8c74218e6155fcaf590a0fedbd1d117c2ae-foo.js */", + "code": "/* code from http://globalcache.com/010a1a9e948edce805a4fa7685a1c64ce97dc035-foo.js */", "dependencies": Array [], "dependencyOffsets": Array [], }, Object { - "code": "/* code from http://globalcache.com/5e95f6c3e9bac0282480cda6f1a984ad8bc83e55-bar.js */", + "code": "/* code from http://globalcache.com/640a1404db77ab17a10fbae1a3b4db92634669c1-bar.js */", "dependencies": Array [], "dependencyOffsets": Array [], }, diff --git a/packages/metro-bundler/src/transformer.js b/packages/metro-bundler/src/transformer.js index 128e61ef..2f7793cb 100644 --- a/packages/metro-bundler/src/transformer.js +++ b/packages/metro-bundler/src/transformer.js @@ -124,7 +124,12 @@ type Params = { }; function transform({filename, options, src, plugins}: Params) { - options = options || {platform: '', projectRoot: '', inlineRequires: false}; + options = options || { + platform: '', + projectRoot: '', + inlineRequires: false, + minify: false, + }; const OLD_BABEL_ENV = process.env.BABEL_ENV; process.env.BABEL_ENV = options.dev ? 'development' : 'production';