Skip to content

Commit

Permalink
Prevent circular deps from causing a stack overflow in HMR runtime (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhurstdialpad authored and devongovett committed Feb 25, 2019
1 parent 0b98eed commit 8afacc3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/core/integration-tests/test/hmr.js
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
@@ -0,0 +1,9 @@
var local = require('./local');

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

run();

module.exports = 'value';
@@ -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
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

0 comments on commit 8afacc3

Please sign in to comment.