Skip to content

Commit 779e724

Browse files
motiz88facebook-github-bot
authored andcommittedMar 29, 2022
Respect unstable_serverRoot setting in experimentalImportBundleSupport mode
Summary: There was a bug when serving bundles in `experimentalImportBundleSupport: true` mode (D15943150 (72329d0), aka development-mode bundle splitting) and using a non-empty `unstable_serverRoot` (D30247868 (026a66c)). The split bundles would be requested with the wrong URL, using `projectRoot` rather than `unstable_serverRoot` as the base path. Here we fix this bug by adding a `serverRoot` property to the serializer's input and using it as the base path for split bundle URLs. We add `serverRoot` *alongside* `projectRoot` in order to avoid breaking custom serializers that may rely on `projectRoot`'s existence. Reviewed By: GijsWeterings Differential Revision: D35188943 fbshipit-source-id: 666943d90f5af613dbc366cf10861d7313ddd3c5
1 parent da5918d commit 779e724

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed
 

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

+47
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ it('adds lazy imports at the end of a bundle', () => {
309309
projectRoot: '/root',
310310
runBeforeMainModule: [],
311311
runModule: true,
312+
serverRoot: '/root',
312313
sourceMapUrl: 'http://localhost/bundle.map',
313314
},
314315
),
@@ -331,3 +332,49 @@ it('adds lazy imports at the end of a bundle', () => {
331332
}
332333
`);
333334
});
335+
336+
it('lazy imports are relative to serverRoot if it differs from projectRoot', () => {
337+
expect(
338+
baseJSBundle(
339+
'/root/foo',
340+
[polyfill],
341+
{
342+
dependencies: new Map([
343+
['/root/foo', fooModule],
344+
['/root/bar', barModule],
345+
]),
346+
entryPoints: ['foo'],
347+
importBundleNames: new Set(['/path/to/async/module.js']),
348+
},
349+
{
350+
asyncRequireModulePath: '/asyncRequire.js',
351+
processModuleFilter: () => true,
352+
createModuleId: filePath => path.basename(filePath),
353+
dev: true,
354+
getRunModuleStatement,
355+
projectRoot: '/root',
356+
runBeforeMainModule: [],
357+
runModule: true,
358+
serverRoot: '/',
359+
sourceMapUrl: 'http://localhost/bundle.map',
360+
},
361+
),
362+
).toMatchInlineSnapshot(`
363+
Object {
364+
"modules": Array [
365+
Array [
366+
"foo",
367+
"__d(function() {/* code for foo */},\\"foo\\",[\\"bar\\"],\\"foo\\");",
368+
],
369+
Array [
370+
"bar",
371+
"__d(function() {/* code for bar */},\\"bar\\",[],\\"bar\\");",
372+
],
373+
],
374+
"post": "(function(){var $$=require(\\"asyncRequire.js\\");$$.addImportBundleNames({\\"module.js\\":\\"path/to/async/module\\"})})();
375+
require(\\"foo\\");
376+
//# sourceMappingURL=http://localhost/bundle.map",
377+
"pre": "__d(function() {/* code for polyfill */});",
378+
}
379+
`);
380+
});

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

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function baseBytecodeBundle(
6363
projectRoot: options.projectRoot,
6464
runBeforeMainModule: options.runBeforeMainModule,
6565
runModule: options.runModule,
66+
serverRoot: options.serverRoot,
6667
sourceMapUrl: options.sourceMapUrl,
6768
sourceUrl: options.sourceUrl,
6869
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function baseJSBundle(
6565
projectRoot: options.projectRoot,
6666
runBeforeMainModule: options.runBeforeMainModule,
6767
runModule: options.runModule,
68+
serverRoot: options.serverRoot,
6869
sourceMapUrl: options.sourceMapUrl,
6970
sourceUrl: options.sourceUrl,
7071
},

‎packages/metro/src/DeltaBundler/types.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export type SerializerOptions = {|
129129
+projectRoot: string,
130130
+runBeforeMainModule: $ReadOnlyArray<string>,
131131
+runModule: boolean,
132+
+serverRoot: string,
132133
+sourceMapUrl: ?string,
133134
+sourceUrl: ?string,
134135
|};

‎packages/metro/src/Server.js

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class Server {
210210
sourceMapUrl: serializerOptions.sourceMapUrl,
211211
sourceUrl: serializerOptions.sourceUrl,
212212
inlineSourceMap: serializerOptions.inlineSourceMap,
213+
serverRoot:
214+
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
213215
};
214216
let bundleCode = null;
215217
let bundleMap = null;
@@ -285,6 +287,8 @@ class Server {
285287
sourceMapUrl: serializerOptions.sourceMapUrl,
286288
sourceUrl: serializerOptions.sourceUrl,
287289
inlineSourceMap: serializerOptions.inlineSourceMap,
290+
serverRoot:
291+
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
288292
});
289293
}
290294

@@ -754,6 +758,8 @@ class Server {
754758
sourceMapUrl: serializerOptions.sourceMapUrl,
755759
sourceUrl: serializerOptions.sourceUrl,
756760
inlineSourceMap: serializerOptions.inlineSourceMap,
761+
serverRoot:
762+
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
757763
},
758764
);
759765

@@ -862,6 +868,8 @@ class Server {
862868
sourceMapUrl: serializerOptions.sourceMapUrl,
863869
sourceUrl: serializerOptions.sourceUrl,
864870
inlineSourceMap: serializerOptions.inlineSourceMap,
871+
serverRoot:
872+
this._config.server.unstable_serverRoot ?? this._config.projectRoot,
865873
}),
866874
);
867875

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Options<T: number | string> = {
2424
+getRunModuleStatement: T => string,
2525
+inlineSourceMap: ?boolean,
2626
+projectRoot: string,
27+
+serverRoot: string,
2728
+runBeforeMainModule: $ReadOnlyArray<string>,
2829
+runModule: boolean,
2930
+sourceMapUrl: ?string,
@@ -42,7 +43,7 @@ function getAppendScripts<T: number | string>(
4243
if (importBundleNames.size) {
4344
const importBundleNamesObject = Object.create(null);
4445
importBundleNames.forEach(absolutePath => {
45-
const bundlePath = path.relative(options.projectRoot, absolutePath);
46+
const bundlePath = path.relative(options.serverRoot, absolutePath);
4647
importBundleNamesObject[options.createModuleId(absolutePath)] =
4748
bundlePath.slice(0, -path.extname(bundlePath).length);
4849
});

0 commit comments

Comments
 (0)
Please sign in to comment.