Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cannot find module error with runtimes in multiple bundles #9646

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 11 additions & 15 deletions packages/core/core/src/applyRuntimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,7 @@ export default async function applyRuntimes<TResult>({
let runtimes = await config.getRuntimes();
let connections: Array<RuntimeConnection> = [];

// As manifest bundles may be added during runtimes we process them in reverse topological
// sort order. This allows bundles to be added to their bundle groups before they are referenced
// by other bundle groups by loader runtimes
let bundles = [];
bundleGraph.traverseBundles({
exit(bundle) {
bundles.push(bundle);
},
});

let bundles = bundleGraph.getBundles({includeInline: true});
for (let bundle of bundles) {
for (let runtime of runtimes) {
let measurement;
Expand Down Expand Up @@ -172,12 +163,20 @@ export default async function applyRuntimes<TResult>({
nameRuntimeBundle(connectionBundle, bundle);
}

connections.push({
let connection = {
bundle: connectionBundle,
assetGroup,
dependency,
isEntry,
});
};

if (connectionBundle === bundle) {
connections.push(connection);
} else {
// If this is a parallel bundle, it needs to be inserted before this one
// so that its dependencies are already loaded when this bundle runs.
connections.unshift(connection);
}
}
}
} catch (e) {
Expand All @@ -192,9 +191,6 @@ export default async function applyRuntimes<TResult>({
}
}

// Correct connection order after generating runtimes in reverse order
connections.reverse();

// Add dev deps for runtime plugins AFTER running them, to account for lazy require().
for (let runtime of runtimes) {
let devDepRequest = await createDevDependency(
Expand Down
33 changes: 33 additions & 0 deletions packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
outputFS,
overlayFS,
ncp,
fsFixture,
} from '@parcel/test-utils';
import path from 'path';

Expand Down Expand Up @@ -3055,4 +3056,36 @@ describe('html', function () {

await run(b, {output: null}, {require: false});
});

it('should insert bundle manifest into the correct bundle with multiple script tags', async function () {
const dir = path.join(__dirname, 'manifest-multi-script');
overlayFS.mkdirp(dir);

await fsFixture(overlayFS, dir)`
index.html:
<body>
<script src="./polyfills.js" type="module"></script>
<script src="./main.js" type="module"></script>
</body>

polyfills.js:
import('./polyfills-async');

polyfills-async.js:
export const foo = 2;

main.js:
import('./main-async');

main-async.js:
export const bar = 3;
`;

let b = await bundle(path.join(dir, '/index.html'), {
inputFS: overlayFS,
});

// Should not error with "Cannot find module" error at runtime.
await run(b);
});
});