Skip to content

Commit e0a46da

Browse files
bebrawshellscape
andauthored
fix: webpack v5 support (#22)
* fix: Fix webpack 5 watch related warning Closes #40. * chore: Add webpack 5 checks * Update lib/compiler.js Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com> * fix: Move function earlier * chore: update nvmrc, add v5 test scripts * fix: Fix tests I kept it simple and extracted a subset that's the same for 4 and 5. * fix: Fix cleanup * Update package.json Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com> * chore: Simplify tests I found a way to run snapshots for both 4 and 5 in the same file. * chore: Drop a redundant snapshot * chore: Update snapshots Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com> Co-authored-by: shellscape <andrew@shellscape.org>
1 parent 4ce53a4 commit e0a46da

File tree

6 files changed

+146
-9
lines changed

6 files changed

+146
-9
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.14.1
1+
14

lib/compiler.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111
const chalk = require('chalk');
1212
const webpack = require('webpack');
1313

14+
const isWebpack5 = () => webpack.version.split('.')[0] === '5';
15+
1416
const run = ({ config, emitJson, watchConfig }, log) => {
17+
// Delete possible watch flag from webpack configuration as that triggers a
18+
// faulty condition in webpack 5. #20
19+
if (isWebpack5()) {
20+
// eslint-disable-next-line no-param-reassign
21+
delete config.watch;
22+
}
23+
1524
let lastHash;
1625
const compiler = webpack(config);
1726

@@ -25,7 +34,8 @@ const run = ({ config, emitJson, watchConfig }, log) => {
2534
return;
2635
}
2736

28-
if (lastHash === stats.hash) {
37+
// Skip duplicate hash check in webpack 5. #20
38+
if (!isWebpack5() && lastHash === stats.hash) {
2939
log.info(chalk`{dim ⁿᵃⁿᵒ} Duplicate build detected {dim (${lastHash})}\n`);
3040
return;
3141
}
@@ -60,4 +70,4 @@ const run = ({ config, emitJson, watchConfig }, log) => {
6070
}
6171
};
6272

63-
module.exports = { run };
73+
module.exports = { isWebpack5, run };

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"lint": "eslint --fix --cache bin/* lib test argv.js",
2424
"lint-staged": "lint-staged",
2525
"security": "npm audit",
26-
"test": "ava"
26+
"test": "npm run test:v4 && npm run test:v5 && npm run test:cleanup",
27+
"test:cleanup": "npm install webpack@^4.42.0 --no-save",
28+
"test:v4": "ava",
29+
"test:v5": "npm install webpack@^5.0.0 --no-save && ava"
2730
},
2831
"files": [
2932
"bin/",
@@ -34,7 +37,7 @@
3437
"LICENSE"
3538
],
3639
"peerDependencies": {
37-
"webpack": "^4.20.2"
40+
"webpack": ">=4.0.0"
3841
},
3942
"dependencies": {
4043
"chalk": "^4.1.0",

test/snapshots/test.js.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,96 @@ Generated by [AVA](https://avajs.dev).
3737
_: [],
3838
}
3939

40-
## json
40+
## json - webpack 5
41+
42+
> Snapshot 1
43+
44+
{
45+
assets: [
46+
{
47+
auxiliaryChunkIdHints: [],
48+
auxiliaryChunkNames: [],
49+
auxiliaryChunks: [],
50+
cached: false,
51+
chunkIdHints: [],
52+
chunkNames: [
53+
'main',
54+
],
55+
chunks: [
56+
'main',
57+
],
58+
comparedForEmit: true,
59+
emitted: false,
60+
info: {
61+
size: 2196,
62+
},
63+
isOverSizeLimit: false,
64+
name: 'main.js',
65+
related: {},
66+
size: 2196,
67+
type: 'asset',
68+
},
69+
],
70+
assetsByChunkName: {
71+
main: [
72+
'main.js',
73+
],
74+
},
75+
children: [],
76+
entrypoints: {
77+
main: {
78+
assets: [
79+
{
80+
name: 'main.js',
81+
size: 2196,
82+
},
83+
],
84+
assetsSize: 2196,
85+
auxiliaryAssets: [],
86+
auxiliaryAssetsSize: 0,
87+
childAssets: {},
88+
children: {},
89+
chunks: [
90+
'main',
91+
],
92+
filteredAssets: 0,
93+
filteredAuxiliaryAssets: 0,
94+
isOverSizeLimit: false,
95+
name: 'main',
96+
},
97+
},
98+
errors: [],
99+
errorsCount: 0,
100+
hash: '30f993fdf21d4d672d17',
101+
namedChunkGroups: {
102+
main: {
103+
assets: [
104+
{
105+
name: 'main.js',
106+
size: 2196,
107+
},
108+
],
109+
assetsSize: 2196,
110+
auxiliaryAssets: [],
111+
auxiliaryAssetsSize: 0,
112+
childAssets: {},
113+
children: {},
114+
chunks: [
115+
'main',
116+
],
117+
filteredAssets: 0,
118+
filteredAuxiliaryAssets: 0,
119+
isOverSizeLimit: false,
120+
name: 'main',
121+
},
122+
},
123+
publicPath: 'auto',
124+
version: '5.1.3',
125+
warnings: [],
126+
warningsCount: 0,
127+
}
128+
129+
## json - webpack 4
41130

42131
> Snapshot 1
43132

test/snapshots/test.js.snap

735 Bytes
Binary file not shown.

test/test.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const test = require('ava');
44
const execa = require('execa');
55
const deferred = require('p-defer');
66

7+
const { isWebpack5 } = require('../lib/compiler');
8+
79
const bin = resolve(__dirname, '../bin/wp.js');
810
const cwd = resolve(__dirname, './fixtures');
911

@@ -52,7 +54,8 @@ test('bad', async (t) => {
5254
try {
5355
await run('--config', 'bad.config.js');
5456
} catch (err) {
55-
t.truthy(err.stderr.includes('WebpackOptionsValidationError'));
57+
// Same subset for webpack 4 and 5
58+
t.truthy(err.stderr.includes('ValidationError'));
5659
}
5760
});
5861

@@ -98,13 +101,45 @@ test('multi', async (t) => {
98101

99102
test('stats', async (t) => {
100103
const { stderr } = await run('--config', 'stats.config.js');
101-
t.snapshot(stderr);
104+
105+
if (isWebpack5()) {
106+
t.truthy(stderr.includes('webpack: 1 asset'));
107+
} else {
108+
// Webpack 4
109+
t.snapshot(stderr);
110+
}
102111
});
103112

104-
test('json', async (t) => {
113+
test('json - webpack 4', async (t) => {
105114
const { stdout } = await run('--config', 'stats.config.js', '--json');
106115
const stats = JSON.parse(stdout);
107116

117+
if (isWebpack5()) {
118+
t.truthy(true);
119+
120+
return;
121+
}
122+
123+
// Remove times since those are transient
124+
delete stats.time;
125+
delete stats.builtAt;
126+
delete stats.outputPath;
127+
delete stats.chunks;
128+
delete stats.modules;
129+
130+
t.snapshot(stats);
131+
});
132+
133+
test('json - webpack 5', async (t) => {
134+
const { stdout } = await run('--config', 'stats.config.js', '--json');
135+
const stats = JSON.parse(stdout);
136+
137+
if (!isWebpack5()) {
138+
t.truthy(true);
139+
140+
return;
141+
}
142+
108143
// Remove times since those are transient
109144
delete stats.time;
110145
delete stats.builtAt;

0 commit comments

Comments
 (0)