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

Do not merge isolated bundles in experimental bundler #8566

Merged
merged 4 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 31 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,16 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of fixes, I notice in the default bundler we maintain bundleByType, which not only holds bundles created via type change, but also all others (lazy/ parallel etc). It looks like that is how we determine where we can merge a type change bundle.
In experimental, we only attempt to merge type change with type change.... does this mean we're not merging as many bundles as we could in experimental ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not merge parallel/lazy bundles, because the transformer that created those dependencies might not expect it. For example, if you have two <script> tags in an HTML page, those have parallel priority, but merging them would result in both scripts pointing to the same URL and (possibly?) executing twice.

}
} else {
// Otherwise, merge.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my mistake, but this comment doesn't make sense here anymore since we don't merge until later

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 +552,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 +607,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 +672,9 @@ function createIdealGraph(
);
if (
bundle !== 'root' &&
bundle.bundleBehavior !== 'inline' &&
!bundle.env.isIsolated()
bundle.bundleBehavior == null &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is what caused the test change, where a bundle was previously internalized and now isn't. I think in the general case, however, that isolated bundles should not be reachable.

!bundle.env.isIsolated() &&
bundle.env.context === root.env.context
) {
bundleRootGraph.addEdge(
bundleRootGraph.getNodeIdByContentKey(root.id),
Expand All @@ -691,11 +694,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 +747,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 +780,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 +1341,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
);
}
2 changes: 2 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -5800,6 +5800,8 @@ describe('javascript', function () {
'other.js',
'esmodule-helpers.js',
'bundle-url.js',
'cacheLoader.js',
'js-loader.js',
],
},
{
Expand Down