Skip to content

Commit

Permalink
Do not merge isolated bundles in experimental bundler (#8566)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Oct 27, 2022
1 parent 96d8f28 commit 27bf596
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
54 changes: 30 additions & 24 deletions packages/bundlers/experimental/src/ExperimentalBundler.js
Expand Up @@ -511,9 +511,8 @@ function createIdealGraph(
let bundleGroupRootAsset = nullthrows(bundleGroup.mainEntryAsset);
if (
entries.has(bundleGroupRootAsset) &&
bundleGroupRootAsset.type === childAsset.type &&
childAsset.bundleBehavior !== 'inline' &&
dependency.bundleBehavior !== 'isolated'
canMerge(bundleGroupRootAsset, childAsset) &&
dependency.bundleBehavior == null
) {
bundleId = bundleGroupNodeId;
}
Expand All @@ -523,7 +522,8 @@ function createIdealGraph(
asset: childAsset,
type: childAsset.type,
env: childAsset.env,
bundleBehavior: childAsset.bundleBehavior,
bundleBehavior:
dependency.bundleBehavior ?? childAsset.bundleBehavior,
target: referencingBundle.target,
needsStableName:
childAsset.bundleBehavior === 'inline' ||
Expand All @@ -534,8 +534,15 @@ function createIdealGraph(
: referencingBundle.needsStableName,
});
bundleId = bundleGraph.addNode(bundle);

// Store Type-Change bundles for later since we need to know ALL bundlegroups they are part of to reduce/combine them
typeChangeIds.add(bundleId);
if (parentAsset.type !== childAsset.type) {
typeChangeIds.add(bundleId);
}
} else {
bundle = bundleGraph.getNode(bundleId);
invariant(bundle != null && bundle !== 'root');

if (
// If this dependency requests isolated, but the bundle is not,
// make the bundle isolated for all uses.
Expand All @@ -544,10 +551,6 @@ function createIdealGraph(
) {
bundle.bundleBehavior = dependency.bundleBehavior;
}
} else {
// Otherwise, merge.
bundle = bundleGraph.getNode(bundleId);
invariant(bundle != null && bundle !== 'root');
}

bundles.set(childAsset.id, bundleId);
Expand Down Expand Up @@ -603,9 +606,7 @@ function createIdealGraph(
b !== 'root' &&
a !== b &&
typeChangeIds.has(nodeIdB) &&
a.bundleBehavior !== 'inline' &&
b.bundleBehavior !== 'inline' &&
a.type === b.type
canMerge(a, b)
) {
let bundleBbundleGroups = getBundleGroupsForBundle(nodeIdB);
if (setEqual(bundleBbundleGroups, bundleABundleGroups)) {
Expand Down Expand Up @@ -670,8 +671,9 @@ function createIdealGraph(
);
if (
bundle !== 'root' &&
bundle.bundleBehavior !== 'inline' &&
!bundle.env.isIsolated()
bundle.bundleBehavior == null &&
!bundle.env.isIsolated() &&
bundle.env.context === root.env.context
) {
bundleRootGraph.addEdge(
bundleRootGraph.getNodeIdByContentKey(root.id),
Expand All @@ -691,11 +693,7 @@ function createIdealGraph(
}
//asset node type
let asset = node.value;
if (
asset.bundleBehavior === 'isolated' ||
asset.bundleBehavior === 'inline' ||
root.type !== asset.type
) {
if (asset.bundleBehavior != null || root.type !== asset.type) {
actions.skipChildren();
return;
}
Expand Down Expand Up @@ -748,10 +746,7 @@ function createIdealGraph(
]) {
let bundleInGroup = nullthrows(bundleGraph.getNode(bundleIdInGroup));
invariant(bundleInGroup !== 'root');
if (
bundleInGroup.bundleBehavior === 'isolated' ||
bundleInGroup.bundleBehavior === 'inline'
) {
if (bundleInGroup.bundleBehavior != null) {
continue;
}

Expand Down Expand Up @@ -784,7 +779,7 @@ function createIdealGraph(
let child = bundleRootGraph.getNode(childId);
invariant(child !== 'root' && child != null);
let bundleBehavior = getBundleFromBundleRoot(child).bundleBehavior;
if (bundleBehavior === 'isolated' || bundleBehavior === 'inline') {
if (bundleBehavior != null) {
continue;
}
let isParallel = bundleRootGraph.hasEdge(
Expand Down Expand Up @@ -1345,3 +1340,14 @@ function getEntryByTarget(
});
return targets;
}

function canMerge(a, b) {
// Bundles can be merged if they have the same type and environment,
// unless they are explicitly marked as isolated or inline.
return (
a.type === b.type &&
a.env.context === b.env.context &&
a.bundleBehavior == null &&
b.bundleBehavior == null
);
}
3 changes: 3 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -5800,6 +5800,9 @@ describe('javascript', function () {
'other.js',
'esmodule-helpers.js',
'bundle-url.js',
...(process.env.PARCEL_TEST_EXPERIMENTAL_BUNDLER
? ['cacheLoader.js', 'js-loader.js']
: []),
],
},
{
Expand Down

0 comments on commit 27bf596

Please sign in to comment.