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

Source Bundles Property for Manual Bundles, loadedBy #9294

Open
wants to merge 14 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 107 additions & 60 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type ManualSharedBundles = Array<{|
name: string,
assets: Array<Glob>,
types?: Array<string>,
parent?: string,
root?: string,
requestedBy?: Array<string>,
split?: number,
|}>;

Expand Down Expand Up @@ -429,30 +430,48 @@ function createIdealGraph(
function makeManualAssetToConfigLookup() {
let manualAssetToConfig = new Map();
let constantModuleToMSB = new DefaultMap(() => []);

let configTorequestByAsset = new DefaultMap(() => []);
if (config.manualSharedBundles.length === 0) {
return {manualAssetToConfig, constantModuleToMSB};
return {manualAssetToConfig, constantModuleToMSB, configTorequestByAsset};
}

let parentsToConfig = new DefaultMap(() => []);
let requestByToConfig = new DefaultMap(() => []);

for (let c of config.manualSharedBundles) {
if (c.parent != null) {
parentsToConfig.get(path.join(config.projectRoot, c.parent)).push(c);
if (c.root != null) {
parentsToConfig.get(path.join(config.projectRoot, c.root)).push(c);
}
if (c.requestedBy != null) {
for (let a of c.requestedBy) {
requestByToConfig.get(path.join(config.projectRoot, a)).push(c);
}
}
}
let numParentsToFind = parentsToConfig.size;
let numrequestBysToFind = requestByToConfig.size;
let configToParentAsset = new Map();

assetGraph.traverse((node, _, actions) => {
if (node.type === 'asset' && parentsToConfig.has(node.value.filePath)) {
for (let c of parentsToConfig.get(node.value.filePath)) {
configToParentAsset.set(c, node.value);
if (
node.type === 'asset' &&
(parentsToConfig.has(node.value.filePath) ||
requestByToConfig.has(node.value.filePath))
) {
if (parentsToConfig.has(node.value.filePath)) {
for (let c of parentsToConfig.get(node.value.filePath)) {
configToParentAsset.set(c, node.value);
numParentsToFind--;
}
}
if (requestByToConfig.has(node.value.filePath)) {
for (let c of requestByToConfig.get(node.value.filePath)) {
configTorequestByAsset.get(c).push(node.value);
numrequestBysToFind--;
}
}

numParentsToFind--;

if (numParentsToFind === 0) {
if (numParentsToFind === 0 && numrequestBysToFind === 0) {
// If we've found all parents we can stop traversal
actions.stop();
}
Expand All @@ -462,7 +481,7 @@ function createIdealGraph(
// Process in reverse order so earlier configs take precedence
for (let c of config.manualSharedBundles.reverse()) {
invariant(
c.parent == null || configToParentAsset.has(c),
c.root == null || configToParentAsset.has(c),
'Invalid manual shared bundle. Could not find parent asset.',
);

Expand Down Expand Up @@ -502,15 +521,15 @@ function createIdealGraph(
}, parentAsset);
}

return {manualAssetToConfig, constantModuleToMSB};
return {manualAssetToConfig, constantModuleToMSB, configTorequestByAsset};
}

//Manual is a map of the user-given name to the bundle node Id that corresponds to ALL the assets that match any glob in that user-specified array
let manualSharedMap: Map<string, NodeId> = new Map();
// May need a map to be able to look up NON- bundle root assets which need special case instructions
// Use this when placing assets into bundles, to avoid duplication
let manualAssetToBundle: Map<Asset, NodeId> = new Map();
let {manualAssetToConfig, constantModuleToMSB} =
let {manualAssetToConfig, constantModuleToMSB, configTorequestByAsset} =
makeManualAssetToConfigLookup();
let manualBundleToInternalizedAsset: DefaultMap<
NodeId,
Expand Down Expand Up @@ -1123,6 +1142,7 @@ function createIdealGraph(
let reachable = new BitSet(assets.length);
let reachableNonEntries = new BitSet(assets.length);
let reachableIntersection = new BitSet(assets.length);
let manualReachable = new BitSet(assets.length);
for (let i = 0; i < assets.length; i++) {
let asset = assets[i];
let manualSharedObject = manualAssetToConfig.get(asset);
Expand Down Expand Up @@ -1174,62 +1194,83 @@ function createIdealGraph(

// If we encounter a "manual" asset, draw an edge from reachable to its MSB
if (manualSharedObject && !reachable.empty()) {
let bundle;
let bundleId;
let manualSharedBundleKey = manualSharedObject.name + ',' + asset.type;
let sourceBundles = [];
reachable.forEach(id => {
sourceBundles.push(nullthrows(bundleRoots.get(assets[id]))[0]);
});
manualReachable.clear();

if (!manualSharedMap.has(manualSharedBundleKey)) {
let firstSourceBundle = nullthrows(
bundleGraph.getNode(sourceBundles[0]),
if (configTorequestByAsset.has(manualSharedObject)) {
let requestBySet = new Set(
configTorequestByAsset.get(manualSharedObject),
);
invariant(firstSourceBundle !== 'root');

bundle = createBundle({
uniqueKey: manualSharedObject.name + firstSourceBundle.type,
target: firstSourceBundle.target,
type: firstSourceBundle.type,
env: firstSourceBundle.env,
manualSharedBundle: manualSharedObject?.name,
reachableRoots[i].forEach(nodeId => {
let assetId = bundleRootGraph.getNode(nodeId);
if (assetId == null) return; // deleted
let entry = assets[assetId];
if (requestBySet.has(entry)) {
manualReachable.add(assetId);
reachable.delete(assetId); //ensure assets can still be processed afterwards
}
});
bundle.sourceBundles = new Set(sourceBundles);
bundle.assets.add(asset);
bundleId = bundleGraph.addNode(bundle);
manualSharedMap.set(manualSharedBundleKey, bundleId);
} else {
bundleId = nullthrows(manualSharedMap.get(manualSharedBundleKey));
bundle = nullthrows(bundleGraph.getNode(bundleId));
invariant(
bundle != null && bundle !== 'root',
'We tried to use the root incorrectly',
);
manualReachable = reachable.clone();
}
if (!manualReachable.empty()) {
let bundle;
let bundleId;
let manualSharedBundleKey = manualSharedObject.name + ',' + asset.type;
let sourceBundles = [];
manualReachable.forEach(id => {
sourceBundles.push(nullthrows(bundleRoots.get(assets[id]))[0]);
});

if (!bundle.assets.has(asset)) {
if (!manualSharedMap.has(manualSharedBundleKey)) {
let firstSourceBundle = nullthrows(
bundleGraph.getNode(sourceBundles[0]),
);
invariant(firstSourceBundle !== 'root');

bundle = createBundle({
uniqueKey: manualSharedObject.name + firstSourceBundle.type,
target: firstSourceBundle.target,
type: firstSourceBundle.type,
env: firstSourceBundle.env,
manualSharedBundle: manualSharedObject?.name,
});
bundle.sourceBundles = new Set(sourceBundles);
bundle.assets.add(asset);
bundle.size += asset.stats.size;
}
bundleId = bundleGraph.addNode(bundle);
manualSharedMap.set(manualSharedBundleKey, bundleId);
} else {
bundleId = nullthrows(manualSharedMap.get(manualSharedBundleKey));
bundle = nullthrows(bundleGraph.getNode(bundleId));
invariant(
bundle != null && bundle !== 'root',
'We tried to use the root incorrectly',
);

for (let s of sourceBundles) {
if (s != bundleId) {
bundle.sourceBundles.add(s);
if (!bundle.assets.has(asset)) {
bundle.assets.add(asset);
bundle.size += asset.stats.size;
}
}
}

for (let sourceBundleId of sourceBundles) {
if (bundleId !== sourceBundleId) {
bundleGraph.addEdge(sourceBundleId, bundleId);
for (let s of sourceBundles) {
if (s != bundleId) {
bundle.sourceBundles.add(s);
}
}
}
for (let sourceBundleId of sourceBundles) {
if (bundleId !== sourceBundleId) {
bundleGraph.addEdge(sourceBundleId, bundleId);
}
}
}

dependencyBundleGraph.addNodeByContentKeyIfNeeded(String(bundleId), {
value: bundle,
type: 'bundle',
});
continue;
dependencyBundleGraph.addNodeByContentKeyIfNeeded(String(bundleId), {
value: bundle,
type: 'bundle',
});
}
if (reachable.empty() || manualSharedObject.requestedBy == null) {
continue;
}
}

// Finally, filter out bundleRoots (bundles) from this assets
Expand Down Expand Up @@ -1720,9 +1761,15 @@ const CONFIG_SCHEMA: SchemaEntity = {
type: 'string',
},
},
parent: {
root: {
type: 'string',
},
requestedBy: {
type: 'array',
items: {
type: 'string',
},
},
split: {
type: 'number',
},
Expand Down
Loading
Loading