Skip to content

Commit

Permalink
Fix HMR when asset is duplicated between multiple bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Nov 25, 2024
1 parent 2a6bbc5 commit bc43a4d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,21 @@ module.hot.dispose((data) => {
assert(!reloaded);
});

it('should work when an asset is duplicated', async function () {
let {reloaded, outputs} = await testHMRClient(
'hmr-duplicate',
outputs => {
assert.deepEqual(outputs, [7]);
return {
'shared.js': 'exports.a = 5;',
};
},
);

assert.deepEqual(outputs, [7, 13]);
assert(!reloaded);
});

it('should bubble to parents if child returns additional parents', async function () {
let {reloaded, outputs} = await testHMRClient('hmr-parents', outputs => {
assert.deepEqual(outputs, ['child 2', 'root']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const x = require('./shared');

exports.a = x.a + 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const x = require('./shared');

exports.a = x.a + 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let a = import('./a');
let b = import('./b');

function run() {
a.then(function (a) {
b.then(function (b) {
output(a.a + b.a);
});
});
};

module.hot.accept();

run();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.a = 2;
6 changes: 5 additions & 1 deletion packages/runtimes/hmr/src/loaders/hmr-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,11 @@ function hmrApply(bundle /*: ParcelRequire */, asset /*: HMRAsset */) {
// $FlowFixMe
let fn = global.parcelHotUpdate[asset.id];
modules[asset.id] = [fn, deps];
} else if (bundle.parent) {
}

// Always traverse to the parent bundle, even if we already replaced the asset in this bundle.
// This is required in case modules are duplicated. We need to ensure all instances have the updated code.
if (bundle.parent) {
hmrApply(bundle.parent, asset);
}
}
Expand Down

0 comments on commit bc43a4d

Please sign in to comment.