Skip to content

Commit db241c9

Browse files
robhoganfacebook-github-bot
authored andcommitted
Pass globalPrefix through to serializer and expose to getRunModuleStatement (#1566)
Summary: Pull Request resolved: #1566 To allow prefixing the global function `__r` (e.g., #1512), expose the prefix to the configured [`getRunModuleStatement`](https://metrobundler.dev/docs/configuration/#getrunmodulestatement), which returns the `__r()` call as a string. ``` - **[Feature]** Expose `globalPrefix` to `getRunModuleStatement` ``` ## Alternative? Runtime prefixing We could alternatively add prefix at runtime by emitting: ``` "globalThis[__METRO_GLOBAL_PREFIX__ + '__r'](/*...*/)" ``` There are a couple of downsides - - These runModule statements are currently outside IIFEs that inject `global` based on (something like) `global = globalThis ?? global ?? window` - we don't rely on the existence of `globalThis` elsewhere (though it's probably safe to now - that's a separate breaking change). - Concatenation/interpolation + object access is more verbose and slightly slower, and there's just no need for it in code emitted by Metro itself (as opposed to framework/userland code, which must use it). Reviewed By: huntie Differential Revision: D81476621 fbshipit-source-id: 90641cf21491711c56606131ec4c5797b7b00020
1 parent 98badca commit db241c9

11 files changed

Lines changed: 47 additions & 9 deletions

File tree

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ This option only has an effect under the default [`transformerPath`](#transforme
562562
563563
#### `getRunModuleStatement`
564564
565-
Type: `(number | string) => string`
565+
Type: `(moduleId: number | string, globalPrefix: string) => string`
566566
567567
Specify the format of the initial require statements that are appended at the end of the bundle. By default is `__r(${moduleId});`.
568568

packages/metro-config/src/defaults/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
5858

5959
serializer: {
6060
polyfillModuleNames: [],
61-
getRunModuleStatement: (moduleId: number | string) =>
61+
getRunModuleStatement: (moduleId: number | string, globalPrefix: string) =>
6262
`__r(${JSON.stringify(moduleId)});`,
6363
getPolyfills: () => [],
6464
getModulesRunBeforeMainModule: () => [],

packages/metro-config/src/types.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ type SerializerConfigT = {
134134
) => mixed,
135135
getModulesRunBeforeMainModule: (entryFilePath: string) => Array<string>,
136136
getPolyfills: ({platform: ?string, ...}) => $ReadOnlyArray<string>,
137-
getRunModuleStatement: (number | string) => string,
137+
getRunModuleStatement: (
138+
moduleId: number | string,
139+
globalPrefix: string,
140+
) => string,
138141
polyfillModuleNames: $ReadOnlyArray<string>,
139142
processModuleFilter: (modules: Module<>) => boolean,
140143
isThirdPartyModule: (module: $ReadOnly<{path: string, ...}>) => boolean,

packages/metro-config/types/types.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export interface SerializerConfigT {
132132
) => unknown;
133133
getModulesRunBeforeMainModule: (entryFilePath: string) => string[];
134134
getPolyfills: (options: {platform: string | null}) => ReadonlyArray<string>;
135-
getRunModuleStatement: (moduleId: number | string) => string;
135+
getRunModuleStatement: (
136+
moduleId: number | string,
137+
globalPrefix: string,
138+
) => string;
136139
polyfillModuleNames: ReadonlyArray<string>;
137140
processModuleFilter: (modules: Module) => boolean;
138141
isThirdPartyModule: (module: {readonly path: string}) => boolean;

packages/metro/src/DeltaBundler/Serializers/__tests__/baseJSBundle-test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ const nonAsciiModule: Module<> = {
9494
getSource: () => Buffer.from('bar-source'),
9595
};
9696

97-
const getRunModuleStatement = (moduleId: number | string) =>
98-
`require(${JSON.stringify(moduleId)});`;
97+
const getRunModuleStatement = jest.fn(
98+
(moduleId: number | string, globalPrefix: string) =>
99+
`require(${JSON.stringify(moduleId)});`,
100+
);
99101

100102
const transformOptions: TransformInputOptions = {
101103
customTransformOptions: {},
@@ -107,6 +109,10 @@ const transformOptions: TransformInputOptions = {
107109
unstable_transformProfile: 'default',
108110
};
109111

112+
beforeEach(() => {
113+
jest.clearAllMocks();
114+
});
115+
110116
test('should generate a very simple bundle', () => {
111117
expect(
112118
baseJSBundle(
@@ -126,6 +132,7 @@ test('should generate a very simple bundle', () => {
126132
createModuleId: filePath => path.basename(filePath),
127133
dev: true,
128134
getRunModuleStatement,
135+
globalPrefix: 'customPrefix',
129136
includeAsyncPaths: false,
130137
inlineSourceMap: false,
131138
modulesOnly: false,
@@ -157,6 +164,8 @@ test('should generate a very simple bundle', () => {
157164
"pre": "__d(function() {/* code for polyfill */});",
158165
}
159166
`);
167+
168+
expect(getRunModuleStatement).toHaveBeenCalledWith('foo', 'customPrefix');
160169
});
161170

162171
test('should generate a bundle with correct non ascii characters parsing', () => {
@@ -177,6 +186,7 @@ test('should generate a bundle with correct non ascii characters parsing', () =>
177186
createModuleId: filePath => path.basename(filePath),
178187
dev: true,
179188
getRunModuleStatement,
189+
globalPrefix: '',
180190
includeAsyncPaths: false,
181191
inlineSourceMap: false,
182192
modulesOnly: false,
@@ -235,6 +245,7 @@ test('should add runBeforeMainModule statements if found in the graph', () => {
235245
createModuleId: filePath => path.basename(filePath),
236246
dev: true,
237247
getRunModuleStatement,
248+
globalPrefix: '',
238249
includeAsyncPaths: false,
239250
inlineSourceMap: false,
240251
modulesOnly: false,
@@ -274,6 +285,7 @@ test('should handle numeric module ids', () => {
274285
createModuleId: createModuleIdFactory(),
275286
dev: true,
276287
getRunModuleStatement,
288+
globalPrefix: '',
277289
includeAsyncPaths: false,
278290
inlineSourceMap: false,
279291
modulesOnly: false,
@@ -322,6 +334,7 @@ test('outputs custom runModule statements', () => {
322334
dev: true,
323335
getRunModuleStatement: moduleId =>
324336
`export default require(${JSON.stringify(moduleId)}).default;`,
337+
globalPrefix: '',
325338
includeAsyncPaths: false,
326339
inlineSourceMap: false,
327340
modulesOnly: false,
@@ -360,6 +373,7 @@ test('should add an inline source map to a very simple bundle', () => {
360373
createModuleId: filePath => path.basename(filePath),
361374
dev: true,
362375
getRunModuleStatement,
376+
globalPrefix: '',
363377
includeAsyncPaths: false,
364378
inlineSourceMap: true,
365379
modulesOnly: false,
@@ -411,6 +425,7 @@ test('emits x_google_ignoreList based on shouldAddToIgnoreList', () => {
411425
createModuleId: filePath => path.basename(filePath),
412426
dev: true,
413427
getRunModuleStatement,
428+
globalPrefix: '',
414429
includeAsyncPaths: false,
415430
inlineSourceMap: true,
416431
modulesOnly: false,
@@ -462,6 +477,7 @@ test('does not add polyfills when `modulesOnly` is used', () => {
462477
createModuleId: filePath => path.basename(filePath),
463478
dev: true,
464479
getRunModuleStatement,
480+
globalPrefix: '',
465481
includeAsyncPaths: false,
466482
inlineSourceMap: false,
467483
modulesOnly: true,

packages/metro/src/DeltaBundler/Serializers/__tests__/getRamBundleInfo-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test('should return the RAM bundle info', async () => {
9494
preloadedModules: {},
9595
ramGroups: [],
9696
}),
97+
globalPrefix: '',
9798
includeAsyncPaths: false,
9899
inlineSourceMap: false,
99100
modulesOnly: false,
@@ -129,6 +130,7 @@ test('emits x_google_ignoreList based on shouldAddToIgnoreList', async () => {
129130
preloadedModules: {},
130131
ramGroups: [],
131132
}),
133+
globalPrefix: '',
132134
includeAsyncPaths: false,
133135
inlineSourceMap: false,
134136
modulesOnly: false,
@@ -167,6 +169,7 @@ test('should use the preloadedModules and ramGroup configs to build a RAM bundle
167169
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
168170
* https://fburl.com/workplace/6291gfvu */
169171
getTransformOptions,
172+
globalPrefix: '',
170173
includeAsyncPaths: false,
171174
inlineSourceMap: null,
172175
modulesOnly: false,

packages/metro/src/DeltaBundler/Serializers/baseJSBundle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default function baseJSBundle(
5959
asyncRequireModulePath: options.asyncRequireModulePath,
6060
createModuleId: options.createModuleId,
6161
getRunModuleStatement: options.getRunModuleStatement,
62+
globalPrefix: options.globalPrefix,
6263
inlineSourceMap: options.inlineSourceMap,
6364
runBeforeMainModule: options.runBeforeMainModule,
6465
runModule: options.runModule,

packages/metro/src/DeltaBundler/types.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ export type SerializerOptions = $ReadOnly<{
168168
asyncRequireModulePath: string,
169169
createModuleId: string => number,
170170
dev: boolean,
171-
getRunModuleStatement: (number | string) => string,
171+
getRunModuleStatement: (
172+
moduleId: number | string,
173+
globalPrefix: string,
174+
) => string,
175+
globalPrefix: string,
172176
includeAsyncPaths: boolean,
173177
inlineSourceMap: ?boolean,
174178
modulesOnly: boolean,

packages/metro/src/Server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export default class Server {
235235
processModuleFilter: this._config.serializer.processModuleFilter,
236236
createModuleId: this._createModuleId,
237237
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
238+
globalPrefix: this._config.transformer.globalPrefix,
238239
dev: transformOptions.dev,
239240
includeAsyncPaths: graphOptions.lazy,
240241
projectRoot: this._config.projectRoot,
@@ -380,6 +381,7 @@ export default class Server {
380381
excludeSource: serializerOptions.excludeSource,
381382
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
382383
getTransformOptions: this._config.transformer.getTransformOptions,
384+
globalPrefix: this._config.transformer.globalPrefix,
383385
includeAsyncPaths: graphOptions.lazy,
384386
platform: transformOptions.platform,
385387
projectRoot: this._config.projectRoot,
@@ -1069,6 +1071,7 @@ export default class Server {
10691071
processModuleFilter: this._config.serializer.processModuleFilter,
10701072
createModuleId: this._createModuleId,
10711073
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
1074+
globalPrefix: this._config.transformer.globalPrefix,
10721075
includeAsyncPaths: graphOptions.lazy,
10731076
dev: transformOptions.dev,
10741077
projectRoot: this._config.projectRoot,

packages/metro/src/lib/getAppendScripts.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import nullthrows from 'nullthrows';
2121
type Options<T: number | string> = $ReadOnly<{
2222
asyncRequireModulePath: string,
2323
createModuleId: string => T,
24-
getRunModuleStatement: T => string,
24+
getRunModuleStatement: (moduleId: T, globalPrefix: string) => string,
25+
globalPrefix: string,
2526
inlineSourceMap: ?boolean,
2627
runBeforeMainModule: $ReadOnlyArray<string>,
2728
runModule: boolean,
@@ -46,6 +47,7 @@ export default function getAppendScripts<T: number | string>(
4647
if (modules.some((module: Module<>) => module.path === path)) {
4748
const code = options.getRunModuleStatement(
4849
options.createModuleId(path),
50+
options.globalPrefix,
4951
);
5052
output.push({
5153
path: `require-${path}`,

0 commit comments

Comments
 (0)