Skip to content

Commit

Permalink
Merge branch 'experimental-bundler-integration' of https://github.com…
Browse files Browse the repository at this point in the history
…/parcel-bundler/parcel into experimental-bundler-integration
  • Loading branch information
gorakong committed Jun 2, 2022
2 parents f851eb7 + 0c3a653 commit 36c647c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
60 changes: 44 additions & 16 deletions packages/bundlers/experimental/src/ExperimentalBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ export default (new Bundler({
},

bundle({bundleGraph, config}) {
decorateLegacyGraph(createIdealGraph(bundleGraph, config), bundleGraph);
let targetMap = getEntryByTarget(bundleGraph);
let graphs = [];
for (let entries of targetMap.values()) {
graphs.push(createIdealGraph(bundleGraph, config, entries));
}
for (let g of graphs) {
decorateLegacyGraph(g, bundleGraph);
}
},
optimize() {},
}): Bundler);
Expand Down Expand Up @@ -276,6 +283,7 @@ function decorateLegacyGraph(
function createIdealGraph(
assetGraph: MutableBundleGraph,
config: ResolvedBundlerConfig,
entries: Map<Asset, Dependency>,
): IdealGraph {
// Asset to the bundle and group it's an entry of
let bundleRoots: Map<BundleRoot, [NodeId, NodeId]> = new Map();
Expand Down Expand Up @@ -304,27 +312,13 @@ function createIdealGraph(
// Models bundleRoots and the assets that require it synchronously
let reachableRoots: ContentGraph<Asset> = new ContentGraph();
// Step Create Entry Bundles: Find and create bundles for entries from assetGraph
let entries: Map<Asset, Dependency> = new Map();
let sharedToSourceBundleIds: Map<NodeId, Array<NodeId>> = new Map();
assetGraph.traverse((node, context, actions) => {
if (node.type !== 'asset') {
return node;
}

invariant(
context != null && context.type === 'dependency' && context.value.isEntry,
);
entries.set(node.value, context.value);
actions.skipChildren();
});

let rootNodeId = nullthrows(asyncBundleRootGraph.addNode('root'));
let bundleGraphRootNodeId = nullthrows(bundleGraph.addNode('root'));
asyncBundleRootGraph.setRootNodeId(rootNodeId);
bundleGraph.setRootNodeId(bundleGraphRootNodeId);

// Step Create Entry Bundles
for (let [asset, dependency] of entries) {
let bundle = createBundle({
asset,
Expand Down Expand Up @@ -362,6 +356,14 @@ function createIdealGraph(
assetGraph.traverse({
enter(node, context, actions) {
if (node.type === 'asset') {
if (
context?.type === 'dependency' &&
context?.value.isEntry &&
!entries.has(node.value)
) {
actions.skipChildren();
return node;
}
assets.push(node.value);

let bundleIdTuple = bundleRoots.get(node.value);
Expand Down Expand Up @@ -1201,3 +1203,29 @@ function getReachableBundleRoots(asset, graph): Array<BundleRoot> {
.getNodeIdsConnectedTo(graph.getNodeIdByContentKey(asset.id))
.map(nodeId => nullthrows(graph.getNode(nodeId)));
}

function getEntryByTarget(
bundleGraph: MutableBundleGraph,
): DefaultMap<string, Map<Asset, Dependency>> {
// Find entries from assetGraph per target
let targets: DefaultMap<string, Map<Asset, Dependency>> = new DefaultMap(
() => new Map(),
);
bundleGraph.traverse({
enter(node, context, actions) {
if (node.type !== 'asset') {
return node;
}
invariant(
context != null &&
context.type === 'dependency' &&
context.value.isEntry &&
context.value.target != null,
);
targets.get(context.value.target.distDir).set(node.value, context.value);
actions.skipChildren();
return node;
},
});
return targets;
}
1 change: 0 additions & 1 deletion packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,6 @@ export default class BundleGraph {
nodeId,
bundleGraphEdgeTypes.references,
),
//.reverse(),
});

return [...referencedBundles];
Expand Down
33 changes: 0 additions & 33 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1994,39 +1994,6 @@ describe('javascript', function () {
);
});

it('should contain duplicate assets in workers when in development', async () => {
if (process.env.PARCEL_TEST_EXPERIMENTAL_BUNDLER) return;
let b = await bundle(
path.join(__dirname, '/integration/worker-shared/index.js'),
{mode: 'development'},
);

assertBundles(b, [
{
name: 'index.js',
assets: [
'index.js',
'bundle-url.js',
'get-worker-url.js',
'lodash.js',
'esmodule-helpers.js',
],
},
{
assets: [
'worker-a.js',
'bundle-url.js',
'esmodule-helpers.js',
'get-worker-url.js',
'lodash.js',
],
},
{
assets: ['worker-b.js', 'lodash.js', 'esmodule-helpers.js'],
},
]);
});

it('should deduplicate and remove an unnecessary async bundle when it contains a cyclic reference to its entry', async () => {
let b = await bundle(
path.join(
Expand Down

0 comments on commit 36c647c

Please sign in to comment.