diff --git a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js index ebde3577..01d52cdb 100644 --- a/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js @@ -24,6 +24,8 @@ const { traverseDependencies, } = require('../traverseDependencies'); +const getTransformOptions = require('../../__fixtures__/getTransformOptions'); + describe('DeltaCalculator', () => { const entryModule = createModule({path: '/bundle', name: 'bundle'}); const moduleFoo = createModule({path: '/foo', name: 'foo'}); @@ -308,6 +310,14 @@ describe('DeltaCalculator', () => { }); }); + it('should return the same params as the standard options', async () => { + const options = await deltaCalculator.getTransformerOptions(); + + expect(Object.keys(options).sort()).toEqual( + Object.keys(await getTransformOptions()).sort(), + ); + }); + it('should handle inlineRequires=true correctly', async () => { Bundler.prototype.getTransformOptionsForEntryFile.mockReturnValue( Promise.resolve({ diff --git a/packages/metro/src/__fixtures__/getTransformOptions.js b/packages/metro/src/__fixtures__/getTransformOptions.js new file mode 100644 index 00000000..e729b5fe --- /dev/null +++ b/packages/metro/src/__fixtures__/getTransformOptions.js @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails oncall+javascript_foundation + * @format + */ + +'use strict'; + +const DeltaCalculator = require('../DeltaBundler/DeltaCalculator'); + +import type {Options as JSTransformerOptions} from '../JSTransformer/worker'; + +async function getTransformOptions(): Promise { + const bundler = { + getGlobalTransformOptions() { + return { + enableBabelRCLookup: true, + projectRoot: '/root', + }; + }, + async getTransformOptionsForEntryFile() { + return { + inlineRequires: true, + }; + }, + }; + const dependencyGraph = { + getWatcher() { + return {on() {}}; + }, + }; + const options = { + assetPlugins: [], + dev: true, + hot: true, + minify: false, + platform: 'ios', + }; + + const deltaCalculator = new DeltaCalculator( + bundler, + dependencyGraph, + options, + ); + + return await deltaCalculator.getTransformerOptions(); +} + +module.exports = getTransformOptions; diff --git a/packages/metro/src/lib/GlobalTransformCache.js b/packages/metro/src/lib/GlobalTransformCache.js index a83aa082..eb807095 100644 --- a/packages/metro/src/lib/GlobalTransformCache.js +++ b/packages/metro/src/lib/GlobalTransformCache.js @@ -439,6 +439,7 @@ class OptionsHasher { options: TransformOptionsStrict, ): crypto$Hash { const { + assetDataPlugins, enableBabelRCLookup, dev, hot, @@ -466,8 +467,9 @@ class OptionsHasher { (+minify << 5), ]), ); - /* eslint-enable no-bitwise */ + /* eslint-enable no-bitwise */ + hash.update(JSON.stringify(assetDataPlugins)); hash.update(JSON.stringify(platform)); hash.update(JSON.stringify(this.toLocalPath(projectRoot))); diff --git a/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js index 3ae608ae..98b3e694 100644 --- a/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js +++ b/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js @@ -19,7 +19,8 @@ jest.mock('node-fetch', () => mockFetch); const {URIBasedGlobalTransformCache} = require('../GlobalTransformCache'); const FetchError = require('node-fetch/lib/fetch-error'); -const path = require('path'); + +const getTransformOptions = require('../../__fixtures__/getTransformOptions'); async function fetchResultURIs( keys: Array, @@ -44,14 +45,8 @@ describe('GlobalTransformCache', () => { rootPath: __dirname, storeResults: null, }); - const transformOptions = { - dev: false, - hot: false, - inlineRequires: false, - minify: false, - platform: 'ios', - projectRoot: path.join(__dirname, 'root'), - }; + const transformOptions = await getTransformOptions(); + const result = await Promise.all([ cache.fetch({ localPath: 'some/where/foo.js', diff --git a/packages/metro/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap b/packages/metro/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap index d05e6956..2eb6754b 100644 --- a/packages/metro/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap +++ b/packages/metro/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/c78d72e5b503433212dce49b558b0ee350b6d578-foo.js */", + "code": "/* code from http://globalcache.com/e4d11b53d671aa56b44f2fdda2cd77b31f3a5cad-foo.js */", "dependencies": Array [], "dependencyOffsets": Array [], }, Object { - "code": "/* code from http://globalcache.com/47637fbb61b7dfeda3a0d0a36241510b4669e4da-bar.js */", + "code": "/* code from http://globalcache.com/bf01163f2c3c2575e7da85343e20e9ff0013bbe8-bar.js */", "dependencies": Array [], "dependencyOffsets": Array [], },