Simplify transform options + pass minify to the transformer

Reviewed By: mjesun

Differential Revision: D6406614

fbshipit-source-id: 722e8e209c7b7c922139f0777b9b3bd2a77bf735
This commit is contained in:
Rafael Oleza 2017-11-24 12:54:44 -08:00 committed by Facebook Github Bot
parent 06466e4f6e
commit 75047399d8
9 changed files with 49 additions and 79 deletions

View File

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

View File

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

View File

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

View File

@ -90,6 +90,7 @@ describe('transforming JS modules:', () => {
generateSourceMaps: true,
hot: false,
inlineRequires: false,
minify: false,
platform: '',
projectRoot: '',
};

View File

@ -52,6 +52,7 @@ const defaultTransformOptions = {
generateSourceMaps: true,
hot: false,
inlineRequires: false,
minify: false,
platform: '',
projectRoot: '',
};

View File

@ -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') {

View File

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

View File

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

View File

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