From a2e77f5da0571ef5f0b36e1cbd08a5002248921e Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Mon, 6 Jul 2020 03:08:11 -0700 Subject: [PATCH] Patch metro to avoid erasing "process" global var Summary: Patch for "metro" to avoid erasing of "process" global var during bundling. Also removed "process" babel transform for main Electron process which was also made to workaround the same issue with "process" being erased. Reviewed By: mweststrate Differential Revision: D22389153 fbshipit-source-id: 569882e20534eedfca45509b8efe0186d335c681 --- .../src/__tests__/electron-process.node.ts | 66 ------------------- .../babel-transformer/src/electron-process.ts | 34 ---------- .../babel-transformer/src/transform-main.ts | 5 +- desktop/patches/metro+0.59.0.patch | 13 ++++ 4 files changed, 14 insertions(+), 104 deletions(-) delete mode 100644 desktop/babel-transformer/src/__tests__/electron-process.node.ts delete mode 100644 desktop/babel-transformer/src/electron-process.ts diff --git a/desktop/babel-transformer/src/__tests__/electron-process.node.ts b/desktop/babel-transformer/src/__tests__/electron-process.node.ts deleted file mode 100644 index 6d715962c..000000000 --- a/desktop/babel-transformer/src/__tests__/electron-process.node.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -import {transform} from '@babel/core'; -const electronProcess = require('../electron-process'); - -const babelOptions = { - ast: true, - plugins: [electronProcess], - filename: 'index.js', -}; - -test('transform "process.exit(0);"', () => { - const src = 'process.exit(0);'; - const code = transform(src, babelOptions)!.code; - expect(code).toMatchInlineSnapshot(`"electronProcess.exit(0);"`); -}); - -test('transform "global.process.exit(0);"', () => { - const src = 'global.process.exit(0);'; - const code = transform(src, babelOptions)!.code; - expect(code).toMatchInlineSnapshot(`"global.electronProcess.exit(0);"`); -}); - -test('transform "process.ENV.TEST = "true";"', () => { - const src = 'process.ENV.TEST = "true";'; - const code = transform(src, babelOptions)!.code; - expect(code).toMatchInlineSnapshot( - `"electronProcess.ENV.TEST = \\"true\\";"`, - ); -}); - -test('do not transform if process bound in an upper scope', () => { - const src = ` - const process = {}; - for (const i=0; i<10; i++) { - process.ENV[i] = i; - } - `; - const code = transform(src, babelOptions)!.code; - expect(code).toMatchInlineSnapshot(` - "const process = {}; - - for (const i = 0; i < 10; i++) { - process.ENV[i] = i; - }" - `); -}); - -test('do not transform if process bound to the current scope', () => { - const src = ` - const process = {}; - process.ENV.TEST = "true"; - `; - const code = transform(src, babelOptions)!.code; - expect(code).toMatchInlineSnapshot(` - "const process = {}; - process.ENV.TEST = \\"true\\";" - `); -}); diff --git a/desktop/babel-transformer/src/electron-process.ts b/desktop/babel-transformer/src/electron-process.ts deleted file mode 100644 index c61ef9a84..000000000 --- a/desktop/babel-transformer/src/electron-process.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -import {MemberExpression} from '@babel/types'; -import {NodePath} from '@babel/traverse'; - -module.exports = () => ({ - name: 'change-process-to-electronProcess', - visitor: { - MemberExpression(path: NodePath) { - if ( - path.node.object.type === 'Identifier' && - path.node.object.name === 'process' && - !path.scope.hasBinding('process') - ) { - path.node.object.name = 'electronProcess'; - } else if ( - path.node.object.type === 'MemberExpression' && - path.node.object.object.type === 'Identifier' && - path.node.object.object.name === 'global' && - path.node.object.property.type === 'Identifier' && - path.node.object.property.name === 'process' - ) { - path.node.object.property.name = 'electronProcess'; - } - }, - }, -}); diff --git a/desktop/babel-transformer/src/transform-main.ts b/desktop/babel-transformer/src/transform-main.ts index b06653b18..1e3cf9a16 100644 --- a/desktop/babel-transformer/src/transform-main.ts +++ b/desktop/babel-transformer/src/transform-main.ts @@ -17,10 +17,7 @@ const presets = [ {targets: {electron: flipperEnv.FLIPPER_ELECTRON_VERSION}}, ], ]; -const plugins = [ - require('./electron-requires-main'), - require('./electron-process'), -]; +const plugins = [require('./electron-requires-main')]; if (flipperEnv.FLIPPER_FB) { plugins.unshift(require('./fb-stubs')); } diff --git a/desktop/patches/metro+0.59.0.patch b/desktop/patches/metro+0.59.0.patch index c4862d1d6..8caaf30ad 100644 --- a/desktop/patches/metro+0.59.0.patch +++ b/desktop/patches/metro+0.59.0.patch @@ -1,3 +1,16 @@ +diff --git a/node_modules/metro/src/lib/getPreludeCode.js b/node_modules/metro/src/lib/getPreludeCode.js +index 57e008e..b645266 100644 +--- a/node_modules/metro/src/lib/getPreludeCode.js ++++ b/node_modules/metro/src/lib/getPreludeCode.js +@@ -42,7 +42,7 @@ function getPreludeCode(_ref) { + "__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now()", + `__DEV__=${String(isDev)}` + ].concat(_toConsumableArray(formatExtraVars(extraVars)), [ +- "process=this.process||{}" ++ "process=process||this.process||global.process||{}" + ]); + return `var ${vars.join(",")};${processEnv( + isDev ? "development" : "production" diff --git a/node_modules/metro/src/lib/polyfills/require.js b/node_modules/metro/src/lib/polyfills/require.js index 8c04756..d773811 100644 --- a/node_modules/metro/src/lib/polyfills/require.js