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

Fix circular bundles, and never hoist the entry asset #548

Merged
merged 1 commit into from
Jan 12, 2018
Merged
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
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>