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

Don't dedupe assets that are depended on in more than one bundle #2122

Merged
merged 2 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 16 additions & 7 deletions src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ class JSPackager extends Packager {
}

async addAsset(asset) {
let key = this.dedupeKey(asset);
if (this.dedupe.has(key)) {
return;
}
// If this module is referenced by another JS bundle, it needs to be exposed externally.
// In that case, don't dedupe the asset as it would affect the module ids that are referenced by other bundles.
let isExposed = !Array.from(asset.parentDeps).every(dep => {
let depAsset = this.bundler.loadedAssets.get(dep.parent);
return this.bundle.assets.has(depAsset) || depAsset.type !== 'js';
});

if (!isExposed) {
let key = this.dedupeKey(asset);
if (this.dedupe.has(key)) {
return;
}

// Don't dedupe when HMR is turned on since it messes with the asset ids
if (!this.options.hmr) {
this.dedupe.set(key, asset.id);
// Don't dedupe when HMR is turned on since it messes with the asset ids
if (!this.options.hmr) {
this.dedupe.set(key, asset.id);
}
}

let deps = {};
Expand Down
3 changes: 3 additions & 0 deletions test/integration/js-dedup-hoist/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import hello2 from './hello2'

export default `${hello2}`
2 changes: 2 additions & 0 deletions test/integration/js-dedup-hoist/hello1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const value = 'Hello'
export default value
2 changes: 2 additions & 0 deletions test/integration/js-dedup-hoist/hello2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const value = 'Hello'
export default value
7 changes: 7 additions & 0 deletions test/integration/js-dedup-hoist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import hello1 from './hello1'
import hello2 from './hello2'

export default async function () {
let a = await import('./a');
return `${hello1} ${hello2}! ${a.default}`;
}
3 changes: 3 additions & 0 deletions test/integration/js-dedup-hoist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"browserslist": ["last 1 Chrome version"]
}
32 changes: 25 additions & 7 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,24 +1520,42 @@ describe('javascript', function() {
}
);
const {rootDir} = b.entryAsset.options;
const dedupedAssets = Array.from(b.offsets.keys()).map(asset => asset.name);
assert.equal(dedupedAssets.length, 2);
assert(dedupedAssets.includes(path.join(rootDir, 'index.js')));
const writtenAssets = Array.from(b.offsets.keys()).map(asset => asset.name);
assert.equal(writtenAssets.length, 2);
assert(writtenAssets.includes(path.join(rootDir, 'index.js')));
assert(
dedupedAssets.includes(path.join(rootDir, 'hello1.js')) ||
dedupedAssets.includes(path.join(rootDir, 'hello2.js'))
writtenAssets.includes(path.join(rootDir, 'hello1.js')) ||
writtenAssets.includes(path.join(rootDir, 'hello2.js'))
);
assert(
!(
dedupedAssets.includes(path.join(rootDir, 'hello1.js')) &&
dedupedAssets.includes(path.join(rootDir, 'hello2.js'))
writtenAssets.includes(path.join(rootDir, 'hello1.js')) &&
writtenAssets.includes(path.join(rootDir, 'hello2.js'))
)
);

let module = await run(b);
assert.equal(module.default, 'Hello Hello!');
});

it('should not dedupe assets that exist in more than one bundle', async function() {
let b = await bundle(
path.join(__dirname, `/integration/js-dedup-hoist/index.js`),
{
hmr: false // enable asset dedupe in JSPackager
}
);
const {rootDir} = b.entryAsset.options;
const writtenAssets = Array.from(b.offsets.keys()).map(asset => asset.name);
assert(
writtenAssets.includes(path.join(rootDir, 'hello1.js')) &&
writtenAssets.includes(path.join(rootDir, 'hello2.js'))
);

let module = await run(b);
assert.equal(await module.default(), 'Hello Hello! Hello');
});

it('should support importing HTML from JS async', async function() {
let b = await bundle(
path.join(__dirname, '/integration/import-html-async/index.js'),
Expand Down