Skip to content

Commit 16936f3

Browse files
committed
feat: add support for Webpack v5.96
1 parent d32655c commit 16936f3

File tree

154 files changed

+1729
-2882
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+1729
-2882
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Change log
22

3+
## 4.2.0 (2024-11-03)
4+
5+
- feat: add support for Webpack `>= 5.96` to correct inline images into CSS and HTML
6+
WARNING: Webpack version `5.96.0` introduces the BREAKING CHANGE in the `CodeGenerationResults` class!
7+
- feat: add support for Webpack `>= 5.96` to correct CSS lazy loading
8+
WARNING: Webpack version `5.96.0` introduces the BREAKING CHANGE in the `AssetGenerator` class!
9+
- chore: update package and devel dependencies
10+
- test: update tests
11+
312
## 4.1.4 (2024-11-01)
413

514
- chore: update dependencies

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ If you have discovered a bug or have a feature suggestion, feel free to create a
208208
## 🔆 What's New in v4
209209

210210
- **NEW** added supports the [multiple configurations](https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations).
211-
- **SUPPORTS** Node.js version `18+`
212-
- **SUPPORTS** Webpack version `5.81+`
211+
- **SUPPORTS** Webpack version `5.96+` (since `v4.2.0`).
212+
- **SUPPORTS** Webpack version `5.81+` (since `v4.0.0`).
213+
- **SUPPORTS** Node.js version `18+`.
213214
- **BREAKING CHANGES** see in the [changelog](https://github.com/webdiscus/html-bundler-webpack-plugin/blob/master/CHANGELOG.md#v4-0-0).
214215

215216
## 🔆 What's New in v3
@@ -235,8 +236,11 @@ For full release notes see the [changelog](https://github.com/webdiscus/html-bun
235236

236237
### Cache type
237238

238-
The current version works stable with `cache.type` as `'memory'` (Webpack's default setting).\
239-
Support for the `'filesystem'` cache type is experimental.
239+
The current version works stably with `cache.type` as `'memory'` (Webpack's default setting).
240+
241+
Support for the `'filesystem'` cache type is in beta.
242+
It works stably in standard use cases, but it cannot be guaranteed to work in all use cases.
243+
If you have any problems, feel free to create an [issue](https://github.com/webdiscus/html-bundler-webpack-plugin/issues).
240244

241245
---
242246

package-lock.json

Lines changed: 1205 additions & 2732 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.1.4",
3+
"version": "4.2.0",
44
"description": "HTML Bundler Plugin for Webpack renders HTML templates containing source files of scripts, styles, images. Supports template engines: Eta, EJS, Handlebars, Nunjucks, Pug, TwigJS. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",
@@ -145,12 +145,12 @@
145145
"@types/html-minifier-terser": "^7.0.2",
146146
"ansis": "3.3.2",
147147
"enhanced-resolve": ">=5.7.0",
148-
"eta": "^3.4.1",
148+
"eta": "^3.5.0",
149149
"html-minifier-terser": "^7.2.0"
150150
},
151151
"devDependencies": {
152-
"@babel/core": "^7.25.2",
153-
"@babel/preset-env": "^7.25.4",
152+
"@babel/core": "^7.26.0",
153+
"@babel/preset-env": "^7.26.0",
154154
"@emotion/react": "11.13.0",
155155
"@emotion/styled": "11.13.0",
156156
"@mui/material": "5.16.7",
@@ -160,23 +160,23 @@
160160
"@test-fixtures/scss": "0.0.7",
161161
"@test/html-bundler-webpack-plugin": "file:./",
162162
"@test/import-css": "file:./test/fixtures/node_modules/import-css/",
163-
"@types/jest": "^29.5.12",
164-
"@types/react-dom": "^18.3.0",
163+
"@types/jest": "^29.5.14",
164+
"@types/react-dom": "^18.3.1",
165165
"copy-webpack-plugin": "9.1.0",
166166
"css-loader": "^7.1.2",
167167
"css-minimizer-webpack-plugin": "^7.0.0",
168-
"cssnano": "^7.0.5",
168+
"cssnano": "^7.0.6",
169169
"ejs": "^3.1.10",
170170
"favicons": "7.2.0",
171171
"handlebars": "^4.7.8",
172172
"handlebars-layouts": "^3.1.4",
173173
"jest": "^29.7.0",
174-
"liquidjs": "^10.17.0",
174+
"liquidjs": "^10.18.0",
175175
"markdown-it": "^14.1.0",
176176
"mustache": "^4.2.0",
177177
"normalize.css": "^8.0.1",
178178
"nunjucks": "^3.2.4",
179-
"parse5": "^7.1.2",
179+
"parse5": "^7.2.1",
180180
"postcss-loader": "^8.1.1",
181181
"prettier": "^3.3.3",
182182
"prismjs": "^1.29.0",
@@ -185,17 +185,17 @@
185185
"react-dom": "18.3.1",
186186
"responsive-loader": "^3.1.2",
187187
"rtlcss": "^4.3.0",
188-
"sass": "1.77.8",
189-
"sass-loader": "15.0.0",
188+
"sass": "1.80.6",
189+
"sass-loader": "16.0.3",
190190
"sharp": "^0.33.5",
191191
"svgo-loader": "^4.0.0",
192192
"ts-loader": "9.5.1",
193193
"tsconfig-paths-webpack-plugin": "^4.1.0",
194194
"twig": "^1.17.1",
195-
"typescript": "5.5.4",
196-
"vue": "3.5.3",
195+
"typescript": "5.6.3",
196+
"vue": "3.5.12",
197197
"vue-loader": "^17.4.2",
198-
"webpack": "^5.95.0",
198+
"webpack": "5.96.1",
199199
"webpack-cli": "5.1.4",
200200
"webpack-dev-server": "^5.1.0"
201201
}

src/Common/Helpers.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,67 @@ const replaceAll = (str, search, replace) => {
130130
return str.replace(new RegExp(search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
131131
};
132132

133+
/**
134+
* Parse version string including leading compare chars.
135+
* For example: '=5.96.1', '>5.96.1', '< 5.96.1', '<= 5.96.1', >= 5.96.1'
136+
*
137+
* @param version
138+
* @return {[compare: string, version: string]}
139+
*/
140+
const parseVersion = (version) => {
141+
let i;
142+
for (i = 0; i < version.length; i++) {
143+
let char = version.codePointAt(i);
144+
if (char >= 48 && char <= 57) {
145+
break;
146+
}
147+
}
148+
let compare = version.slice(0, i);
149+
compare = compare.trim();
150+
151+
return [compare, version.slice(i)];
152+
};
153+
154+
/**
155+
* Compare two semantic versions.
156+
*
157+
* @param {string} version1
158+
* @param {string} compare One of: `=`, `<`, `>`, `<=`, `>=`
159+
* @param {string} version2
160+
* @return {boolean}
161+
*/
162+
const compareVersions = (version1, compare, version2) => {
163+
const sortVersions = (x, v = (s) => s.match(/[a-z]|\d+/g).map((c) => (c == ~~c ? String.fromCharCode(97 + c) : c))) =>
164+
x.sort((a, b) => ((a + b).match(/[a-z]/) ? (v(b) < v(a) ? 1 : -1) : a.localeCompare(b, 0, { numeric: true })));
165+
166+
const versions = [version1, version2];
167+
const sorted = sortVersions(versions);
168+
let result;
169+
170+
if (version1 === version2) {
171+
result = 0;
172+
} else if (sorted[0] === version1) {
173+
result = -1;
174+
} else {
175+
result = 1;
176+
}
177+
178+
switch (compare) {
179+
case '=':
180+
return result === 0;
181+
case '<':
182+
return result === -1;
183+
case '>':
184+
return result === 1;
185+
case '<=':
186+
return result === 0 || result === -1;
187+
case '>=':
188+
return result === 0 || result === 1;
189+
}
190+
191+
return false;
192+
};
193+
133194
module.exports = {
134195
isWin,
135196
isFunction,
@@ -143,4 +204,6 @@ module.exports = {
143204
detectIndent,
144205
replaceAll,
145206
outToConsole,
207+
parseVersion,
208+
compareVersions,
146209
};

src/Plugin/AssetCompiler.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Config = require('../Common/Config');
2121
const { baseUri, urlPathPrefix, cssLoaderName } = require('../Loader/Utils');
2222
const { findRootIssuer } = require('../Common/CompilationHelpers');
2323
const { isDir } = require('../Common/FileUtils');
24-
const { outToConsole } = require('../Common/Helpers');
24+
const { parseVersion, compareVersions } = require('../Common/Helpers');
2525
const createPersistentCache = require('./createPersistentCache');
2626

2727
const CssExtractModule = require('./Modules/CssExtractModule');
@@ -99,6 +99,9 @@ let RawSource;
9999
class AssetCompiler {
100100
static processAssetsPromises = [];
101101

102+
/** Whether the installed Webpack version < 5.96.0 */
103+
IS_WEBPACK_VERSION_LOWER_5_96_0 = true;
104+
102105
/** @type {Array<Promise>} */
103106
promises = [];
104107

@@ -398,6 +401,8 @@ class AssetCompiler {
398401
const { NormalModule, Compilation } = compilation.compiler.webpack;
399402
const normalModuleHooks = NormalModule.getCompilationHooks(compilation);
400403

404+
this.IS_WEBPACK_VERSION_LOWER_5_96_0 = compareVersions(compilation.compiler.webpack.version, '<', '5.96.0');
405+
401406
this.compilation = compilation;
402407
this.pluginContext.compilation = compilation;
403408
this.assetEntry.setCompilation(compilation);
@@ -778,11 +783,22 @@ class AssetCompiler {
778783

779784
// lazy load CSS in JS using `?url` query, see js-import-css-lazy-url
780785
if (meta.isImportedStyle && isUrl && !query.includes(cssLoaderName)) {
781-
const dataUrlOptions = undefined;
782786
const filename = this.pluginOption.getCss().filename;
783787

788+
if (this.IS_WEBPACK_VERSION_LOWER_5_96_0) {
789+
// Webpack <= 5.95
790+
createData.generator = new AssetGenerator(undefined, filename);
791+
} else {
792+
// Webpack >= 5.96
793+
const moduleGraph = this.compilation.moduleGraph;
794+
const dataUrl = undefined;
795+
const publicPath = undefined;
796+
const outputPath = undefined;
797+
const emit = true;
798+
createData.generator = new AssetGenerator(moduleGraph, dataUrl, filename, publicPath, outputPath, emit);
799+
}
800+
784801
createData.parser = new AssetParser(false);
785-
createData.generator = new AssetGenerator(dataUrlOptions, filename);
786802
createData.type = ASSET_MODULE_TYPE_RESOURCE;
787803
}
788804
}

src/Plugin/AssetInline.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,28 @@ class AssetInline {
224224
}
225225
} else if (item.source == null) {
226226
// data URL for binary resource
227-
const dataUrl = codeGenerationResults.getData(module, chunk.runtime, 'url').toString();
227+
const data = codeGenerationResults.getData(module, chunk.runtime, 'url');
228+
let dataUrl;
229+
230+
// note: webpack has introduced a braking change by 5.95.0 -> 5.96.0
231+
if (!data?.javascript) {
232+
// webpack <= 5.95.x: data is Buffer
233+
dataUrl = data.toString();
234+
} else if (data?.javascript) {
235+
// webpack => 5.96.0: data contains `javascript` key
236+
dataUrl = data?.javascript;
237+
238+
// remove quotes in value like "data:image/..."
239+
if (dataUrl.at(0) === '"') {
240+
dataUrl = dataUrl.slice(1, -1);
241+
}
242+
} else {
243+
// warning as svg
244+
const warning1 = 'Downgrade your webpack.';
245+
const warning2 = 'This version is not compatible.';
246+
dataUrl = `data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='40' viewBox='0 0 200 40'><text y='15'>${warning1}</text><text dy='30'>${warning2}</text></svg>`;
247+
}
248+
228249
item.source = { dataUrl };
229250
}
230251
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<!DOCTYPE html><html><head><style>h1{color:#228b22}</style>
1+
<!DOCTYPE html><html><head><style>h1{color:forestgreen}</style>
22
</head><body><h1>Home</h1><div class="my-component"><style scope="some">.my-component{color:violet}</style><div>my component</div></div></body></html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="css/main.4f0fb2f1.css"><link rel="stylesheet" href="css/style.f07588a7.css"><script src="js/main.bundle.js"></script></head><body><h4>Default layout</h4><h1>About</h1><script src="js/script.1.bundle.js"></script></body></html>
1+
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="css/main.4f0fb2f1.css"><link rel="stylesheet" href="css/style.f07588a7.css"><script src="js/main.5317c1f6.js"></script></head><body><h4>Default layout</h4><h1>About</h1><script src="js/about.e55bdc4a.js"></script></body></html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="css/main.4f0fb2f1.css"><script src="js/main.bundle.js"></script></head><body><h4>Default layout</h4><h1>Start page</h1></body></html>
1+
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="css/main.4f0fb2f1.css"><script src="js/main.5317c1f6.js"></script></head><body><h4>Default layout</h4><h1>Start page</h1></body></html>

0 commit comments

Comments
 (0)