UNPKG

1.61 kBJavaScriptView Raw
1/*
2 Copyright © 2018 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11const { HotModuleReplacementPlugin, version } = require('webpack');
12
13const { getMajorVersion } = require('../helpers');
14
15const { PluginExistsError } = require('../errors');
16
17const addPlugin = (compiler) => {
18 const hmrPlugin = new HotModuleReplacementPlugin();
19 hmrPlugin.apply(compiler);
20};
21
22const init = function init(compiler, log) {
23 const webpackMajorVersion = getMajorVersion(version);
24 // eslint-disable-next-line no-param-reassign
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 /* istanbul ignore else */
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
50module.exports = { init };