Skip to content

Commit

Permalink
Fix circular bundles, and never hoist the entry asset (#548)
Browse files Browse the repository at this point in the history
Slightly simpler, more generic version of #489.
  • Loading branch information
devongovett committed Jan 12, 2018
1 parent 97cf294 commit dd26db3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class Bundler extends EventEmitter {
this.buildQueue.delete(asset);
}

createBundleTree(asset, dep, bundle) {
createBundleTree(asset, dep, bundle, parentBundles = new Set()) {
if (dep) {
asset.parentDeps.add(dep);
}
Expand All @@ -405,7 +405,14 @@ class Bundler extends EventEmitter {
this.moveAssetToBundle(asset, commonBundle);
return;
}
} else return;
} else {
return;
}

// Detect circular bundles
if (parentBundles.has(asset.parentBundle)) {
return;
}
}

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

asset.parentBundle = bundle;
parentBundles.add(bundle);

for (let [dep, assetDep] of asset.depAssets) {
this.createBundleTree(assetDep, dep, bundle);
this.createBundleTree(assetDep, dep, bundle, parentBundles);
}

parentBundles.delete(bundle);
return bundle;
}

moveAssetToBundle(asset, commonBundle) {
// Never move the entry asset of a bundle, as it was explicitly requested to be placed in a separate bundle.
if (asset.parentBundle.entryAsset === asset) {
return;
}

for (let bundle of Array.from(asset.bundles)) {
bundle.removeAsset(asset);
commonBundle.getSiblingBundle(bundle.type).addAsset(asset);
Expand Down
32 changes: 32 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,36 @@ describe('html', function() {
]
});
});

it('should support circular dependencies', async function() {
let b = await bundle(__dirname + '/integration/circular/index.html');

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'html',
assets: ['about.html'],
childBundles: [
{
type: 'js',
assets: ['about.js', 'index.js'],
childBundles: []
},
{
type: 'html',
assets: ['test.html'],
childBundles: []
}
]
},
{
type: 'js',
assets: ['about.js', 'index.js'],
childBundles: []
}
]
});
});
});
6 changes: 6 additions & 0 deletions test/integration/circular/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"sourceType": "module"
}
}
15 changes: 15 additions & 0 deletions test/integration/circular/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script src="./about.js"></script>
</head>
<body>
On About

<a href="./index.html">Home</a>
<a href="./about.html">About</a>
<a href="./test.html">Test</a>

</body>
</html>
2 changes: 2 additions & 0 deletions test/integration/circular/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-unused-vars
import index from './index';
15 changes: 15 additions & 0 deletions test/integration/circular/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script src="./index.js"></script>
</head>
<body>
On Home

<a href="./index.html">Home</a>
<a href="./about.html">About</a>
<a href="./test.html">Test</a>

</body>
</html>
2 changes: 2 additions & 0 deletions test/integration/circular/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-unused-vars
import about from './about';
14 changes: 14 additions & 0 deletions test/integration/circular/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
Test

<a href="./index.html">Home</a>
<a href="./about.html">About</a>
<a href="./test.html">Test</a>

</body>
</html>

0 comments on commit dd26db3

Please sign in to comment.