Skip to content

Commit f699e81

Browse files
pcattoridevongovett
authored andcommitted
fixes #933 : JSPackager deduplication now accounts for differences in absolute dependency paths (#1011)
1 parent 06fb3c8 commit f699e81

13 files changed

Lines changed: 94 additions & 3 deletions

File tree

ā€Žsrc/packagers/JSPackager.jsā€Ž

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path');
33
const Packager = require('./Packager');
44
const urlJoin = require('../utils/urlJoin');
55
const lineCounter = require('../utils/lineCounter');
6+
const objectHash = require('../utils/objectHash');
67

78
const prelude = {
89
source: fs
@@ -35,13 +36,14 @@ class JSPackager extends Packager {
3536
}
3637

3738
async addAsset(asset) {
38-
if (this.dedupe.has(asset.generated.js)) {
39+
let key = this.dedupeKey(asset);
40+
if (this.dedupe.has(key)) {
3941
return;
4042
}
4143

4244
// Don't dedupe when HMR is turned on since it messes with the asset ids
4345
if (!this.options.hmr) {
44-
this.dedupe.set(asset.generated.js, asset.id);
46+
this.dedupe.set(key, asset.id);
4547
}
4648

4749
let deps = {};
@@ -60,7 +62,7 @@ class JSPackager extends Packager {
6062
deps[dep.name] = bundles;
6163
this.bundleLoaders.add(mod.type);
6264
} else {
63-
deps[dep.name] = this.dedupe.get(mod.generated.js) || mod.id;
65+
deps[dep.name] = this.dedupe.get(this.dedupeKey(mod)) || mod.id;
6466

6567
// If the dep isn't in this bundle, add it to the list of external modules to preload.
6668
// Only do this if this is the root JS bundle, otherwise they will have already been
@@ -93,6 +95,14 @@ class JSPackager extends Packager {
9395
return name;
9496
}
9597

98+
dedupeKey(asset) {
99+
// cannot rely *only* on generated JS for deduplication because paths like
100+
// `../` can cause 2 identical JS files to behave differently depending on
101+
// where they are located on the filesystem
102+
let deps = Array.from(asset.depAssets.values(), dep => dep.name).sort();
103+
return objectHash([asset.generated.js, deps]);
104+
}
105+
96106
async writeModule(id, code, deps = {}, map) {
97107
let wrapped = this.first ? '' : ',';
98108
wrapped +=
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value = 'Hello'
2+
export default value
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import hello from './hello.js'
2+
import world from './world.js'
3+
4+
export default `${hello} ${world}!`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value = 'World'
2+
export default value
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// hello/index.js and world/index.js are exactly the same content-wise, but in different locations
2+
import value from './value'
3+
export default value
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value = 'Hello'
2+
export default value
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import hello from './hello'
2+
import world from './world'
3+
4+
export default `${hello} ${world}!`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// hello/index.js and world/index.js are exactly the same content-wise, but in different locations
2+
import value from './value'
3+
export default value
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value = 'World'
2+
export default value
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value = 'Hello'
2+
export default value

0 commit comments

Comments
Ā (0)
⚔