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
This commit is contained in:
Anton Nikolaev
2020-07-06 03:08:11 -07:00
committed by Facebook GitHub Bot
parent 7c3d264803
commit a2e77f5da0
4 changed files with 14 additions and 104 deletions

View File

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

View File

@@ -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<MemberExpression>) {
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';
}
},
},
});

View File

@@ -17,10 +17,7 @@ const presets = [
{targets: {electron: flipperEnv.FLIPPER_ELECTRON_VERSION}}, {targets: {electron: flipperEnv.FLIPPER_ELECTRON_VERSION}},
], ],
]; ];
const plugins = [ const plugins = [require('./electron-requires-main')];
require('./electron-requires-main'),
require('./electron-process'),
];
if (flipperEnv.FLIPPER_FB) { if (flipperEnv.FLIPPER_FB) {
plugins.unshift(require('./fb-stubs')); plugins.unshift(require('./fb-stubs'));
} }

View File

@@ -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 diff --git a/node_modules/metro/src/lib/polyfills/require.js b/node_modules/metro/src/lib/polyfills/require.js
index 8c04756..d773811 100644 index 8c04756..d773811 100644
--- a/node_modules/metro/src/lib/polyfills/require.js --- a/node_modules/metro/src/lib/polyfills/require.js