diff --git a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js index a5effb21..efcdfd62 100644 --- a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js +++ b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js @@ -16,6 +16,7 @@ const DeltaCalculator = require('./DeltaCalculator'); const addParamsToDefineCall = require('../lib/addParamsToDefineCall'); const createModuleIdFactory = require('../lib/createModuleIdFactory'); +const removeInlineRequiresBlacklistFromOptions = require('../lib/removeInlineRequiresBlacklistFromOptions'); const {EventEmitter} = require('events'); @@ -559,7 +560,9 @@ class DeltaTransformer extends EventEmitter { +map: CompactRawMappings, +source: string, }> { - return await module.read(transformOptions); + return await module.read( + removeInlineRequiresBlacklistFromOptions(module.path, transformOptions), + ); } _onFileChange = () => { diff --git a/packages/metro-bundler/src/DeltaBundler/traverseDependencies.js b/packages/metro-bundler/src/DeltaBundler/traverseDependencies.js index 49fc7e03..6659f30a 100644 --- a/packages/metro-bundler/src/DeltaBundler/traverseDependencies.js +++ b/packages/metro-bundler/src/DeltaBundler/traverseDependencies.js @@ -12,6 +12,8 @@ 'use strict'; +const removeInlineRequiresBlacklistFromOptions = require('../lib/removeInlineRequiresBlacklistFromOptions'); + import type {Options as JSTransformerOptions} from '../JSTransformer/worker'; import type DependencyGraph from '../node-haste/DependencyGraph'; @@ -217,7 +219,7 @@ async function addDependency( const shallowDeps = await dependencyGraph.getShallowDependencies( dependencyEdge.path, - transformOptions, + removeInlineRequiresBlacklistFromOptions(module.path, transformOptions), ); const nonNullDependencyEdge = dependencyEdge; diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index 122a48a2..c8030b6e 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -60,7 +60,7 @@ export type TransformOptionsStrict = {| +dev: boolean, +generateSourceMaps: boolean, +hot: boolean, - +inlineRequires: {+blacklist: {[string]: true}} | boolean, + +inlineRequires: boolean, +minify: boolean, +platform: ?string, +projectRoot: string, @@ -71,7 +71,7 @@ export type TransformOptions = { +dev?: boolean, +generateSourceMaps?: boolean, +hot?: boolean, - +inlineRequires?: {+blacklist: {[string]: true}} | boolean, + +inlineRequires: boolean, +minify: boolean, +platform: ?string, +projectRoot: string, diff --git a/packages/metro-bundler/src/lib/GlobalTransformCache.js b/packages/metro-bundler/src/lib/GlobalTransformCache.js index e1d607ae..7b03826d 100644 --- a/packages/metro-bundler/src/lib/GlobalTransformCache.js +++ b/packages/metro-bundler/src/lib/GlobalTransformCache.js @@ -445,7 +445,7 @@ class OptionsHasher { +dev | (+generateSourceMaps << 1) | (+hot << 2) | - (+!!inlineRequires << 3) | + (+inlineRequires << 3) | (+enableBabelRCLookup << 4) | (+minify << 5), ]), @@ -453,15 +453,8 @@ class OptionsHasher { /* eslint-enable no-bitwise */ hash.update(JSON.stringify(platform)); - let blacklistWithLocalPaths = []; - if (typeof inlineRequires === 'object') { - blacklistWithLocalPaths = this.pathsToLocal( - Object.keys(inlineRequires.blacklist), - ); - } - const localProjectRoot = this.toLocalPath(projectRoot); - const optionTuple = [blacklistWithLocalPaths, localProjectRoot]; - hash.update(JSON.stringify(optionTuple)); + hash.update(JSON.stringify(this.toLocalPath(projectRoot))); + return hash; } 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 bae2e335..d05e6956 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/010a1a9e948edce805a4fa7685a1c64ce97dc035-foo.js */", + "code": "/* code from http://globalcache.com/c78d72e5b503433212dce49b558b0ee350b6d578-foo.js */", "dependencies": Array [], "dependencyOffsets": Array [], }, Object { - "code": "/* code from http://globalcache.com/640a1404db77ab17a10fbae1a3b4db92634669c1-bar.js */", + "code": "/* code from http://globalcache.com/47637fbb61b7dfeda3a0d0a36241510b4669e4da-bar.js */", "dependencies": Array [], "dependencyOffsets": Array [], }, diff --git a/packages/metro-bundler/src/lib/__tests__/removeInlineRequiresBlacklistFromOptions-test.js b/packages/metro-bundler/src/lib/__tests__/removeInlineRequiresBlacklistFromOptions-test.js new file mode 100644 index 00000000..09576f09 --- /dev/null +++ b/packages/metro-bundler/src/lib/__tests__/removeInlineRequiresBlacklistFromOptions-test.js @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2016-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. + * + * @format + * @emails oncall+js_foundation + */ + +'use strict'; + +const removeInlineRequiresBlacklistFromOptions = require('../removeInlineRequiresBlacklistFromOptions'); + +it('should not touch a transformOption object with boolean inlineRequires', () => { + const transformOptions = { + inlineRequires: false, + }; + + expect( + removeInlineRequiresBlacklistFromOptions('/path', transformOptions), + ).toBe(transformOptions); +}); + +it('should change inlineRequires to true when the path is not in the blacklist', () => { + const transformOptions = { + inlineRequires: { + blacklist: {'/other': true}, + }, + foo: 'bar', + }; + + expect( + removeInlineRequiresBlacklistFromOptions('/path', transformOptions), + ).toEqual({ + inlineRequires: true, + foo: 'bar', + }); +}); + +it('should change inlineRequires to false when the path is in the blacklist', () => { + const transformOptions = { + inlineRequires: { + blacklist: {'/path': true}, + }, + foo: 'bar', + }; + + expect( + removeInlineRequiresBlacklistFromOptions('/path', transformOptions), + ).toEqual({ + inlineRequires: false, + foo: 'bar', + }); +}); diff --git a/packages/metro-bundler/src/lib/removeInlineRequiresBlacklistFromOptions.js b/packages/metro-bundler/src/lib/removeInlineRequiresBlacklistFromOptions.js new file mode 100644 index 00000000..30d6cfbb --- /dev/null +++ b/packages/metro-bundler/src/lib/removeInlineRequiresBlacklistFromOptions.js @@ -0,0 +1,32 @@ +/** + * 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. + * + * @flow + * @format + */ + +'use strict'; + +import type {Options as JSTransformerOptions} from '../JSTransformer/worker'; + +function removeInlineRequiresBlacklistFromOptions( + path: string, + transformOptions: JSTransformerOptions, +): JSTransformerOptions { + if (typeof transformOptions.inlineRequires === 'object') { + // $FlowIssue #23854098 - Object.assign() loses the strictness of an object in flow + return { + ...transformOptions, + inlineRequires: !(path in transformOptions.inlineRequires.blacklist), + }; + } + + return transformOptions; +} + +module.exports = removeInlineRequiresBlacklistFromOptions; diff --git a/packages/metro-bundler/src/transformer.js b/packages/metro-bundler/src/transformer.js index 2f7793cb..203849ef 100644 --- a/packages/metro-bundler/src/transformer.js +++ b/packages/metro-bundler/src/transformer.js @@ -99,10 +99,7 @@ function buildBabelConfig(filename, options, plugins?: BabelPlugins = []) { // Add extra plugins const extraPlugins = [externalHelpersPlugin]; - var inlineRequires = options.inlineRequires; - var blacklist = - typeof inlineRequires === 'object' ? inlineRequires.blacklist : null; - if (inlineRequires && !(blacklist && filename in blacklist)) { + if (options.inlineRequires) { extraPlugins.push(inlineRequiresPlugin); }