Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metro-config): disable require rename #27853

Closed
Closed
1 change: 1 addition & 0 deletions packages/@expo/metro-config/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@

### 🎉 New features

- Skip renaming `require` in iife to speed up transformation. Add `EXPO_METRO_RENAME_REQUIRES` env to re-enable this functionality. ([#27853](https://github.com/expo/expo/pull/27853) by [@EvanBacon](https://github.com/EvanBacon))
- Add basic `react-server` support. ([#27264](https://github.com/expo/expo/pull/27264) by [@EvanBacon](https://github.com/EvanBacon))
- Add support for splitting on `require.resolveWeak` syntax. ([#27014](https://github.com/expo/expo/pull/27014) by [@EvanBacon](https://github.com/EvanBacon))

Expand Down
2 changes: 2 additions & 0 deletions packages/@expo/metro-config/build/env.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/@expo/metro-config/build/env.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@expo/metro-config/build/env.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/@expo/metro-config/src/env.ts
Expand Up @@ -25,6 +25,11 @@ class Env {
get EXPO_NO_CLIENT_ENV_VARS(): boolean {
return boolish('EXPO_NO_CLIENT_ENV_VARS', false);
}

/** Rename `require` statements to `_$$_REQUIRE` in an additional traversal pass during transformation. */
get EXPO_METRO_RENAME_REQUIRES(): boolean {
return boolish('EXPO_METRO_RENAME_REQUIRES', false);
}
}

export const env = new Env();
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`allows the constantFoldingPlugin to not remove used helpers when \`dev: false\` 1`] = `
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.test = undefined;
var test = _interopRequireWildcard(_$$_REQUIRE(_dependencyMap[0]));
var test = _interopRequireWildcard(require(_dependencyMap[0]));
exports.test = test;
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
Expand Down Expand Up @@ -43,13 +43,13 @@ exports[`transforms a simple script 1`] = `
`;

exports[`transforms an es module with asyncToGenerator 1`] = `
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
var _interopRequireDefault = require(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.test = test;
var _asyncToGenerator2 = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "@babel/runtime/helpers/asyncToGenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require(_dependencyMap[1], "@babel/runtime/helpers/asyncToGenerator"));
function test() {
return _test.apply(this, arguments);
}
Expand All @@ -71,14 +71,14 @@ exports[`transforms an es module with asyncToGenerator 2`] = `
`;

exports[`transforms async generators 1`] = `
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
var _interopRequireDefault = require(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.test = test;
var _awaitAsyncGenerator2 = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "@babel/runtime/helpers/awaitAsyncGenerator"));
var _wrapAsyncGenerator2 = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[2], "@babel/runtime/helpers/wrapAsyncGenerator"));
var _awaitAsyncGenerator2 = _interopRequireDefault(require(_dependencyMap[1], "@babel/runtime/helpers/awaitAsyncGenerator"));
var _wrapAsyncGenerator2 = _interopRequireDefault(require(_dependencyMap[2], "@babel/runtime/helpers/wrapAsyncGenerator"));
function test() {
return _test.apply(this, arguments);
}
Expand Down
Expand Up @@ -58,7 +58,7 @@ const babelTransformerPath = require.resolve('@expo/metro-config/babel-transform
const transformerContents = jest.requireActual('fs').readFileSync(babelTransformerPath);

const HEADER_DEV =
'__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {';
'__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {';
const HEADER_PROD = '__d(function (g, r, i, a, m, e, d) {';

// let fs: typeof import('fs');
Expand Down Expand Up @@ -234,11 +234,11 @@ it('transforms a module with dependencies', async () => {
HEADER_DEV,
' "use strict";',
'',
' var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");',
' var _c = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "./c"));',
' _$$_REQUIRE(_dependencyMap[2], "./a");',
' var _interopRequireDefault = require(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");',
' var _c = _interopRequireDefault(require(_dependencyMap[1], "./c"));',
' require(_dependencyMap[2], "./a");',
' arbitrary(code);',
' var b = _$$_REQUIRE(_dependencyMap[3], "b");',
' var b = require(_dependencyMap[3], "b");',
'});',
].join('\n')
);
Expand Down Expand Up @@ -279,7 +279,7 @@ it('transforms a module with dependencies', async () => {
expect(originalPositionFor(trace, { line: 4, column: 31 })).toMatchObject({
line: 1,
column: 13,
name: '_$$_REQUIRE',
name: 'require',
});
expect(originalPositionFor(trace, { line: 4, column: 43 })).toMatchObject({
line: 1,
Expand Down Expand Up @@ -593,7 +593,7 @@ it('allows disabling the normalizePseudoGlobals pass when minifying', async () =
{ ...baseTransformOptions, dev: false, minify: true }
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
minified(code);
});"
`);
Expand All @@ -608,7 +608,7 @@ it('allows emitting compact code when not minifying', async () => {
{ ...baseTransformOptions, dev: false, minify: false }
);
expect(result.output[0].data.code).toMatchInlineSnapshot(
`"__d(function(global,_$$_REQUIRE,_$$_IMPORT_DEFAULT,_$$_IMPORT_ALL,module,exports,_dependencyMap){arbitrary(code);});"`
`"__d(function(global,require,_$$_IMPORT_DEFAULT,_$$_IMPORT_ALL,module,exports,_dependencyMap){arbitrary(code);});"`
);
});

Expand All @@ -627,7 +627,7 @@ it('skips minification in Hermes stable transform profile', async () => {
}
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
arbitrary(code);
});"
`);
Expand All @@ -648,7 +648,7 @@ it('skips minification in Hermes canary transform profile', async () => {
}
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
arbitrary(code);
});"
`);
Expand Down Expand Up @@ -700,7 +700,7 @@ it('outputs comments when `minify: false`', async () => {
{ ...baseTransformOptions, dev: false, minify: false }
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
"__d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
/*#__PURE__*/arbitrary(code);
});"
`);
Expand Down
Expand Up @@ -42,6 +42,8 @@ import assert from 'node:assert';

import * as assetTransformer from './asset-transformer';
import { shouldMinify } from './resolveOptions';
import { wrapModule } from './wrapModule';
import { env } from '../env';

export { JsTransformOptions };

Expand Down Expand Up @@ -327,12 +329,13 @@ async function transformJS(
wrappedAst = ast;
} else {
// TODO: Replace this with a cheaper transform that doesn't require AST.
({ ast: wrappedAst } = JsFileWrapping.wrapModule(
({ ast: wrappedAst } = wrapModule(
ast,
importDefault,
importAll,
dependencyMapName,
config.globalPrefix
config.globalPrefix,
!env.EXPO_METRO_RENAME_REQUIRES
));
}
}
Expand Down Expand Up @@ -585,6 +588,7 @@ export function getCacheKey(config: JsTransformerConfig): string {
require.resolve(babelTransformerPath),
require.resolve(minifierPath),
require.resolve('metro-transform-worker/src/utils/getMinifier'),
require.resolve('./wrapModule'),
require.resolve('./asset-transformer'),
require.resolve('metro/src/ModuleGraph/worker/generateImportNames'),
require.resolve('metro/src/ModuleGraph/worker/JsFileWrapping'),
Expand Down