Skip to content

Commit 8bccec8

Browse files
authored
fix: HotModuleReplacementPlugin warning message on webpack v5 (#206)
* fix hotUpdateMainFilename webpack's error message * fix tests * add getMajorVersion
1 parent 572c1d2 commit 8bccec8

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/helpers.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
getMajorVersion: (version) =>
3+
typeof version === 'string' && version.includes('.') ? version.split('.')[0] : false
4+
};

lib/plugins/hmr.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
The above copyright notice and this permission notice shall be
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
11-
const { HotModuleReplacementPlugin } = require('webpack');
11+
const { HotModuleReplacementPlugin, version } = require('webpack');
12+
13+
const { getMajorVersion } = require('../helpers');
1214

1315
const { PluginExistsError } = require('../errors');
1416

@@ -18,10 +20,17 @@ const addPlugin = (compiler) => {
1820
};
1921

2022
const init = function init(compiler, log) {
23+
const webpackMajorVersion = getMajorVersion(version);
2124
// eslint-disable-next-line no-param-reassign
2225
compiler.options.output = Object.assign(compiler.options.output, {
23-
hotUpdateChunkFilename: `${compiler.wpsId}-[id]-wps-hmr.js`,
24-
hotUpdateMainFilename: `${compiler.wpsId}-wps-hmr.json`
26+
hotUpdateChunkFilename:
27+
webpackMajorVersion >= 5
28+
? `[runtime]-${compiler.wpsId}-[id]-wps-hmr.js`
29+
: `${compiler.wpsId}-[id]-wps-hmr.js`,
30+
hotUpdateMainFilename:
31+
webpackMajorVersion >= 5
32+
? `[runtime]-${compiler.wpsId}-wps-hmr.json`
33+
: `${compiler.wpsId}-wps-hmr.json`
2534
});
2635

2736
const hasHMRPlugin = compiler.options.plugins.some(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"ci:coverage": "nyc npm run test && nyc report --reporter=text-lcov > coverage.lcov",
1717
"ci:lint": "npm run lint && npm run security",
1818
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
19-
"ci:test": "npm run test -- --verbose",
19+
"ci:test": "npm run test -- --verbose --timeout=30000",
2020
"commitlint": "commitlint",
2121
"commitmsg": "commitlint -e $GIT_PARAMS",
2222
"dev": "npm run dev:clean && node node_modules/webpack-nano/bin/wp --config test/fixtures/simple/webpack.config.js",

test/helpers.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const test = require('ava');
2+
3+
const { getMajorVersion } = require('../lib/helpers');
4+
5+
test('Get major version with correct value', (t) => {
6+
const majorVersion = getMajorVersion('5.2.3');
7+
t.true(majorVersion === '5');
8+
});
9+
10+
test('Get major version with incorrect value', (t) => {
11+
const majorVersion = getMajorVersion('5');
12+
t.true(majorVersion === false);
13+
});

0 commit comments

Comments
 (0)