1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const { HotModuleReplacementPlugin, version } = require('webpack');
|
12 |
|
13 | const { getMajorVersion } = require('../helpers');
|
14 |
|
15 | const { PluginExistsError } = require('../errors');
|
16 |
|
17 | const addPlugin = (compiler) => {
|
18 | const hmrPlugin = new HotModuleReplacementPlugin();
|
19 | hmrPlugin.apply(compiler);
|
20 | };
|
21 |
|
22 | const init = function init(compiler, log) {
|
23 | const webpackMajorVersion = getMajorVersion(version);
|
24 |
|
25 | compiler.options.output = Object.assign(compiler.options.output, {
|
26 | hotUpdateChunkFilename: `${compiler.wpsId}-[id]-wps-hmr.js`,
|
27 | hotUpdateMainFilename:
|
28 | webpackMajorVersion >= 5
|
29 | ? `[runtime]-${compiler.wpsId}-wps-hmr.json`
|
30 | : `${compiler.wpsId}-wps-hmr.json`
|
31 | });
|
32 |
|
33 | const hasHMRPlugin = compiler.options.plugins.some(
|
34 | (plugin) => plugin instanceof HotModuleReplacementPlugin
|
35 | );
|
36 |
|
37 |
|
38 | if (!hasHMRPlugin) {
|
39 | addPlugin(compiler);
|
40 | } else {
|
41 | log.error(
|
42 | 'webpack-plugin-serve adds HotModuleReplacementPlugin automatically. Please remove it from your config.'
|
43 | );
|
44 | throw new PluginExistsError(
|
45 | 'HotModuleReplacementPlugin exists in the specified configuration.'
|
46 | );
|
47 | }
|
48 | };
|
49 |
|
50 | module.exports = { init };
|