Skip to content

Commit

Permalink
Merge branch 'v2' into detect-tsc-polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles committed Oct 18, 2023
2 parents ffc904b + bc5c715 commit 90b63b3
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 20 deletions.
74 changes: 59 additions & 15 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,14 @@ function decorateLegacyGraph(
idealBundle.sourceBundles.size > 0 &&
!idealBundle.mainEntryAsset
) {
let uniqueKey =
idealBundle.uniqueKey != null
? idealBundle.uniqueKey
: [...idealBundle.assets].map(asset => asset.id).join(',');

bundle = nullthrows(
bundleGraph.createBundle({
uniqueKey:
[...idealBundle.assets].map(asset => asset.id).join(',') +
[...idealBundle.sourceBundles].join(','),
uniqueKey,
needsStableName: idealBundle.needsStableName,
bundleBehavior: idealBundle.bundleBehavior,
type: idealBundle.type,
Expand Down Expand Up @@ -619,6 +622,7 @@ function createIdealGraph(
manualBundleToInternalizedAsset.get(bundleId),
).push(childAsset);
bundle.manualSharedBundle = manualSharedObject.name;
bundle.uniqueKey = manualSharedObject.name + childAsset.type;
}
} else {
bundle = nullthrows(bundleGraph.getNode(bundleId));
Expand Down Expand Up @@ -657,6 +661,7 @@ function createIdealGraph(
manualSharedMap.set(manualSharedBundleKey, bundleId);
}
bundle.manualSharedBundle = manualSharedObject.name;
bundle.uniqueKey = manualSharedObject.name + childAsset.type;
}
}

Expand Down Expand Up @@ -766,6 +771,7 @@ function createIdealGraph(
manualSharedMap.set(manualSharedBundleKey, bundleId);
}
bundle.manualSharedBundle = manualSharedObject.name;
bundle.uniqueKey = manualSharedObject.name + childAsset.type;
}
} else {
bundle = bundleGraph.getNode(bundleId);
Expand Down Expand Up @@ -795,6 +801,7 @@ function createIdealGraph(
manualSharedMap.set(manualSharedBundleKey, bundleId);
}
bundle.manualSharedBundle = manualSharedObject.name;
bundle.uniqueKey = manualSharedObject.name + childAsset.type;
}

bundles.set(childAsset.id, bundleId);
Expand Down Expand Up @@ -935,6 +942,8 @@ function createIdealGraph(
// order the bundles are loaded.
let ancestorAssets = [];

let inlineConstantDeps = new DefaultMap(() => new Set());

for (let [bundleRootId, assetId] of bundleRootGraph.nodes.entries()) {
let reachable = new BitSet(assets.length);
reachableAssets.push(reachable);
Expand Down Expand Up @@ -994,6 +1003,18 @@ function createIdealGraph(
let assetIndex = nullthrows(assetToIndex.get(node.value));
reachable.add(assetIndex);
reachableRoots[assetIndex].add(bundleRootId);

if (asset.meta.isConstantModule === true) {
let parents = assetGraph
.getIncomingDependencies(asset)
.map(dep => nullthrows(assetGraph.getAssetWithDependency(dep)));

for (let parent of parents) {
inlineConstantDeps.get(parent).add(asset);
}
}

return;
},
root,
{skipUnusedDependencies: true},
Expand Down Expand Up @@ -1143,6 +1164,16 @@ function createIdealGraph(
deleteBundle(bundleRoot);
}
}

function assignInlineConstants(parentAsset: Asset, bundle: Bundle) {
for (let inlineConstant of inlineConstantDeps.get(parentAsset)) {
if (!bundle.assets.has(inlineConstant)) {
bundle.assets.add(inlineConstant);
bundle.size += inlineConstant.stats.size;
}
}
}

// Step Insert Or Share: Place all assets into bundles or create shared bundles. Each asset
// is placed into a single bundle based on the bundle entries it is reachable from.
// This creates a maximally code split bundle graph with no duplication.
Expand All @@ -1153,19 +1184,15 @@ function createIdealGraph(
let asset = assets[i];
let manualSharedObject = manualAssetToConfig.get(asset);

if (asset.meta.isConstantModule === true) {
// Add assets to non-splittable bundles.
reachableRoots[i].forEach(nodeId => {
let assetId = bundleRootGraph.getNode(nodeId);
if (assetId == null) return; // deleted
let entry = assets[assetId];
let entryBundleId = nullthrows(bundleRoots.get(entry))[0];
let entryBundle = nullthrows(bundleGraph.getNode(entryBundleId));
invariant(entryBundle !== 'root');
entryBundle.assets.add(asset);
entryBundle.size += asset.stats.size;
});
if (bundleRoots.has(asset) && inlineConstantDeps.get(asset).size > 0) {
let entryBundleId = nullthrows(bundleRoots.get(asset))[0];
let entryBundle = nullthrows(bundleGraph.getNode(entryBundleId));
invariant(entryBundle !== 'root');
assignInlineConstants(asset, entryBundle);
}

if (asset.meta.isConstantModule === true) {
// Ignore constant modules as they are placed with their direct parents
continue;
}

Expand All @@ -1191,6 +1218,8 @@ function createIdealGraph(
invariant(entryBundle !== 'root');
entryBundle.assets.add(asset);
entryBundle.size += asset.stats.size;

assignInlineConstants(asset, entryBundle);
} else if (!ancestorAssets[nodeId]?.has(i)) {
// Filter out bundles from this asset's reachable array if
// bundle does not contain the asset in its ancestry
Expand All @@ -1217,6 +1246,7 @@ function createIdealGraph(
invariant(firstSourceBundle !== 'root');

bundle = createBundle({
uniqueKey: manualSharedObject.name + firstSourceBundle.type,
target: firstSourceBundle.target,
type: firstSourceBundle.type,
env: firstSourceBundle.env,
Expand Down Expand Up @@ -1365,6 +1395,8 @@ function createIdealGraph(
bundle.assets.add(asset);
bundle.size += asset.stats.size;

assignInlineConstants(asset, bundle);

for (let sourceBundleId of sourceBundles) {
if (bundleId !== sourceBundleId) {
bundleGraph.addEdge(sourceBundleId, bundleId);
Expand All @@ -1386,6 +1418,8 @@ function createIdealGraph(
invariant(bundle !== 'root');
bundle.assets.add(asset);
bundle.size += asset.stats.size;

assignInlineConstants(asset, bundle);
}
}
}
Expand Down Expand Up @@ -1417,6 +1451,7 @@ function createIdealGraph(

for (let i = 1; i < [...remainderMap.keys()].length; i++) {
let bundle = createBundle({
uniqueKey: manualSharedObject.name + firstSourceBundle.type + i,
target: firstSourceBundle.target,
type: firstSourceBundle.type,
env: firstSourceBundle.env,
Expand Down Expand Up @@ -1911,6 +1946,15 @@ async function loadBundlerConfig(
});
}

if (modeConfig.unstable_manualSharedBundles) {
let nameArray = modeConfig.unstable_manualSharedBundles.map(a => a.name);
let nameSet = new Set(nameArray);
invariant(
nameSet.size == nameArray.length,
'The name field must be unique for property unstable_manualSharedBundles',
);
}

validateSchema.diagnostic(
CONFIG_SCHEMA,
{
Expand Down

0 comments on commit 90b63b3

Please sign in to comment.