Make inlineRequires boolean when passed to the transformer

Reviewed By: mjesun

Differential Revision: D6408081

fbshipit-source-id: 812c8e3983677f4133ef3080525859a4a70418b7
This commit is contained in:
Rafael Oleza 2017-11-26 11:11:55 -08:00 committed by Facebook Github Bot
parent 2d03a34bbc
commit 091f8560f2
8 changed files with 104 additions and 20 deletions

View File

@ -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 = () => {

View File

@ -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;

View File

@ -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,

View File

@ -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;
}

View File

@ -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 [],
},

View File

@ -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',
});
});

View File

@ -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;

View File

@ -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);
}