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 all 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
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;
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;
}
})
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