diff --git a/packages/core/integration-tests/test/hmr.js b/packages/core/integration-tests/test/hmr.js index d1dc9d765db..99671c3faab 100644 --- a/packages/core/integration-tests/test/hmr.js +++ b/packages/core/integration-tests/test/hmr.js @@ -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'), diff --git a/packages/core/integration-tests/test/integration/hmr-new-bundle/index.css b/packages/core/integration-tests/test/integration/hmr-new-bundle/index.css new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/integration/hmr-new-bundle/index.html b/packages/core/integration-tests/test/integration/hmr-new-bundle/index.html new file mode 100644 index 00000000000..2201e3452ca --- /dev/null +++ b/packages/core/integration-tests/test/integration/hmr-new-bundle/index.html @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/hmr-new-bundle/index.js b/packages/core/integration-tests/test/integration/hmr-new-bundle/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/parcel-bundler/src/Bundler.js b/packages/core/parcel-bundler/src/Bundler.js index d521ad24c3c..b63e335f9a5 100644 --- a/packages/core/parcel-bundler/src/Bundler.js +++ b/packages/core/parcel-bundler/src/Bundler.js @@ -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 ); @@ -313,8 +314,9 @@ 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...`); @@ -322,7 +324,7 @@ class Bundler extends EventEmitter { // Package everything up this.bundleHashes = await this.mainBundle.package( this, - this.bundleHashes + bundlesChanged ? null : this.bundleHashes ); // Unload any orphaned assets diff --git a/packages/core/parcel-bundler/src/HMRServer.js b/packages/core/parcel-bundler/src/HMRServer.js index 1ccbf1806fc..080df427763 100644 --- a/packages/core/parcel-bundler/src/HMRServer.js +++ b/packages/core/parcel-bundler/src/HMRServer.js @@ -63,7 +63,7 @@ class HMRServer { this.broadcast(this.unresolvedError); } - emitUpdate(assets) { + emitUpdate(assets, reload = false) { if (this.unresolvedError) { this.unresolvedError = null; this.broadcast({ @@ -71,7 +71,7 @@ class HMRServer { }); } - const shouldReload = assets.some(asset => asset.hmrPageReload); + const shouldReload = reload || assets.some(asset => asset.hmrPageReload); if (shouldReload) { this.broadcast({ type: 'reload'