diff --git a/packages/metro/src/DeltaBundler/DeltaTransformer.js b/packages/metro/src/DeltaBundler/DeltaTransformer.js index 20a4ae4d..020a6527 100644 --- a/packages/metro/src/DeltaBundler/DeltaTransformer.js +++ b/packages/metro/src/DeltaBundler/DeltaTransformer.js @@ -17,6 +17,7 @@ const DeltaCalculator = require('./DeltaCalculator'); const addParamsToDefineCall = require('../lib/addParamsToDefineCall'); const createModuleIdFactory = require('../lib/createModuleIdFactory'); const defaults = require('../defaults'); +const getPreludeCode = require('../lib/getPreludeCode'); const nullthrows = require('fbjs/lib/nullthrows'); const path = require('path'); const removeInlineRequiresBlacklistFromOptions = require('../lib/removeInlineRequiresBlacklistFromOptions'); @@ -324,6 +325,8 @@ class DeltaTransformer extends EventEmitter { transformOptions: JSTransformerOptions, dependencyEdges: DependencyEdges, ): Promise { + const preludeId = this._getModuleId('__prelude__'); + // Get all the polyfills from the relevant option params (the // `getPolyfills()` method and the `polyfillModuleNames` variable). const polyfillModuleNames = this._getPolyfills({ @@ -332,7 +335,7 @@ class DeltaTransformer extends EventEmitter { // Build the module system dependencies (scripts that need to // be included at the very beginning of the bundle) + any polifyll. - const modules = this._getModuleSystemDependencies() + const modules = [defaults.moduleSystem] .concat(polyfillModuleNames) .map(polyfillModuleName => this._dependencyGraph.createPolyfill({ @@ -342,11 +345,23 @@ class DeltaTransformer extends EventEmitter { }), ); - return await this._transformModules( + const transformedModules = await this._transformModules( modules, transformOptions, 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 { @@ -526,16 +541,6 @@ class DeltaTransformer extends EventEmitter { _onFileChange = () => { 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; diff --git a/packages/metro/src/ModuleGraph/ModuleGraph.js b/packages/metro/src/ModuleGraph/ModuleGraph.js index d0d49af2..1eea315b 100644 --- a/packages/metro/src/ModuleGraph/ModuleGraph.js +++ b/packages/metro/src/ModuleGraph/ModuleGraph.js @@ -13,6 +13,7 @@ const defaults = require('../defaults'); const virtualModule = require('./module').virtual; +const getPreludeCode = require('../lib/getPreludeCode'); import type {BuildResult, GraphFn, PostProcessModules} from './types.flow'; @@ -54,7 +55,7 @@ exports.createBuildSetup = ( ]); const {entryModules} = graph; - const preludeScript = prelude(optimize); + const preludeScript = virtualModule(getPreludeCode({isDev: !optimize})); const prependedScripts = [preludeScript, ...moduleSystem, ...polyfills]; return { entryModules, @@ -62,11 +63,3 @@ exports.createBuildSetup = ( 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'}'}};`, - ); -} diff --git a/packages/metro/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap b/packages/metro/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap index 46d79572..40681153 100644 --- a/packages/metro/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap +++ b/packages/metro/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap @@ -1,15 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`basic_bundle bundles package with polyfills 1`] = ` -"(function (global) { - global.__DEV__ = false; - global.__BUNDLE_START_TIME__ = global.nativePerformanceNow ? global.nativePerformanceNow() : Date.now(); - global.process = { - env: { - NODE_ENV: 'production' - } - }; -})(this); +"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}}; (function (global) { 'use strict'; @@ -197,15 +189,7 @@ require(4);" `; exports[`basic_bundle bundles package without polyfills 1`] = ` -"(function (global) { - global.__DEV__ = false; - global.__BUNDLE_START_TIME__ = global.nativePerformanceNow ? global.nativePerformanceNow() : Date.now(); - global.process = { - env: { - NODE_ENV: 'production' - } - }; -})(this); +"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}}; (function (global) { 'use strict'; diff --git a/packages/metro/src/lib/getPreludeCode.js b/packages/metro/src/lib/getPreludeCode.js new file mode 100644 index 00000000..1807010f --- /dev/null +++ b/packages/metro/src/lib/getPreludeCode.js @@ -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; diff --git a/packages/metro/src/lib/polyfills/prelude.js b/packages/metro/src/lib/polyfills/prelude.js deleted file mode 100644 index 62493f86..00000000 --- a/packages/metro/src/lib/polyfills/prelude.js +++ /dev/null @@ -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'}}; diff --git a/packages/metro/src/lib/polyfills/prelude_dev.js b/packages/metro/src/lib/polyfills/prelude_dev.js deleted file mode 100644 index c615f723..00000000 --- a/packages/metro/src/lib/polyfills/prelude_dev.js +++ /dev/null @@ -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'}};