This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathWatchStateLoggerPlugin.ts
83 lines (74 loc) · 3.29 KB
/
WatchStateLoggerPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
export enum messages {
compilationComplete = "Webpack compilation complete.",
startWatching = "Webpack compilation complete. Watching for file changes.",
changeDetected = "File change detected. Starting incremental webpack compilation..."
}
/**
* This little plugin will report the webpack state through the console.
* So the {N} CLI can get some idea when compilation completes.
*/
export class WatchStateLoggerPlugin {
isRunningWatching: boolean;
apply(compiler) {
const plugin = this;
compiler.hooks.watchRun.tapAsync("WatchStateLoggerPlugin", function (compiler, callback) {
plugin.isRunningWatching = true;
if (plugin.isRunningWatching) {
console.log(messages.changeDetected);
}
process.send && process.send(messages.changeDetected, error => null);
callback();
});
compiler.hooks.afterEmit.tapAsync("WatchStateLoggerPlugin", function (compilation, callback) {
callback();
if (plugin.isRunningWatching) {
console.log(messages.startWatching);
} else {
console.log(messages.compilationComplete);
}
let emittedFiles = Object
.keys(compilation.assets)
.filter(assetKey => compilation.assets[assetKey].emitted);
const webpackRuntimeFiles = getWebpackRuntimeOnlyFiles(compilation);
const entryPointFiles = getEntryPointFiles(compilation);
process.send && process.send(messages.compilationComplete, error => null);
// Send emitted files so they can be LiveSynced if need be
process.send && process.send({ emittedFiles, webpackRuntimeFiles, entryPointFiles }, error => null);
});
}
}
function getWebpackRuntimeOnlyFiles(compilation) {
let runtimeOnlyFiles = [];
try {
runtimeOnlyFiles = [].concat(...Array.from<any>(compilation.entrypoints.values())
.map(entrypoint => entrypoint.runtimeChunk)
// filter embedded runtime chunks (e.g. part of bundle.js or inspector-modules.js)
.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);
} catch (e) {
// breaking change in the Webpack API
console.log("Warning: Unable to find Webpack runtime files.");
}
return runtimeOnlyFiles;
}
function getEntryPointFiles(compilation) {
const entryPointFiles = [];
try {
Array.from(compilation.entrypoints.values())
.forEach((entrypoint: any) => {
for (const entryChunk of entrypoint.chunks) {
entryChunk.files.forEach(fileName => {
if (fileName.indexOf("hot-update") === -1) {
entryPointFiles.push(fileName);
}
});
}
});
} catch (e) {
console.log("Warning: Unable to find Webpack entry point files.");
}
return entryPointFiles
.filter((value, index, self) => self.indexOf(value) === index); // get only the unique files
}