Skip to content

Commit

Permalink
ScopeHoistingPackager: Handle different wrapped ancestries in wrappin…
Browse files Browse the repository at this point in the history
…g dfs
  • Loading branch information
Will Binns-Smith committed Mar 31, 2022
1 parent eda0627 commit 53f296a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
9 changes: 7 additions & 2 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,12 @@ export default class BundleGraph {
traverseAssets<TContext>(
bundle: Bundle,
visit: GraphVisitor<Asset, TContext>,
startAsset?: Asset,
): ?TContext {
return this.traverseBundle(
bundle,
mapVisitor(node => (node.type === 'asset' ? node.value : null), visit),
startAsset,
);
}

Expand Down Expand Up @@ -1057,8 +1059,9 @@ export default class BundleGraph {
traverseBundle<TContext>(
bundle: Bundle,
visit: GraphVisitor<AssetNode | DependencyNode, TContext>,
startAsset?: Asset,
): ?TContext {
let entries = true;
let entries = !startAsset;
let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);

// A modified DFS traversal which traverses entry assets in the same order
Expand All @@ -1085,7 +1088,9 @@ export default class BundleGraph {

actions.skipChildren();
}, visit),
startNodeId: bundleNodeId,
startNodeId: startAsset
? this._graph.getNodeIdByContentKey(startAsset.id)
: bundleNodeId,
getChildren: nodeId => {
let children = this._graph
.getNodeIdsConnectedFrom(nodeId)
Expand Down
6 changes: 5 additions & 1 deletion packages/core/core/src/public/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ export class Bundle implements IBundle {
);
}

traverseAssets<TContext>(visit: GraphVisitor<IAsset, TContext>): ?TContext {
traverseAssets<TContext>(
visit: GraphVisitor<IAsset, TContext>,
startAsset?: IAsset,
): ?TContext {
return this.#bundleGraph.traverseAssets(
this.#bundle,
mapVisitor(asset => assetFromValue(asset, this.#options), visit),
startAsset ? assetToAssetValue(startAsset) : undefined,
);
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,10 @@ export interface Bundle {
/** Returns whether the bundle includes the given dependency. */
hasDependency(Dependency): boolean;
/** Traverses the assets in the bundle. */
traverseAssets<TContext>(visit: GraphVisitor<Asset, TContext>): ?TContext;
traverseAssets<TContext>(
visit: GraphVisitor<Asset, TContext>,
startAsset?: Asset,
): ?TContext;
/** Traverses assets and dependencies in the bundle. */
traverse<TContext>(
visit: GraphVisitor<BundleTraversable, TContext>,
Expand Down
16 changes: 13 additions & 3 deletions packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class ScopeHoistingPackager {
async loadAssets(): Promise<Array<Asset>> {
let queue = new PromiseQueue({maxConcurrent: 32});
let wrapped = [];
this.bundle.traverseAssets((asset, shouldWrap) => {
this.bundle.traverseAssets(asset => {
queue.add(async () => {
let [code, map] = await Promise.all([
asset.getCode(),
Expand All @@ -251,7 +251,6 @@ export class ScopeHoistingPackager {
});

if (
shouldWrap ||
asset.meta.shouldWrap ||
this.isAsyncBundle ||
this.bundle.env.sourceType === 'script' ||
Expand All @@ -262,10 +261,21 @@ export class ScopeHoistingPackager {
) {
this.wrappedAssets.add(asset.id);
wrapped.push(asset);
return true;
}
});

for (let wrappedAsset of [...wrapped]) {
this.bundle.traverseAssets((asset, _, actions) => {
if (asset !== wrappedAsset && this.wrappedAssets.has(asset.id)) {
actions.skipChildren();
return;
}

this.wrappedAssets.add(asset.id);
wrapped.push(asset);
}, wrappedAsset);
}

this.assetOutputs = new Map(await queue.run());
return wrapped;
}
Expand Down

0 comments on commit 53f296a

Please sign in to comment.