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

Commit 121c3b2

Browse files
committed
fix: don't restart application when lazy loaded code is changed in angular app with uglify option
Currently there is a logic that gets all runtime files and entry point files from webpack compilation. These files are needed to CLI in order to decides if the application should be restarted or refreshed on device(when there is at least one file that is not hot update file, CLI restarts the application). However, this logic doesn't work for lazy loaded modules in angular application as they are reported neither entry point files nor runtime files. Lazy loaded modules are directly injected into webpack compilation using the hooks of ContextModuleFactory - https://github.com/angular/ngtools-webpack-builds/blob/39ccb0b487e92a7ac4330ff9db821337b7aa5c45/src/angular_compiler_plugin.js#L516. This PR fixes the behavior with lazy loaded files as it gets all chunks produced from webpack compilation and omits hot-update.js files from them. Chunk files are all files except hot update files. Chunk files are: `runtime.js`, `tns_modules/inspector-modules.js`, `bundle.js`, `vendor.js` and all lazy loaded modules. When a files is changed in hmr mode, <hash>.hot-update.js file is included into chunk files. This way we don't need to separate the files to entry point, runtime, lazy loaded and others. We just need to omit <hash>.hot-update.js file from chunk files from webpack compilation.
1 parent aba0313 commit 121c3b2

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

Diff for: plugins/WatchStateLoggerPlugin.ts

+12-35
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,28 @@ export class WatchStateLoggerPlugin {
3333
.keys(compilation.assets)
3434
.filter(assetKey => compilation.assets[assetKey].emitted);
3535

36-
const webpackRuntimeFiles = getWebpackRuntimeOnlyFiles(compilation);
37-
const entryPointFiles = getEntryPointFiles(compilation);
36+
const chunkFiles = getChunkFiles(compilation);
3837

3938
process.send && process.send(messages.compilationComplete, error => null);
4039
// Send emitted files so they can be LiveSynced if need be
41-
process.send && process.send({ emittedFiles, webpackRuntimeFiles, entryPointFiles }, error => null);
40+
process.send && process.send({ emittedFiles, chunkFiles }, error => null);
4241
});
4342
}
4443
}
4544

46-
function getWebpackRuntimeOnlyFiles(compilation) {
47-
let runtimeOnlyFiles = [];
45+
function getChunkFiles(compilation) {
46+
const chunkFiles = [];
4847
try {
49-
runtimeOnlyFiles = [].concat(...Array.from<any>(compilation.entrypoints.values())
50-
.map(entrypoint => entrypoint.runtimeChunk)
51-
// filter embedded runtime chunks (e.g. part of bundle.js or inspector-modules.js)
52-
.filter(runtimeChunk => !!runtimeChunk && runtimeChunk.preventIntegration)
53-
.map(runtimeChunk => runtimeChunk.files))
54-
// get only the unique files in case of "single" runtime (e.g. runtime.js)
55-
.filter((value, index, self) => self.indexOf(value) === index);
56-
} catch (e) {
57-
// breaking change in the Webpack API
58-
console.log("Warning: Unable to find Webpack runtime files.");
59-
}
60-
61-
return runtimeOnlyFiles;
62-
}
63-
64-
function getEntryPointFiles(compilation) {
65-
const entryPointFiles = [];
66-
try {
67-
Array.from(compilation.entrypoints.values())
68-
.forEach((entrypoint: any) => {
69-
for (const entryChunk of entrypoint.chunks) {
70-
entryChunk.files.forEach(fileName => {
71-
if (fileName.indexOf("hot-update") === -1) {
72-
entryPointFiles.push(fileName);
73-
}
74-
});
48+
compilation.chunks.forEach(chunk => {
49+
chunk.files.forEach(file => {
50+
if (file.indexOf("hot-update") === -1) {
51+
chunkFiles.push(file);
7552
}
7653
});
54+
});
7755
} catch (e) {
78-
console.log("Warning: Unable to find Webpack entry point files.");
56+
console.log("Warning: Unable to find chunk files.");
7957
}
8058

81-
return entryPointFiles
82-
.filter((value, index, self) => self.indexOf(value) === index); // get only the unique files
83-
}
59+
return chunkFiles;
60+
}

0 commit comments

Comments
 (0)