metro: do not override process.env completely in prelude

Summary: This changeset tweaks the prelude so as to avoid overriding `process.env` if it already exists, and add tests to verify this behavior. This is useful when a bundle is loaded in a context that already provides a global `process` somehow, ex. Electron/Node.js. We still do want to override `NODE_ENV` in that case so as to have consistency with constant inlining.

Reviewed By: rafeca

Differential Revision: D6702441

fbshipit-source-id: 69dd9ba2303a43db151cbe1877f01e38d45b05b9
This commit is contained in:
Jean Lauliac 2018-01-11 09:54:53 -08:00 committed by Facebook Github Bot
parent 325a216442
commit 67385d8e4a
5 changed files with 70 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

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