From be82ab7a267d6c8a0ab879fe289115eeae73f8f0 Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Tue, 23 Apr 2019 11:20:34 +0300 Subject: [PATCH 1/2] fix: ignore the Webpack runtime chunks when sending HMR updates --- lib/compiler.js | 2 +- lib/utils.js | 6 +++++- plugins/WatchStateLoggerPlugin.ts | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 588edc5d..6398436c 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -80,7 +80,7 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $ return; } - const result = getUpdatedEmittedFiles(message.emittedFiles); + const result = getUpdatedEmittedFiles(message.emittedFiles, message.webpackRuntimeFiles); if (hookArgs.hmrData) { hookArgs.hmrData[platform] = { diff --git a/lib/utils.js b/lib/utils.js index 5bb0dea2..b5341c52 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -34,7 +34,7 @@ function buildEnvData($projectData, platform, env) { * if yes this is a HMR update and remove all bundle files as we don't need them to be synced, * but only the update chunks */ -function getUpdatedEmittedFiles(emittedFiles) { +function getUpdatedEmittedFiles(emittedFiles, webpackRuntimeFiles) { let fallbackFiles = []; let hotHash; if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) { @@ -45,6 +45,10 @@ function getUpdatedEmittedFiles(emittedFiles) { hotHash = hash; // remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js result = result.filter(file => file !== `${name}.js`); + if (webpackRuntimeFiles && webpackRuntimeFiles.length) { + // remove files containing only the Webpack runtime (e.g. runtime.js) + result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1); + } }); //if applying of hot update fails, we must fallback to the full files fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1); diff --git a/plugins/WatchStateLoggerPlugin.ts b/plugins/WatchStateLoggerPlugin.ts index 565407c1..0a8229c5 100644 --- a/plugins/WatchStateLoggerPlugin.ts +++ b/plugins/WatchStateLoggerPlugin.ts @@ -31,6 +31,7 @@ export class WatchStateLoggerPlugin { console.log(messages.compilationComplete); } + const runtimeOnlyFiles = getWebpackRuntimeOnlyFiles(compilation, compiler); let emittedFiles = Object .keys(compilation.assets) .filter(assetKey => compilation.assets[assetKey].emitted); @@ -42,7 +43,28 @@ export class WatchStateLoggerPlugin { process.send && process.send(messages.compilationComplete, error => null); // Send emitted files so they can be LiveSynced if need be - process.send && process.send({ emittedFiles: emittedFilesFakePaths }, error => null); + process.send && process.send({ emittedFiles: emittedFilesFakePaths, webpackRuntimeFiles: runtimeOnlyFiles }, error => null); }); } } + +function getWebpackRuntimeOnlyFiles(compilation, compiler) { + let runtimeOnlyFiles = []; + try { + runtimeOnlyFiles = [].concat(...compilation.chunkGroups + // get the chunk group of each entry points (e.g. main.js and inspector-modules.js) + .map(chunkGroup => chunkGroup.runtimeChunk) + // filter embedded runtime chunks (e.g. part of bundle.js or inspector-modules.js) + .filter(runtimeChunk => runtimeChunk.preventIntegration) + .map(runtimeChunk => runtimeChunk.files)) + // get only the unique files in case of "single" runtime (e.g. runtime.js) + .filter((value, index, self) => self.indexOf(value) === index) + // convert to absolute paths + .map(fileName => join(compiler.context, fileName)); + } catch (e) { + // breaking change in the Webpack API + console.log("Warning: Unable to find Webpack runtime files."); + } + + return runtimeOnlyFiles; +} From e9b52b64be37d6eaa4c6a5c375a5d438596ef1e9 Mon Sep 17 00:00:00 2001 From: Stanimira Vlaeva Date: Thu, 25 Apr 2019 09:00:22 +0300 Subject: [PATCH 2/2] chore: apply PR suggestions --- plugins/WatchStateLoggerPlugin.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugins/WatchStateLoggerPlugin.ts b/plugins/WatchStateLoggerPlugin.ts index 0a8229c5..55cbb8ba 100644 --- a/plugins/WatchStateLoggerPlugin.ts +++ b/plugins/WatchStateLoggerPlugin.ts @@ -31,7 +31,7 @@ export class WatchStateLoggerPlugin { console.log(messages.compilationComplete); } - const runtimeOnlyFiles = getWebpackRuntimeOnlyFiles(compilation, compiler); + const runtimeOnlyFiles = getWebpackRuntimeOnlyFiles(compilation, compiler.context); let emittedFiles = Object .keys(compilation.assets) .filter(assetKey => compilation.assets[assetKey].emitted); @@ -48,19 +48,18 @@ export class WatchStateLoggerPlugin { } } -function getWebpackRuntimeOnlyFiles(compilation, compiler) { +function getWebpackRuntimeOnlyFiles(compilation, basePath) { let runtimeOnlyFiles = []; try { - runtimeOnlyFiles = [].concat(...compilation.chunkGroups - // get the chunk group of each entry points (e.g. main.js and inspector-modules.js) - .map(chunkGroup => chunkGroup.runtimeChunk) + runtimeOnlyFiles = [].concat(...Array.from(compilation.entrypoints.values()) + .map(entrypoint => entrypoint.runtimeChunk) // filter embedded runtime chunks (e.g. part of bundle.js or inspector-modules.js) - .filter(runtimeChunk => runtimeChunk.preventIntegration) + .filter(runtimeChunk => !!runtimeChunk && runtimeChunk.preventIntegration) .map(runtimeChunk => runtimeChunk.files)) // get only the unique files in case of "single" runtime (e.g. runtime.js) .filter((value, index, self) => self.indexOf(value) === index) // convert to absolute paths - .map(fileName => join(compiler.context, fileName)); + .map(fileName => join(basePath, fileName)); } catch (e) { // breaking change in the Webpack API console.log("Warning: Unable to find Webpack runtime files.");