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

Regenerate all bundles and trigger an HMR page reload when a new bundle is created #2762

Merged
merged 1 commit into from Mar 12, 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
34 changes: 34 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Expand Up @@ -484,6 +484,40 @@ describe('hmr', function() {
assert(spy.calledOnce);
});

it('should trigger a page reload when a new bundle is created', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-new-bundle'),
path.join(__dirname, '/input')
);

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

let ctx = await run([...bundle.childBundles][0], {}, {require: false});
let spy = sinon.spy(ctx.location, 'reload');

await sleep(50);
assert(spy.notCalled);

await sleep(100);
fs.writeFile(
path.join(__dirname, '/input/index.js'),
'import "./index.css"'
);

await nextEvent(b, 'bundled');
assert(spy.calledOnce);

let contents = await fs.readFile(
path.join(__dirname, '/dist/index.html'),
'utf8'
);
assert(contents.includes('.css'));
});

it('should log emitted errors and show an error overlay', async function() {
await ncp(
path.join(__dirname, '/integration/commonjs'),
Expand Down
@@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
<script src="./index.js"></script>
</body>
</html>
Empty file.
6 changes: 4 additions & 2 deletions packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -304,6 +304,7 @@ class Bundler extends EventEmitter {
}

// Generate the final bundle names, and replace references in the built assets.
let numBundles = this.bundleNameMap ? this.bundleNameMap.size : 0;
this.bundleNameMap = this.mainBundle.getBundleNameMap(
this.options.contentHash
);
Expand All @@ -313,16 +314,17 @@ class Bundler extends EventEmitter {
}

// Emit an HMR update if this is not the initial bundle.
let bundlesChanged = numBundles !== this.bundleNameMap.size;
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate(changedAssets);
this.hmr.emitUpdate(changedAssets, bundlesChanged);
}

logger.progress(`Packaging...`);

// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
this.bundleHashes
bundlesChanged ? null : this.bundleHashes
);

// Unload any orphaned assets
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/HMRServer.js
Expand Up @@ -63,15 +63,15 @@ class HMRServer {
this.broadcast(this.unresolvedError);
}

emitUpdate(assets) {
emitUpdate(assets, reload = false) {
if (this.unresolvedError) {
this.unresolvedError = null;
this.broadcast({
type: 'error-resolved'
});
}

const shouldReload = assets.some(asset => asset.hmrPageReload);
const shouldReload = reload || assets.some(asset => asset.hmrPageReload);
if (shouldReload) {
this.broadcast({
type: 'reload'
Expand Down