Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/metro/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export type SerializerOptions = Readonly<{
createModuleId: (absolutePath: string) => number;
dependencyMapReservedName?: null | undefined | string;
dev: boolean;
excludeSource?: null | undefined | boolean;
getRunModuleStatement: (moduleId: number | string, globalPrefix: string) => string;
globalPrefix: string;
includeAsyncPaths: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,54 @@ test('applies processModuleFilter to the inline source map', () => {
]);
});

test.each([
[undefined, ['foo-source', 'bar-source']],
[false, ['foo-source', 'bar-source']],
[true, [undefined, undefined]],
])(
'inline source map respects excludeSource: %s',
(excludeSource, expectedSourcesContent) => {
const bundle = baseJSBundle(
'/root/foo',
[],
{
dependencies: new Map([
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
asyncRequireModulePath: '',
createModuleId: createModuleIdFactory(),
dev: true,
excludeSource,
getRunModuleStatement,
globalPrefix: '',
includeAsyncPaths: false,
inlineSourceMap: true,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
reservedString: '_$$_METRO_',
runBeforeMainModule: [],
runModule: true,
serverRoot: '/root',
shouldAddToIgnoreList: () => false,
sourceMapUrl: null,
sourceUrl: null,
getSourceUrl: null,
},
);
const base64 = bundle.post.slice(bundle.post.lastIndexOf('base64') + 7);
const map = JSON.parse(Buffer.from(base64, 'base64').toString());
expect(
map.sections.map(section => section.map.sourcesContent?.[0]),
).toEqual(expectedSourcesContent);
},
);

test('emits x_google_ignoreList based on shouldAddToIgnoreList', () => {
const bundle = baseJSBundle(
'/root/foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function baseJSBundle(
getAppendScripts(entryPoint, [...preModules, ...modules], {
asyncRequireModulePath: options.asyncRequireModulePath,
createModuleId: options.createModuleId,
excludeSource: options.excludeSource,
getRunModuleStatement: options.getRunModuleStatement,
globalPrefix: options.globalPrefix,
inlineSourceMap: options.inlineSourceMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import {appendLazyModuleSwitch} from './helpers/lazyModuleSwitch';
import processModules from './helpers/processModules';
import {BundleBuilder, fromRawMappings} from 'metro-source-map';

type Options = Readonly<{
...SerializerOptions,
excludeSource?: boolean,
}>;

/**
* Serializes a bundle in which the graph's modules are placed inside a single
* segment definer -- `__registerSegment(0, function (moduleId) { switch (...) })`
Expand All @@ -50,7 +45,7 @@ export default function baseJSBundleWithLazyModuleDefinition(
entryPoint: string,
preModules: ReadonlyArray<Module<>>,
graph: ReadOnlyGraph<>,
options: Options,
options: SerializerOptions,
): {code: string, map: string} {
// Assign ids up front so ordering and dependency-map ids are stable, matching
// `baseJSBundle`.
Expand Down Expand Up @@ -132,6 +127,7 @@ export default function baseJSBundleWithLazyModuleDefinition(
getAppendScripts(entryPoint, [...preModules, ...modules], {
asyncRequireModulePath: options.asyncRequireModulePath,
createModuleId: options.createModuleId,
excludeSource: options.excludeSource,
getRunModuleStatement: options.getRunModuleStatement,
globalPrefix: options.globalPrefix,
inlineSourceMap: options.inlineSourceMap,
Expand Down
1 change: 1 addition & 0 deletions packages/metro/src/DeltaBundler/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export type SerializerOptions = Readonly<{
createModuleId: (absolutePath: string) => number,
dependencyMapReservedName?: ?string,
dev: boolean,
excludeSource?: ?boolean,
getRunModuleStatement: (
moduleId: number | string,
globalPrefix: string,
Expand Down
2 changes: 2 additions & 0 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export default class Server {
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
globalPrefix: this._config.transformer.globalPrefix,
dev: transformOptions.dev,
excludeSource: serializerOptions.excludeSource,
includeAsyncPaths: graphOptions.lazy,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
Expand Down Expand Up @@ -1149,6 +1150,7 @@ export default class Server {
globalPrefix: this._config.transformer.globalPrefix,
includeAsyncPaths: graphOptions.lazy,
dev: transformOptions.dev,
excludeSource: serializerOptions.excludeSource,
projectRoot: this._config.projectRoot,
modulesOnly: serializerOptions.modulesOnly,
reservedString: RESERVED_STRING,
Expand Down
77 changes: 77 additions & 0 deletions packages/metro/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,55 @@ describe('processRequest', () => {
});
});

test.each([
['', ['code-require', 'code-mybundle', 'code-foo']],
['&excludeSource=false', ['code-require', 'code-mybundle', 'code-foo']],
['&excludeSource=true', [undefined, undefined, undefined]],
])(
'inline source map respects `excludeSource` (query: %j)',
async (query, expectedSourcesContent) => {
const response = await makeRequest(
`mybundle.bundle?inlineSourceMap=true${query}`,
);
const code: string = response._getString();
const base64 = code.slice(
code.indexOf('base64,') + 7,
code.indexOf('\n', code.indexOf('base64,')),
);
const map = JSON.parse(Buffer.from(base64, 'base64').toString());

expect(
map.sections.map(section => section.map.sourcesContent?.[0]),
).toEqual(expectedSourcesContent);
},
);

test.each([
['mybundle.bundle', false],
['mybundle.bundle?excludeSource=true', true],
])(
'passes `excludeSource` to a custom serializer (request: %s)',
async (requestUrl, expectedExcludeSource) => {
const customSerializer = jest.fn(
async (..._args: ReadonlyArray<unknown>) => 'code',
);
server = new Server(
mergeConfig(config, {
serializer: {customSerializer},
} as InputConfigT),
);

await makeRequest(requestUrl);

expect(customSerializer).toHaveBeenCalledWith(
expect.any(String),
expect.any(Array),
expect.any(Object),
expect.objectContaining({excludeSource: expectedExcludeSource}),
);
},
);

test('does not rebuild the graph when requesting the sourcemaps after having requested the same bundle', async () => {
expect((await makeRequest('mybundle.bundle?platform=ios')).statusCode).toBe(
200,
Expand Down Expand Up @@ -1133,6 +1182,34 @@ describe('processRequest', () => {
});

describe('build(options)', () => {
test.each([[false], [true]])(
'passes `excludeSource: %s` to a custom serializer',
async excludeSource => {
const customSerializer = jest.fn(
async (..._args: ReadonlyArray<unknown>) => 'code',
);
server = new Server(
mergeConfig(config, {
serializer: {customSerializer},
} as InputConfigT),
);

await server.build({
...Server.DEFAULT_BUNDLE_OPTIONS,
entryFile: 'mybundle.js',
excludeSource,
platform: undefined,
});

expect(customSerializer).toHaveBeenCalledWith(
expect.any(String),
expect.any(Array),
expect.any(Object),
expect.objectContaining({excludeSource}),
);
},
);

test('Calls the delta bundler with the correct args', async () => {
await server.build({
...Server.DEFAULT_BUNDLE_OPTIONS,
Expand Down
3 changes: 2 additions & 1 deletion packages/metro/src/lib/getAppendScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import nullthrows from 'nullthrows';
type Options<T extends number | string> = Readonly<{
asyncRequireModulePath: string,
createModuleId: (absolutePath: string) => T,
excludeSource?: ?boolean,
getRunModuleStatement: (moduleId: T, globalPrefix: string) => string,
globalPrefix: string,
inlineSourceMap: ?boolean,
Expand Down Expand Up @@ -75,7 +76,7 @@ export default function getAppendScripts<T extends number | string>(
? getInlineSourceMappingURL(
sourceMapString(modules, {
processModuleFilter: options.processModuleFilter,
excludeSource: false,
excludeSource: options.excludeSource === true,
shouldAddToIgnoreList: options.shouldAddToIgnoreList,
getSourceUrl: options.getSourceUrl,
}),
Expand Down
Loading