-
Notifications
You must be signed in to change notification settings - Fork 5
/
WatchHtmlPlugin.js
127 lines (115 loc) · 3.26 KB
/
WatchHtmlPlugin.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const HtmlWebpackPlugin = require("html-webpack-plugin");
const colors = require("ansi-colors");
const chokidar = require("chokidar");
const spawn = require("cross-spawn");
const path = require("path");
const glob = require("glob");
module.exports = class WatchHtmlPlugin {
constructor(options) {
this.options = options;
this.initialFiles = new Set();
this.watcher = null;
this.browserSyncInstance = null;
this.serverProcess = null;
this.restartTimeout = null;
}
apply(compiler) {
this.initializeFiles();
this.addInitialHtmlPlugins(compiler);
this.browserSyncInstance = this.findBrowserSyncInstance(compiler);
compiler.hooks.watchRun.tapAsync(
"WatchHtmlPlugin",
(compilation, callback) => {
if (!this.watcher) this.setupFileWatcher();
callback();
}
);
}
initializeFiles() {
glob
.sync(`${this.options.srcDir}/**/*.html`)
.forEach((file) => this.initialFiles.add(path.resolve(file)));
}
addInitialHtmlPlugins(compiler) {
this.createHtmlPlugins(Array.from(this.initialFiles)).forEach((plugin) =>
plugin.apply(compiler)
);
}
createHtmlPlugins(files) {
return files.map(
(filePath) =>
new HtmlWebpackPlugin({
...this.options.htmlPluginOptions,
template: filePath,
filename: path
.relative(this.options.srcDir, filePath)
.replace(/\\/g, "/"),
})
);
}
findBrowserSyncInstance(compiler) {
return (
compiler.options.plugins?.find(
(plugin) =>
plugin.browserSync && typeof plugin.browserSync.exit === "function"
)?.browserSync || null
);
}
setupFileWatcher() {
this.watcher = chokidar.watch(`${this.options.srcDir}/*.html`, {
ignoreInitial: true,
});
this.watcher.on("add", this.debounceRestart.bind(this));
this.watcher.on("unlink", this.debounceRestart.bind(this));
this.watcher.on("error", (error) =>
console.error(
colors.bgRed.whiteBright(`[WatchHtmlPlugin] Watcher error: ${error}`)
)
);
}
stopBrowserSync() {
return this.browserSyncInstance
? new Promise((resolve) => {
this.browserSyncInstance.exit();
this.browserSyncInstance = null;
resolve();
})
: Promise.resolve();
}
stopServerProcess() {
return this.serverProcess
? new Promise((resolve) => {
this.serverProcess.on("exit", () => {
this.serverProcess = null;
resolve();
});
this.serverProcess.kill("SIGTERM");
})
: Promise.resolve();
}
debounceRestart() {
clearTimeout(this.restartTimeout);
this.restartTimeout = setTimeout(() => this.restartDevServer(), 300);
}
restartDevServer() {
console.log(
colors.bgRed.whiteBright.bold(
"[WatchHtmlPlugin] Restarting the development server..."
)
);
Promise.all([this.stopBrowserSync(), this.stopServerProcess()])
.then(() => {
this.serverProcess = spawn("npm", ["run", "start"], {
stdio: "inherit",
});
})
.catch((error) =>
console.error(
colors.bgRed.whiteBright(
"[WatchHtmlPlugin] Error during restart:",
error
)
)
);
}
};