Skip to content

Commit 882d395

Browse files
committed
Fix cannot find module runtime error with runtimes in multiple bundles
1 parent 179f63a commit 882d395

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

packages/core/core/src/applyRuntimes.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,7 @@ export default async function applyRuntimes<TResult>({
8484
let runtimes = await config.getRuntimes();
8585
let connections: Array<RuntimeConnection> = [];
8686

87-
// As manifest bundles may be added during runtimes we process them in reverse topological
88-
// sort order. This allows bundles to be added to their bundle groups before they are referenced
89-
// by other bundle groups by loader runtimes
90-
let bundles = [];
91-
bundleGraph.traverseBundles({
92-
exit(bundle) {
93-
bundles.push(bundle);
94-
},
95-
});
96-
87+
let bundles = bundleGraph.getBundles({includeInline: true});
9788
for (let bundle of bundles) {
9889
for (let runtime of runtimes) {
9990
let measurement;
@@ -172,12 +163,20 @@ export default async function applyRuntimes<TResult>({
172163
nameRuntimeBundle(connectionBundle, bundle);
173164
}
174165

175-
connections.push({
166+
let connection = {
176167
bundle: connectionBundle,
177168
assetGroup,
178169
dependency,
179170
isEntry,
180-
});
171+
};
172+
173+
if (connectionBundle === bundle) {
174+
connections.push(connection);
175+
} else {
176+
// If this is a parallel bundle, it needs to be inserted before this one
177+
// so that its dependencies are already loaded when this bundle runs.
178+
connections.unshift(connection);
179+
}
181180
}
182181
}
183182
} catch (e) {
@@ -192,9 +191,6 @@ export default async function applyRuntimes<TResult>({
192191
}
193192
}
194193

195-
// Correct connection order after generating runtimes in reverse order
196-
connections.reverse();
197-
198194
// Add dev deps for runtime plugins AFTER running them, to account for lazy require().
199195
for (let runtime of runtimes) {
200196
let devDepRequest = await createDevDependency(

packages/core/integration-tests/test/html.js

+33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
outputFS,
1212
overlayFS,
1313
ncp,
14+
fsFixture,
1415
} from '@parcel/test-utils';
1516
import path from 'path';
1617

@@ -3055,4 +3056,36 @@ describe('html', function () {
30553056

30563057
await run(b, {output: null}, {require: false});
30573058
});
3059+
3060+
it('should insert bundle manifest into the correct bundle with multiple script tags', async function () {
3061+
const dir = path.join(__dirname, 'manifest-multi-script');
3062+
overlayFS.mkdirp(dir);
3063+
3064+
await fsFixture(overlayFS, dir)`
3065+
index.html:
3066+
<body>
3067+
<script src="./polyfills.js" type="module"></script>
3068+
<script src="./main.js" type="module"></script>
3069+
</body>
3070+
3071+
polyfills.js:
3072+
import('./polyfills-async');
3073+
3074+
polyfills-async.js:
3075+
export const foo = 2;
3076+
3077+
main.js:
3078+
import('./main-async');
3079+
3080+
main-async.js:
3081+
export const bar = 3;
3082+
`;
3083+
3084+
let b = await bundle(path.join(dir, '/index.html'), {
3085+
inputFS: overlayFS,
3086+
});
3087+
3088+
// Should not error with "Cannot find module" error at runtime.
3089+
await run(b);
3090+
});
30583091
});

0 commit comments

Comments
 (0)