Skip to content

Commit dd26db3

Browse files
authored
Fix circular bundles, and never hoist the entry asset (#548)
Slightly simpler, more generic version of #489.
1 parent 97cf294 commit dd26db3

8 files changed

Lines changed: 103 additions & 3 deletions

File tree

ā€Žsrc/Bundler.jsā€Ž

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class Bundler extends EventEmitter {
389389
this.buildQueue.delete(asset);
390390
}
391391

392-
createBundleTree(asset, dep, bundle) {
392+
createBundleTree(asset, dep, bundle, parentBundles = new Set()) {
393393
if (dep) {
394394
asset.parentDeps.add(dep);
395395
}
@@ -405,7 +405,14 @@ class Bundler extends EventEmitter {
405405
this.moveAssetToBundle(asset, commonBundle);
406406
return;
407407
}
408-
} else return;
408+
} else {
409+
return;
410+
}
411+
412+
// Detect circular bundles
413+
if (parentBundles.has(asset.parentBundle)) {
414+
return;
415+
}
409416
}
410417

411418
// Create the root bundle if it doesn't exist
@@ -444,15 +451,22 @@ class Bundler extends EventEmitter {
444451
}
445452

446453
asset.parentBundle = bundle;
454+
parentBundles.add(bundle);
447455

448456
for (let [dep, assetDep] of asset.depAssets) {
449-
this.createBundleTree(assetDep, dep, bundle);
457+
this.createBundleTree(assetDep, dep, bundle, parentBundles);
450458
}
451459

460+
parentBundles.delete(bundle);
452461
return bundle;
453462
}
454463

455464
moveAssetToBundle(asset, commonBundle) {
465+
// Never move the entry asset of a bundle, as it was explicitly requested to be placed in a separate bundle.
466+
if (asset.parentBundle.entryAsset === asset) {
467+
return;
468+
}
469+
456470
for (let bundle of Array.from(asset.bundles)) {
457471
bundle.removeAsset(asset);
458472
commonBundle.getSiblingBundle(bundle.type).addAsset(asset);

ā€Žtest/html.jsā€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,36 @@ describe('html', function() {
299299
]
300300
});
301301
});
302+
303+
it('should support circular dependencies', async function() {
304+
let b = await bundle(__dirname + '/integration/circular/index.html');
305+
306+
assertBundleTree(b, {
307+
name: 'index.html',
308+
assets: ['index.html'],
309+
childBundles: [
310+
{
311+
type: 'html',
312+
assets: ['about.html'],
313+
childBundles: [
314+
{
315+
type: 'js',
316+
assets: ['about.js', 'index.js'],
317+
childBundles: []
318+
},
319+
{
320+
type: 'html',
321+
assets: ['test.html'],
322+
childBundles: []
323+
}
324+
]
325+
},
326+
{
327+
type: 'js',
328+
assets: ['about.js', 'index.js'],
329+
childBundles: []
330+
}
331+
]
332+
});
333+
});
302334
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../.eslintrc.json",
3+
"parserOptions": {
4+
"sourceType": "module"
5+
}
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Document</title>
5+
<script src="./about.js"></script>
6+
</head>
7+
<body>
8+
On About
9+
10+
<a href="./index.html">Home</a>
11+
<a href="./about.html">About</a>
12+
<a href="./test.html">Test</a>
13+
14+
</body>
15+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import index from './index';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Document</title>
5+
<script src="./index.js"></script>
6+
</head>
7+
<body>
8+
On Home
9+
10+
<a href="./index.html">Home</a>
11+
<a href="./about.html">About</a>
12+
<a href="./test.html">Test</a>
13+
14+
</body>
15+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import about from './about';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Document</title>
5+
</head>
6+
<body>
7+
Test
8+
9+
<a href="./index.html">Home</a>
10+
<a href="./about.html">About</a>
11+
<a href="./test.html">Test</a>
12+
13+
</body>
14+
</html>

0 commit comments

Comments
Ā (0)
⚔