Skip to content

Commit a3c0134

Browse files
feat: add support for webpack@5 and html-webpack-plugin@5 (#187)
Co-authored-by: Simen Bekkhus <[email protected]>
1 parent 77e5de2 commit a3c0134

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

.github/workflows/node.js.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
node-version: [6.x, 8.x, 10.x, 12.x, 14.x, 15.x]
18-
html-plugin-version: [3, 4]
19-
webpack-version: [4]
18+
html-plugin-version: [3, 4, 5]
19+
webpack-version: [4, 5]
20+
exclude:
21+
# plugin@5 only supports webpack@5
22+
- html-plugin-version: 5
23+
webpack-version: 4
24+
# plugin@3 only supports webpack@4
25+
- html-plugin-version: 3
26+
webpack-version: 5
27+
# webpack@5 and plugin@5 does not support node 6 or 8
28+
- node-version: 6.x
29+
webpack-version: 5
30+
- node-version: 6.x
31+
html-plugin-version: 5
32+
- node-version: 8.x
33+
webpack-version: 5
34+
- node-version: 8.x
35+
html-plugin-version: 5
2036
include:
2137
- node-version: 10.x
2238
install-puppeteer: true

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"eslint-config-simenb-base": "^15.0.1",
5555
"eslint_d": "^7.1.0",
5656
"express": "^4.17.1",
57-
"html-webpack-plugin": "^4.0.0-0",
57+
"html-webpack-plugin": "^4.0.0",
5858
"husky": "^1.0.1",
5959
"jest": "^24.0.0",
6060
"jest-watch-typeahead": "^0.2.0",
@@ -67,8 +67,8 @@
6767
"webpack-cli": "^3.1.0"
6868
},
6969
"peerDependencies": {
70-
"html-webpack-plugin": "^3.0.4 || ^4.0.0-0",
71-
"webpack": "^4.0.0"
70+
"html-webpack-plugin": "^3.0.4 || ^4.0.0-0 || ^5.0.0",
71+
"webpack": "^4.0.0 || ^5.0.0"
7272
},
7373
"engines": {
7474
"node": ">=6"

src/index.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,41 @@ import pEachSeries from 'p-each-series';
33
import micromatch from 'micromatch';
44
import crypto from 'crypto';
55
import globby from 'globby';
6+
import path from 'path';
7+
import webpack from 'webpack';
68
import {
79
ensureTrailingSlash,
10+
fsReadFileAsync,
811
handleUrl,
912
resolveOutput,
1013
resolvePublicPath,
1114
} from './utils';
1215

16+
/* istanbul ignore next: webpack 5 not in unit test mocks */
17+
/**
18+
* Pushes the content of the given filename to the compilation assets
19+
* @param {string} filename
20+
* @param {WebpackCompilation} compilation
21+
* @returns {Promise<string>} file basename
22+
*/
23+
function addFileToAssetsWebpack5(filename, compilation) {
24+
const resolvedFilename = path.resolve(compilation.compiler.context, filename);
25+
26+
return fsReadFileAsync(resolvedFilename)
27+
.then(source => new webpack.sources.RawSource(source, true))
28+
.catch(() =>
29+
Promise.reject(
30+
new Error(`HtmlWebpackPlugin: could not load file ${resolvedFilename}`),
31+
),
32+
)
33+
.then(rawSource => {
34+
const basename = path.basename(resolvedFilename);
35+
compilation.fileDependencies.add(resolvedFilename);
36+
compilation.emitAsset(basename, rawSource);
37+
return basename;
38+
});
39+
}
40+
1341
export default class AddAssetHtmlPlugin {
1442
constructor(assets = []) {
1543
this.assets = Array.isArray(assets) ? assets.slice().reverse() : [assets];
@@ -22,7 +50,7 @@ export default class AddAssetHtmlPlugin {
2250
let beforeGenerationHook;
2351
let alterAssetTagsHook;
2452

25-
if (HtmlWebpackPlugin.version === 4) {
53+
if (HtmlWebpackPlugin.version >= 4) {
2654
const hooks = HtmlWebpackPlugin.getHooks(compilation);
2755

2856
beforeGenerationHook = hooks.beforeAssetTagGeneration;
@@ -105,10 +133,11 @@ export default class AddAssetHtmlPlugin {
105133
}
106134
}
107135

108-
const addedFilename = await htmlPluginData.plugin.addFileToAssets(
109-
filepath,
110-
compilation,
111-
);
136+
const addFileToAssets =
137+
htmlPluginData.plugin.addFileToAssets ||
138+
/* istanbul ignore next: webpack 5 not in unit test mocks */ addFileToAssetsWebpack5;
139+
140+
const addedFilename = await addFileToAssets(filepath, compilation);
112141

113142
let suffix = '';
114143
if (hash) {
@@ -133,7 +162,7 @@ export default class AddAssetHtmlPlugin {
133162
const relatedFiles = await globby(`${filepath}.*`);
134163
await Promise.all(
135164
relatedFiles.sort().map(async relatedFile => {
136-
const addedMapFilename = await htmlPluginData.plugin.addFileToAssets(
165+
const addedMapFilename = await addFileToAssets(
137166
relatedFile,
138167
compilation,
139168
);

src/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import globby from 'globby';
3+
import fs from 'fs';
34

45
export function ensureTrailingSlash(string) {
56
if (string.length && string.substr(-1, 1) !== '/') {
@@ -30,6 +31,19 @@ export function resolveOutput(compilation, addedFilename, outputPath) {
3031
}
3132
}
3233

34+
/* istanbul ignore next: webpack 5 not in unit test mocks */
35+
export function fsReadFileAsync(filePath) {
36+
return new Promise((resolve, reject) => {
37+
fs.readFile(filePath, (err, res) => {
38+
if (err) {
39+
reject(err);
40+
} else {
41+
resolve(res);
42+
}
43+
});
44+
});
45+
}
46+
3347
/**
3448
* handle globby filepath and return an array with all matched assets.
3549
*

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3869,7 +3869,7 @@ html-minifier-terser@^5.0.1:
38693869
relateurl "^0.2.7"
38703870
terser "^4.6.3"
38713871

3872-
html-webpack-plugin@^4.0.0-0:
3872+
html-webpack-plugin@^4.0.0:
38733873
version "4.5.2"
38743874
resolved "https://registry.yarnpkg.com/html-webpack-plugin/-/html-webpack-plugin-4.5.2.tgz#76fc83fa1a0f12dd5f7da0404a54e2699666bc12"
38753875
integrity sha512-q5oYdzjKUIPQVjOosjgvCHQOv9Ett9CYYHlgvJeXG0qQvdSojnBq4vAdQBwn1+yGveAwHCoe/rMR86ozX3+c2A==

0 commit comments

Comments
 (0)