diff --git a/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js b/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js index 06621fb5..26b048c3 100644 --- a/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js +++ b/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js @@ -30,28 +30,13 @@ describe('build setup', () => { const result = await buildSetup(noEntryPoints, noOptions); const [prelude] = result.modules; - expect(prelude).toEqual({ - dependencies: [], - file: { - code: - 'var __DEV__=true,__BUNDLE_START_TIME__=' + - 'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' + - "process={env:{NODE_ENV:'development'}};", - map: null, - path: '', - type: 'script', - }, - }); + expect(prelude).toMatchSnapshot(); }); it('sets `__DEV__` to false in the prelude if optimization is enabled', async () => { const result = await buildSetup(noEntryPoints, {optimize: true}); const [prelude] = result.modules; - expect(prelude.file.code).toEqual( - 'var __DEV__=false,__BUNDLE_START_TIME__=' + - 'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' + - "process={env:{NODE_ENV:'production'}};", - ); + expect(prelude).toMatchSnapshot(); }); it('places the module system implementation directly after the prelude', async () => { diff --git a/packages/metro/src/ModuleGraph/__tests__/__snapshots__/ModuleGraph-test.js.snap b/packages/metro/src/ModuleGraph/__tests__/__snapshots__/ModuleGraph-test.js.snap new file mode 100644 index 00000000..8796999b --- /dev/null +++ b/packages/metro/src/ModuleGraph/__tests__/__snapshots__/ModuleGraph-test.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`build setup adds a prelude containing start time and \`__DEV__\` to the build 1`] = ` +Object { + "dependencies": Array [], + "file": Object { + "code": "var __DEV__=true,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process=this.process||{};process.env=process.env||{};process.env.NODE_ENV='development';", + "map": null, + "path": "", + "type": "script", + }, +} +`; + +exports[`build setup sets \`__DEV__\` to false in the prelude if optimization is enabled 1`] = ` +Object { + "dependencies": Array [], + "file": Object { + "code": "var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process=this.process||{};process.env=process.env||{};process.env.NODE_ENV='production';", + "map": null, + "path": "", + "type": "script", + }, +} +`; 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 40681153..42f96672 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,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`basic_bundle bundles package with polyfills 1`] = ` -"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}}; +"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process=this.process||{};process.env=process.env||{};process.env.NODE_ENV='production'; (function (global) { 'use strict'; @@ -189,7 +189,7 @@ require(4);" `; exports[`basic_bundle bundles package without polyfills 1`] = ` -"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process={env:{NODE_ENV:'production'}}; +"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process=this.process||{};process.env=process.env||{};process.env.NODE_ENV='production'; (function (global) { 'use strict'; diff --git a/packages/metro/src/lib/__tests__/getPreludeCode-test.js b/packages/metro/src/lib/__tests__/getPreludeCode-test.js new file mode 100644 index 00000000..4773fb18 --- /dev/null +++ b/packages/metro/src/lib/__tests__/getPreludeCode-test.js @@ -0,0 +1,39 @@ +/** + * 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. + * + * @emails oncall+javascript_foundation + * @flow + * @format + */ + +'use strict'; + +const getPreludeCode = require('../getPreludeCode'); +const vm = require('vm'); + +['development', 'production'].forEach(mode => { + describe(`${mode} mode`, () => { + it('sets up `process.env.NODE_ENV` and `__DEV__`', () => { + const sandbox: $FlowFixMe = {}; + vm.createContext(sandbox); + vm.runInContext(getPreludeCode({isDev: mode == 'development'}), sandbox); + expect(sandbox.process.env.NODE_ENV).toEqual(mode); + expect(sandbox.__DEV__).toEqual(mode == 'development'); + }); + + it('does not override an existing `process.env`', () => { + const nextTick = () => {}; + const sandbox: $FlowFixMe = {process: {nextTick, env: {FOOBAR: 123}}}; + vm.createContext(sandbox); + vm.runInContext(getPreludeCode({isDev: mode == 'development'}), sandbox); + expect(sandbox.process.env.NODE_ENV).toEqual(mode); + expect(sandbox.process.env.FOOBAR).toEqual(123); + expect(sandbox.process.nextTick).toEqual(nextTick); + }); + }); +}); diff --git a/packages/metro/src/lib/getPreludeCode.js b/packages/metro/src/lib/getPreludeCode.js index 1807010f..acde8c7e 100644 --- a/packages/metro/src/lib/getPreludeCode.js +++ b/packages/metro/src/lib/getPreludeCode.js @@ -16,7 +16,8 @@ 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'}'}};` + 'process=this.process||{};process.env=process.env||{};' + + `process.env.NODE_ENV='${isDev ? 'development' : 'production'}';` ); }