Skip to content

Commit

Permalink
Fix HMR failure with js error on load, take 2 (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed Mar 6, 2019
1 parent 011bfa9 commit 30624f7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
55 changes: 54 additions & 1 deletion packages/core/integration-tests/test/hmr.js
@@ -1,7 +1,14 @@
const assert = require('assert');
const fs = require('@parcel/fs');
const path = require('path');
const {bundler, run, rimraf, ncp} = require('@parcel/test-utils');
const {
bundler,
run,
rimraf,
ncp,
prepareBrowserContext
} = require('@parcel/test-utils');
const vm = require('vm');
const {sleep} = require('@parcel/test-utils');
const WebSocket = require('ws');
const json5 = require('json5');
Expand Down Expand Up @@ -318,6 +325,52 @@ describe('hmr', function() {
assert.deepEqual(outputs, [3, 10]);
});

it('should accept HMR updates in the runtime after an initial error', async function() {
await fs.mkdirp(path.join(__dirname, '/input'));
fs.writeFile(
path.join(__dirname, '/input/index.js'),
'module.hot.accept();throw new Error("Something");\noutput(123);'
);

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

let outputs = [];
let errors = [];

var ctx = prepareBrowserContext(bundle, {
output(o) {
outputs.push(o);
},
error(e) {
errors.push(e);
}
});
vm.createContext(ctx);
vm.runInContext(
`try {
${(await fs.readFile(bundle.name)).toString()}
} catch(e) {
error(e);
}`,
ctx
);

assert.deepEqual(outputs, []);
assert.equal(errors.length, 1);
assert.equal(errors[0].message, 'Something');

await sleep(100);
fs.writeFile(path.join(__dirname, '/input/index.js'), 'output(123);');

await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [123]);
assert.equal(errors.length, 1);
});

it('should call dispose and accept callbacks', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-callbacks'),
Expand Down
21 changes: 16 additions & 5 deletions packages/core/parcel-bundler/src/builtins/prelude.js
Expand Up @@ -5,9 +5,7 @@
//
// anything defined in a previous bundle is accessed via the
// orig method which is the require for previous bundles

// eslint-disable-next-line no-global-assign
parcelRequire = (function (modules, cache, entry, globalName) {
(function (modules, cache, entry, globalName) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof parcelRequire === 'function' && parcelRequire;
var nodeRequire = typeof require === 'function' && require;
Expand Down Expand Up @@ -77,8 +75,16 @@ parcelRequire = (function (modules, cache, entry, globalName) {
}, {}];
};

var error;
for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
try {
newRequire(entry[i]);
} catch (e) {
// Save first error but execute all entries
if (!error) {
error = e;
}
}
}

if (entry.length) {
Expand All @@ -103,5 +109,10 @@ parcelRequire = (function (modules, cache, entry, globalName) {
}

// Override the current require with this new one
return newRequire;
parcelRequire = newRequire;

if (error) {
// throw error from earlier, _after updating parcelRequire_
throw error;
}
})
1 change: 1 addition & 0 deletions packages/core/test-utils/src/utils.js
Expand Up @@ -293,6 +293,7 @@ exports.symlinkPrivilegeWarning = symlinkPrivilegeWarning;
exports.bundler = bundler;
exports.bundle = bundle;
exports.run = run;
exports.prepareBrowserContext = prepareBrowserContext;
exports.assertBundleTree = assertBundleTree;
exports.nextBundle = nextBundle;
exports.deferred = deferred;
Expand Down

0 comments on commit 30624f7

Please sign in to comment.