Skip to content

Commit bc43a4d

Browse files
committed
Fix HMR when asset is duplicated between multiple bundles
1 parent 2a6bbc5 commit bc43a4d

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

packages/core/integration-tests/test/hmr.js

+15
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,21 @@ module.hot.dispose((data) => {
527527
assert(!reloaded);
528528
});
529529

530+
it('should work when an asset is duplicated', async function () {
531+
let {reloaded, outputs} = await testHMRClient(
532+
'hmr-duplicate',
533+
outputs => {
534+
assert.deepEqual(outputs, [7]);
535+
return {
536+
'shared.js': 'exports.a = 5;',
537+
};
538+
},
539+
);
540+
541+
assert.deepEqual(outputs, [7, 13]);
542+
assert(!reloaded);
543+
});
544+
530545
it('should bubble to parents if child returns additional parents', async function () {
531546
let {reloaded, outputs} = await testHMRClient('hmr-parents', outputs => {
532547
assert.deepEqual(outputs, ['child 2', 'root']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const x = require('./shared');
2+
3+
exports.a = x.a + 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const x = require('./shared');
2+
3+
exports.a = x.a + 2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let a = import('./a');
2+
let b = import('./b');
3+
4+
function run() {
5+
a.then(function (a) {
6+
b.then(function (b) {
7+
output(a.a + b.a);
8+
});
9+
});
10+
};
11+
12+
module.hot.accept();
13+
14+
run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.a = 2;

packages/runtimes/hmr/src/loaders/hmr-runtime.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,11 @@ function hmrApply(bundle /*: ParcelRequire */, asset /*: HMRAsset */) {
482482
// $FlowFixMe
483483
let fn = global.parcelHotUpdate[asset.id];
484484
modules[asset.id] = [fn, deps];
485-
} else if (bundle.parent) {
485+
}
486+
487+
// Always traverse to the parent bundle, even if we already replaced the asset in this bundle.
488+
// This is required in case modules are duplicated. We need to ensure all instances have the updated code.
489+
if (bundle.parent) {
486490
hmrApply(bundle.parent, asset);
487491
}
488492
}

0 commit comments

Comments
 (0)