Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ This option only has an effect under the default [`transformerPath`](#transforme

#### `getRunModuleStatement`

Type: `(number | string) => string`
Type: `(moduleId: number | string, globalPrefix: string) => string`

Specify the format of the initial require statements that are appended at the end of the bundle. By default is `__r(${moduleId});`.

Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({

serializer: {
polyfillModuleNames: [],
getRunModuleStatement: (moduleId: number | string) =>
getRunModuleStatement: (moduleId: number | string, globalPrefix: string) =>
`__r(${JSON.stringify(moduleId)});`,
getPolyfills: () => [],
getModulesRunBeforeMainModule: () => [],
Expand Down
5 changes: 4 additions & 1 deletion packages/metro-config/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ type SerializerConfigT = {
) => mixed,
getModulesRunBeforeMainModule: (entryFilePath: string) => Array<string>,
getPolyfills: ({platform: ?string, ...}) => $ReadOnlyArray<string>,
getRunModuleStatement: (number | string) => string,
getRunModuleStatement: (
moduleId: number | string,
globalPrefix: string,
) => string,
polyfillModuleNames: $ReadOnlyArray<string>,
processModuleFilter: (modules: Module<>) => boolean,
isThirdPartyModule: (module: $ReadOnly<{path: string, ...}>) => boolean,
Expand Down
5 changes: 4 additions & 1 deletion packages/metro-config/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export interface SerializerConfigT {
) => unknown;
getModulesRunBeforeMainModule: (entryFilePath: string) => string[];
getPolyfills: (options: {platform: string | null}) => ReadonlyArray<string>;
getRunModuleStatement: (moduleId: number | string) => string;
getRunModuleStatement: (
moduleId: number | string,
globalPrefix: string,
) => string;
polyfillModuleNames: ReadonlyArray<string>;
processModuleFilter: (modules: Module) => boolean;
isThirdPartyModule: (module: {readonly path: string}) => boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ const nonAsciiModule: Module<> = {
getSource: () => Buffer.from('bar-source'),
};

const getRunModuleStatement = (moduleId: number | string) =>
`require(${JSON.stringify(moduleId)});`;
const getRunModuleStatement = jest.fn(
(moduleId: number | string, globalPrefix: string) =>
`require(${JSON.stringify(moduleId)});`,
);

const transformOptions: TransformInputOptions = {
customTransformOptions: {},
Expand All @@ -107,6 +109,10 @@ const transformOptions: TransformInputOptions = {
unstable_transformProfile: 'default',
};

beforeEach(() => {
jest.clearAllMocks();
});

test('should generate a very simple bundle', () => {
expect(
baseJSBundle(
Expand All @@ -126,6 +132,7 @@ test('should generate a very simple bundle', () => {
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: 'customPrefix',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -157,6 +164,8 @@ test('should generate a very simple bundle', () => {
"pre": "__d(function() {/* code for polyfill */});",
}
`);

expect(getRunModuleStatement).toHaveBeenCalledWith('foo', 'customPrefix');
});

test('should generate a bundle with correct non ascii characters parsing', () => {
Expand All @@ -177,6 +186,7 @@ test('should generate a bundle with correct non ascii characters parsing', () =>
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -235,6 +245,7 @@ test('should add runBeforeMainModule statements if found in the graph', () => {
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -274,6 +285,7 @@ test('should handle numeric module ids', () => {
createModuleId: createModuleIdFactory(),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -322,6 +334,7 @@ test('outputs custom runModule statements', () => {
dev: true,
getRunModuleStatement: moduleId =>
`export default require(${JSON.stringify(moduleId)}).default;`,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -360,6 +373,7 @@ test('should add an inline source map to a very simple bundle', () => {
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: true,
modulesOnly: false,
Expand Down Expand Up @@ -411,6 +425,7 @@ test('emits x_google_ignoreList based on shouldAddToIgnoreList', () => {
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: true,
modulesOnly: false,
Expand Down Expand Up @@ -462,6 +477,7 @@ test('does not add polyfills when `modulesOnly` is used', () => {
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ test('should return the RAM bundle info', async () => {
preloadedModules: {},
ramGroups: [],
}),
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -129,6 +130,7 @@ test('emits x_google_ignoreList based on shouldAddToIgnoreList', async () => {
preloadedModules: {},
ramGroups: [],
}),
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
Expand Down Expand Up @@ -167,6 +169,7 @@ test('should use the preloadedModules and ramGroup configs to build a RAM bundle
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
* https://fburl.com/workplace/6291gfvu */
getTransformOptions,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: null,
modulesOnly: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function baseJSBundle(
asyncRequireModulePath: options.asyncRequireModulePath,
createModuleId: options.createModuleId,
getRunModuleStatement: options.getRunModuleStatement,
globalPrefix: options.globalPrefix,
inlineSourceMap: options.inlineSourceMap,
runBeforeMainModule: options.runBeforeMainModule,
runModule: options.runModule,
Expand Down
6 changes: 5 additions & 1 deletion packages/metro/src/DeltaBundler/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ export type SerializerOptions = $ReadOnly<{
asyncRequireModulePath: string,
createModuleId: string => number,
dev: boolean,
getRunModuleStatement: (number | string) => string,
getRunModuleStatement: (
moduleId: number | string,
globalPrefix: string,
) => string,
globalPrefix: string,
includeAsyncPaths: boolean,
inlineSourceMap: ?boolean,
modulesOnly: boolean,
Expand Down
3 changes: 3 additions & 0 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default class Server {
processModuleFilter: this._config.serializer.processModuleFilter,
createModuleId: this._createModuleId,
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
globalPrefix: this._config.transformer.globalPrefix,
dev: transformOptions.dev,
includeAsyncPaths: graphOptions.lazy,
projectRoot: this._config.projectRoot,
Expand Down Expand Up @@ -380,6 +381,7 @@ export default class Server {
excludeSource: serializerOptions.excludeSource,
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
getTransformOptions: this._config.transformer.getTransformOptions,
globalPrefix: this._config.transformer.globalPrefix,
includeAsyncPaths: graphOptions.lazy,
platform: transformOptions.platform,
projectRoot: this._config.projectRoot,
Expand Down Expand Up @@ -1069,6 +1071,7 @@ export default class Server {
processModuleFilter: this._config.serializer.processModuleFilter,
createModuleId: this._createModuleId,
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
globalPrefix: this._config.transformer.globalPrefix,
includeAsyncPaths: graphOptions.lazy,
dev: transformOptions.dev,
projectRoot: this._config.projectRoot,
Expand Down
4 changes: 3 additions & 1 deletion packages/metro/src/lib/getAppendScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import nullthrows from 'nullthrows';
type Options<T: number | string> = $ReadOnly<{
asyncRequireModulePath: string,
createModuleId: string => T,
getRunModuleStatement: T => string,
getRunModuleStatement: (moduleId: T, globalPrefix: string) => string,
globalPrefix: string,
inlineSourceMap: ?boolean,
runBeforeMainModule: $ReadOnlyArray<string>,
runModule: boolean,
Expand All @@ -46,6 +47,7 @@ export default function getAppendScripts<T: number | string>(
if (modules.some((module: Module<>) => module.path === path)) {
const code = options.getRunModuleStatement(
options.createModuleId(path),
options.globalPrefix,
);
output.push({
path: `require-${path}`,
Expand Down
5 changes: 4 additions & 1 deletion packages/metro/types/DeltaBundler/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ export interface SerializerOptions<T = MixedOutput> {
readonly asyncRequireModulePath: string;
readonly createModuleId: (filePath: string) => number;
readonly dev: boolean;
readonly getRunModuleStatement: (moduleId: string | number) => string;
readonly getRunModuleStatement: (
moduleId: number | string,
globalPrefix: string,
) => string;
readonly includeAsyncPaths: boolean;
readonly inlineSourceMap?: boolean;
readonly modulesOnly: boolean;
Expand Down
Loading