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

Fix HMR failure with js error on load, take 2 #2531

Merged
merged 8 commits into from Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/core/integration-tests/test/hmr.js
@@ -1,7 +1,8 @@
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 @@ -287,6 +288,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'),
'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
17 changes: 16 additions & 1 deletion packages/core/parcel-bundler/src/builtins/prelude.js
Expand Up @@ -77,8 +77,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 +111,12 @@ parcelRequire = (function (modules, cache, entry, globalName) {
}

// Override the current require with this new one
parcelRequire = newRequire;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems dangerous. Why are we overwriting the global variable here? Won't that already happen when the function returns?

Copy link
Member Author

@mischnic mischnic Mar 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because returning doesn't work if an error is thrown and parcelRequire is then undefined for subsequent HMR updates

parcelRequire = newRequire;
if(error){
// throw error from earlier, _after updating parcelRequire_
throw error;
}
return newRequire;

And this overwrites a global variable as well:

parcelRequire = (function (modules, cache, entry, globalName) {


if(error){
// throw error from earlier, _after updating parcelRequire_
throw error;
}

return newRequire;
})
1 change: 1 addition & 0 deletions packages/core/test-utils/src/utils.js
Expand Up @@ -289,6 +289,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