Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

fix: ignore the Webpack runtime chunks when sending HMR updates #873

Merged
merged 2 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
6 changes: 5 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) {
Expand All @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion plugins/WatchStateLoggerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}