From 521653798fb14a9dc5852dabb893ad085274825c Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 8 Jan 2018 10:56:28 -0800 Subject: [PATCH] metro, metro-buck: preprend NODE_ENV variable to bundles Reviewed By: rafeca Differential Revision: D6674216 fbshipit-source-id: ca0d17f679eb8fb8dd4c50192836faaf02d1aba8 --- packages/metro/src/ModuleGraph/ModuleGraph.js | 3 ++- .../src/ModuleGraph/__tests__/ModuleGraph-test.js | 6 ++++-- packages/metro/src/Resolver/polyfills/prelude.js | 2 ++ packages/metro/src/Resolver/polyfills/prelude_dev.js | 2 ++ .../__tests__/__snapshots__/basic_bundle-test.js.snap | 10 ++++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/metro/src/ModuleGraph/ModuleGraph.js b/packages/metro/src/ModuleGraph/ModuleGraph.js index e6491049..d0d49af2 100644 --- a/packages/metro/src/ModuleGraph/ModuleGraph.js +++ b/packages/metro/src/ModuleGraph/ModuleGraph.js @@ -66,6 +66,7 @@ exports.createBuildSetup = ( function prelude(optimize) { return virtualModule( `var __DEV__=${String(!optimize)},` + - '__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now();', + '__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),' + + `process={env:{NODE_ENV:'${optimize ? 'production' : 'development'}'}};`, ); } diff --git a/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js b/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js index b955064b..06621fb5 100644 --- a/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js +++ b/packages/metro/src/ModuleGraph/__tests__/ModuleGraph-test.js @@ -35,7 +35,8 @@ describe('build setup', () => { file: { code: 'var __DEV__=true,__BUNDLE_START_TIME__=' + - 'this.nativePerformanceNow?nativePerformanceNow():Date.now();', + 'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' + + "process={env:{NODE_ENV:'development'}};", map: null, path: '', type: 'script', @@ -48,7 +49,8 @@ describe('build setup', () => { const [prelude] = result.modules; expect(prelude.file.code).toEqual( 'var __DEV__=false,__BUNDLE_START_TIME__=' + - 'this.nativePerformanceNow?nativePerformanceNow():Date.now();', + 'this.nativePerformanceNow?nativePerformanceNow():Date.now(),' + + "process={env:{NODE_ENV:'production'}};", ); }); diff --git a/packages/metro/src/Resolver/polyfills/prelude.js b/packages/metro/src/Resolver/polyfills/prelude.js index c4afe04a..62493f86 100644 --- a/packages/metro/src/Resolver/polyfills/prelude.js +++ b/packages/metro/src/Resolver/polyfills/prelude.js @@ -15,3 +15,5 @@ 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/Resolver/polyfills/prelude_dev.js b/packages/metro/src/Resolver/polyfills/prelude_dev.js index 068785bc..c615f723 100644 --- a/packages/metro/src/Resolver/polyfills/prelude_dev.js +++ b/packages/metro/src/Resolver/polyfills/prelude_dev.js @@ -15,3 +15,5 @@ global.__DEV__ = true; global.__BUNDLE_START_TIME__ = global.nativePerformanceNow ? global.nativePerformanceNow() : Date.now(); + +global.process = {env: {NODE_ENV: '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 36c76c9d..46d79572 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 @@ -4,6 +4,11 @@ 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); (function (global) { 'use strict'; @@ -195,6 +200,11 @@ 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); (function (global) { 'use strict';