metro, metro-buck: unify prelude code

Reviewed By: rafeca

Differential Revision: D6692449

fbshipit-source-id: 21434de55d076bcb020e156212c26b6bc72e437c
This commit is contained in:
Jean Lauliac 2018-01-11 04:51:45 -08:00 committed by Facebook Github Bot
parent 3ee6e5bfb5
commit 325a216442
6 changed files with 44 additions and 77 deletions

View File

@ -17,6 +17,7 @@ const DeltaCalculator = require('./DeltaCalculator');
const addParamsToDefineCall = require('../lib/addParamsToDefineCall'); const addParamsToDefineCall = require('../lib/addParamsToDefineCall');
const createModuleIdFactory = require('../lib/createModuleIdFactory'); const createModuleIdFactory = require('../lib/createModuleIdFactory');
const defaults = require('../defaults'); const defaults = require('../defaults');
const getPreludeCode = require('../lib/getPreludeCode');
const nullthrows = require('fbjs/lib/nullthrows'); const nullthrows = require('fbjs/lib/nullthrows');
const path = require('path'); const path = require('path');
const removeInlineRequiresBlacklistFromOptions = require('../lib/removeInlineRequiresBlacklistFromOptions'); const removeInlineRequiresBlacklistFromOptions = require('../lib/removeInlineRequiresBlacklistFromOptions');
@ -324,6 +325,8 @@ class DeltaTransformer extends EventEmitter {
transformOptions: JSTransformerOptions, transformOptions: JSTransformerOptions,
dependencyEdges: DependencyEdges, dependencyEdges: DependencyEdges,
): Promise<DeltaEntries> { ): Promise<DeltaEntries> {
const preludeId = this._getModuleId('__prelude__');
// Get all the polyfills from the relevant option params (the // Get all the polyfills from the relevant option params (the
// `getPolyfills()` method and the `polyfillModuleNames` variable). // `getPolyfills()` method and the `polyfillModuleNames` variable).
const polyfillModuleNames = this._getPolyfills({ const polyfillModuleNames = this._getPolyfills({
@ -332,7 +335,7 @@ class DeltaTransformer extends EventEmitter {
// Build the module system dependencies (scripts that need to // Build the module system dependencies (scripts that need to
// be included at the very beginning of the bundle) + any polifyll. // be included at the very beginning of the bundle) + any polifyll.
const modules = this._getModuleSystemDependencies() const modules = [defaults.moduleSystem]
.concat(polyfillModuleNames) .concat(polyfillModuleNames)
.map(polyfillModuleName => .map(polyfillModuleName =>
this._dependencyGraph.createPolyfill({ this._dependencyGraph.createPolyfill({
@ -342,11 +345,23 @@ class DeltaTransformer extends EventEmitter {
}), }),
); );
return await this._transformModules( const transformedModules = await this._transformModules(
modules, modules,
transformOptions, transformOptions,
dependencyEdges, dependencyEdges,
); );
// The prelude needs to be the first thing in the file, and the insertion
// order of entries in the Map is significant.
return new Map([
[preludeId, this._getPrelude(preludeId)],
...transformedModules,
]);
}
_getPrelude(id: number): DeltaEntry {
const code = getPreludeCode({isDev: this._bundleOptions.dev});
const name = '__prelude__';
return {code, id, map: [], name, source: code, path: name, type: 'script'};
} }
async _getAppend(dependencyEdges: DependencyEdges): Promise<DeltaEntries> { async _getAppend(dependencyEdges: DependencyEdges): Promise<DeltaEntries> {
@ -526,16 +541,6 @@ class DeltaTransformer extends EventEmitter {
_onFileChange = () => { _onFileChange = () => {
this.emit('change'); this.emit('change');
}; };
_getModuleSystemDependencies() {
const prelude = this._bundleOptions.dev
? path.resolve(__dirname, '../lib/polyfills/prelude_dev.js')
: path.resolve(__dirname, '../lib/polyfills/prelude.js');
const moduleSystem = defaults.moduleSystem;
return [prelude, moduleSystem];
}
} }
module.exports = DeltaTransformer; module.exports = DeltaTransformer;

View File

@ -13,6 +13,7 @@
const defaults = require('../defaults'); const defaults = require('../defaults');
const virtualModule = require('./module').virtual; const virtualModule = require('./module').virtual;
const getPreludeCode = require('../lib/getPreludeCode');
import type {BuildResult, GraphFn, PostProcessModules} from './types.flow'; import type {BuildResult, GraphFn, PostProcessModules} from './types.flow';
@ -54,7 +55,7 @@ exports.createBuildSetup = (
]); ]);
const {entryModules} = graph; const {entryModules} = graph;
const preludeScript = prelude(optimize); const preludeScript = virtualModule(getPreludeCode({isDev: !optimize}));
const prependedScripts = [preludeScript, ...moduleSystem, ...polyfills]; const prependedScripts = [preludeScript, ...moduleSystem, ...polyfills];
return { return {
entryModules, entryModules,
@ -62,11 +63,3 @@ exports.createBuildSetup = (
prependedScripts, prependedScripts,
}; };
}; };
function prelude(optimize) {
return virtualModule(
`var __DEV__=${String(!optimize)},` +
'__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),' +
`process={env:{NODE_ENV:'${optimize ? 'production' : 'development'}'}};`,
);
}

View File

@ -1,15 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`basic_bundle bundles package with polyfills 1`] = ` exports[`basic_bundle bundles package with polyfills 1`] = `
"(function (global) { "var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}};
global.__DEV__ = false;
global.__BUNDLE_START_TIME__ = global.nativePerformanceNow ? global.nativePerformanceNow() : Date.now();
global.process = {
env: {
NODE_ENV: 'production'
}
};
})(this);
(function (global) { (function (global) {
'use strict'; 'use strict';
@ -197,15 +189,7 @@ require(4);"
`; `;
exports[`basic_bundle bundles package without polyfills 1`] = ` exports[`basic_bundle bundles package without polyfills 1`] = `
"(function (global) { "var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}};
global.__DEV__ = false;
global.__BUNDLE_START_TIME__ = global.nativePerformanceNow ? global.nativePerformanceNow() : Date.now();
global.process = {
env: {
NODE_ENV: 'production'
}
};
})(this);
(function (global) { (function (global) {
'use strict'; 'use strict';

View File

@ -0,0 +1,23 @@
/**
* 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.
*
* @flow
* @format
*/
'use strict';
function getPreludeCode({isDev}: {|+isDev: boolean|}): string {
return (
`var __DEV__=${String(isDev)},` +
'__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),' +
`process={env:{NODE_ENV:'${isDev ? 'development' : 'production'}'}};`
);
}
module.exports = getPreludeCode;

View File

@ -1,19 +0,0 @@
/**
* Copyright (c) 2013-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.
*
* @polyfill
* @format
*/
global.__DEV__ = false;
global.__BUNDLE_START_TIME__ = global.nativePerformanceNow
? global.nativePerformanceNow()
: Date.now();
global.process = {env: {NODE_ENV: 'production'}};

View File

@ -1,19 +0,0 @@
/**
* Copyright (c) 2013-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.
*
* @polyfill
* @format
*/
global.__DEV__ = true;
global.__BUNDLE_START_TIME__ = global.nativePerformanceNow
? global.nativePerformanceNow()
: Date.now();
global.process = {env: {NODE_ENV: 'development'}};