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

Prevent circular deps from causing a stack overflow in HMR runtime #2660

Merged
merged 3 commits into from
Feb 25, 2019
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
31 changes: 31 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,37 @@ describe('hmr', function() {
assert.deepEqual(outputs, [3, 10]);
});

it('should work with circular dependencies', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-circular'),
path.join(__dirname, '/input')
);

b = bundler(path.join(__dirname, '/input/index.js'), {
watch: true,
hmr: true
});
let bundle = await b.bundle();
let outputs = [];

await run(bundle, {
output(o) {
outputs.push(o);
}
});

assert.deepEqual(outputs, [3]);

await sleep(100);
fs.writeFile(
path.join(__dirname, '/input/local.js'),
"var other = require('./index.js'); exports.a = 5; exports.b = 5;"
);

await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3, 10]);
});

it('should call dispose and accept callbacks', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-callbacks'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var local = require('./local');

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

run();

module.exports = 'value';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var other = require('./index.js');

exports.a = 1;
exports.b = 2;
8 changes: 8 additions & 0 deletions packages/core/parcel-bundler/src/builtins/hmr-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ function Module(moduleName) {
}

module.bundle.Module = Module;
var updatedAssets;

var parent = module.bundle.parent;
if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
var hostname = process.env.HMR_HOSTNAME || location.hostname;
var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
var ws = new WebSocket(protocol + '://' + hostname + ':' + process.env.HMR_PORT + '/');
ws.onmessage = function(event) {
updatedAssets = {};

var data = JSON.parse(event.data);

if (data.type === 'update') {
Expand Down Expand Up @@ -147,6 +150,11 @@ function hmrAccept(bundle, id) {
return hmrAccept(bundle.parent, id);
}

if (updatedAssets[id]) {
return;
}
updatedAssets[id] = true;

var cached = bundle.cache[id];
bundle.hotData = {};
if (cached) {
Expand Down