mirror of
https://github.com/status-im/metro.git
synced 2025-01-23 09:30:55 +00:00
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:
parent
325a216442
commit
67385d8e4a
@ -30,28 +30,13 @@ describe('build setup', () => {
|
|||||||
const result = await buildSetup(noEntryPoints, noOptions);
|
const result = await buildSetup(noEntryPoints, noOptions);
|
||||||
|
|
||||||
const [prelude] = result.modules;
|
const [prelude] = result.modules;
|
||||||
expect(prelude).toEqual({
|
expect(prelude).toMatchSnapshot();
|
||||||
dependencies: [],
|
|
||||||
file: {
|
|
||||||
code:
|
|
||||||
'var __DEV__=true,__BUNDLE_START_TIME__=' +
|
|
||||||
'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' +
|
|
||||||
"process={env:{NODE_ENV:'development'}};",
|
|
||||||
map: null,
|
|
||||||
path: '',
|
|
||||||
type: 'script',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets `__DEV__` to false in the prelude if optimization is enabled', async () => {
|
it('sets `__DEV__` to false in the prelude if optimization is enabled', async () => {
|
||||||
const result = await buildSetup(noEntryPoints, {optimize: true});
|
const result = await buildSetup(noEntryPoints, {optimize: true});
|
||||||
const [prelude] = result.modules;
|
const [prelude] = result.modules;
|
||||||
expect(prelude.file.code).toEqual(
|
expect(prelude).toMatchSnapshot();
|
||||||
'var __DEV__=false,__BUNDLE_START_TIME__=' +
|
|
||||||
'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' +
|
|
||||||
"process={env:{NODE_ENV:'production'}};",
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('places the module system implementation directly after the prelude', async () => {
|
it('places the module system implementation directly after the prelude', async () => {
|
||||||
|
@ -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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`;
|
@ -1,7 +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`] = `
|
||||||
"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) {
|
(function (global) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
@ -189,7 +189,7 @@ require(4);"
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`basic_bundle bundles package without polyfills 1`] = `
|
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) {
|
(function (global) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
39
packages/metro/src/lib/__tests__/getPreludeCode-test.js
Normal file
39
packages/metro/src/lib/__tests__/getPreludeCode-test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -16,7 +16,8 @@ function getPreludeCode({isDev}: {|+isDev: boolean|}): string {
|
|||||||
return (
|
return (
|
||||||
`var __DEV__=${String(isDev)},` +
|
`var __DEV__=${String(isDev)},` +
|
||||||
'__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),' +
|
'__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'}';`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user