Skip to content

Commit 492872b

Browse files
kwy404robhogan
andauthored
Apply processModuleFilter to inline source maps (#2124)
* Apply `processModuleFilter` to inline source maps getAppendScripts built the inline source map with a filter that accepts every module, while the bundle code and the external source map apply `serializer.processModuleFilter`. Filtered modules still took up lines in the inline map, so every mapping after them was shifted. * Default processModuleFilter to include all modules in getAppendScripts * Require processModuleFilter in getAppendScripts --------- Co-authored-by: Rob Hogan <2590098+robhogan@users.noreply.github.com>
1 parent 0ab227f commit 492872b

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,49 @@ test('should add an inline source map to a very simple bundle', () => {
424424
});
425425
});
426426

427+
test('applies processModuleFilter to the inline source map', () => {
428+
const bundle = baseJSBundle(
429+
'/root/foo',
430+
[polyfill],
431+
{
432+
dependencies: new Map([
433+
['/root/foo', fooModule],
434+
['/root/bar', barModule],
435+
]),
436+
entryPoints: new Set(['/root/foo']),
437+
transformOptions,
438+
},
439+
{
440+
asyncRequireModulePath: '',
441+
createModuleId: createModuleIdFactory(),
442+
dev: true,
443+
getRunModuleStatement,
444+
globalPrefix: '',
445+
includeAsyncPaths: false,
446+
inlineSourceMap: true,
447+
modulesOnly: false,
448+
processModuleFilter: module => module.path !== '/polyfill',
449+
projectRoot: '/root',
450+
runBeforeMainModule: [],
451+
runModule: true,
452+
serverRoot: '/root',
453+
shouldAddToIgnoreList: () => false,
454+
sourceMapUrl: null,
455+
sourceUrl: null,
456+
getSourceUrl: null,
457+
},
458+
);
459+
// Without the polyfill, foo and bar are the first two lines of the bundle.
460+
const base64 = bundle.post.slice(bundle.post.lastIndexOf('base64') + 7);
461+
const map = JSON.parse(Buffer.from(base64, 'base64').toString());
462+
expect(
463+
map.sections.map(section => [section.offset.line, section.map.sources[0]]),
464+
).toEqual([
465+
[0, '/root/foo'],
466+
[1, '/root/bar'],
467+
]);
468+
});
469+
427470
test('emits x_google_ignoreList based on shouldAddToIgnoreList', () => {
428471
const bundle = baseJSBundle(
429472
'/root/foo',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export default function baseJSBundle(
6464
getRunModuleStatement: options.getRunModuleStatement,
6565
globalPrefix: options.globalPrefix,
6666
inlineSourceMap: options.inlineSourceMap,
67+
processModuleFilter: options.processModuleFilter,
6768
runBeforeMainModule: options.runBeforeMainModule,
6869
runModule: options.runModule,
6970
shouldAddToIgnoreList: options.shouldAddToIgnoreList,

‎packages/metro/src/DeltaBundler/Serializers/baseJSBundleWithLazyModuleDefinition.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export default function baseJSBundleWithLazyModuleDefinition(
132132
getRunModuleStatement: options.getRunModuleStatement,
133133
globalPrefix: options.globalPrefix,
134134
inlineSourceMap: options.inlineSourceMap,
135+
processModuleFilter: options.processModuleFilter,
135136
runBeforeMainModule: options.runBeforeMainModule,
136137
runModule: options.runModule,
137138
shouldAddToIgnoreList: options.shouldAddToIgnoreList,

‎packages/metro/src/lib/getAppendScripts.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Options<T extends number | string> = Readonly<{
2424
getRunModuleStatement: (moduleId: T, globalPrefix: string) => string,
2525
globalPrefix: string,
2626
inlineSourceMap: ?boolean,
27+
processModuleFilter: (module: Module<>) => boolean,
2728
runBeforeMainModule: ReadonlyArray<string>,
2829
runModule: boolean,
2930
shouldAddToIgnoreList: (Module<>) => boolean,
@@ -73,7 +74,7 @@ export default function getAppendScripts<T extends number | string>(
7374
const sourceMappingURL = options.inlineSourceMap
7475
? getInlineSourceMappingURL(
7576
sourceMapString(modules, {
76-
processModuleFilter: (): boolean => true,
77+
processModuleFilter: options.processModuleFilter,
7778
excludeSource: false,
7879
shouldAddToIgnoreList: options.shouldAddToIgnoreList,
7980
getSourceUrl: options.getSourceUrl,

0 commit comments

Comments
 (0)