Skip to content

Commit

Permalink
Regenerate all bundles and trigger an HMR page reload when a new bund…
Browse files Browse the repository at this point in the history
…le is created (#2762)
  • Loading branch information
devongovett committed Mar 12, 2019
1 parent 70f1008 commit 161cd27
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
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
Empty file.
@@ -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

0 comments on commit 161cd27

Please sign in to comment.