Skip to content

Commit 50536c4

Browse files
committed
fix: add Exception when used splitChunks and occurs the error: cannot resolve a CSS file in template
1 parent 165de55 commit 50536c4

33 files changed

Lines changed: 350 additions & 14 deletions

File tree

CHANGELOG.md

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

3+
## 4.4.2 (2024-11-18)
4+
5+
- fix: add Exception when used `splitChunks` and occurs the error: Can't resolve a CSS file in template.
6+
- fix: correct Exception message when a source CSS file is not found
7+
38
## 4.4.1 (2024-11-05)
49

510
- fix: if used multiple config and cache `filesystem`, occurs the error 'PersistentCache is already registered'

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ module.exports = {
19331933
splitChunks: {
19341934
cacheGroups: {
19351935
scripts: {
1936-
test: /\.(js|ts)$/, // <= IMPORTANT: split only JS files
1936+
test: /\.(js|ts)$/, // <= IMPORTANT: split only script files
19371937
chunks: 'all',
19381938
},
19391939
},
@@ -6217,7 +6217,7 @@ module.exports = {
62176217
splitChunks: {
62186218
cacheGroups: {
62196219
scripts: {
6220-
test: /\.(js|ts)$/,
6220+
test: /\.(js|ts)$/, // <= IMPORTANT: split only script files
62216221
chunks: 'all',
62226222
},
62236223
},
@@ -6262,7 +6262,7 @@ module.exports = {
62626262
splitChunks: {
62636263
cacheGroups: {
62646264
vendor: {
6265-
test: /[\\/]node_modules[\\/].+\.(js|ts)$/, // use exactly this Regexp
6265+
test: /[\\/]node_modules[\\/].+\.(js|ts)$/, // <= IMPORTANT: split only script files
62666266
name: 'vendor',
62676267
chunks: 'all',
62686268
},
@@ -6334,8 +6334,8 @@ module.exports = {
63346334
maxSize: 1000000, // split chunks bigger than 100KB, defaults is 20KB
63356335
cacheGroups: {
63366336
app: {
6337-
test: /\.(js|ts)$/, // split only JS files
6338-
chunks: 'all', // <- use it only in cache groups
6337+
test: /\.(js|ts)$/, // <= IMPORTANT: split only script files
6338+
chunks: 'all', // <= use it only in cache groups
63396339
name({ context }, chunks, groupName) {
63406340
// save split chunks of the node module under package name
63416341
if (/[\\/]node_modules[\\/]/.test(context)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.4.1",
3+
"version": "4.4.2",
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",

src/Plugin/Messages/Exception.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ const optionPreloadAsException = (config, type, availableTypes) => {
7474
* @param {string} file The unresolved file can be absolute or relative.
7575
* @param {string} issuer The absolute issuer file of unresolved file.
7676
* @param {string} rootContext The absolute path to project files.
77+
* @param {object} pluginOptions The instance of the pluginOptions.
7778
* @throws {Error}
7879
*/
79-
const resolveException = (file, issuer, rootContext) => {
80+
const resolveException = (file, issuer, rootContext, pluginOptions) => {
8081
let isExistsFile = true;
8182
issuer = path.relative(rootContext, issuer);
8283

@@ -88,7 +89,7 @@ const resolveException = (file, issuer, rootContext) => {
8889

8990
if (!isExistsFile) {
9091
message = `File ${yellow(file)} not found in ${cyan(issuer)}`;
91-
} else if (/\.css$/.test(file) && file.indexOf('??ruleSet')) {
92+
} else if (/\.css$/.test(file) && file.includes('??ruleSet')) {
9293
message +=
9394
`\nThe handling of ${yellow`@import at-rules in CSS`} is not supported. Disable the 'import' option in 'css-loader':\n` +
9495
white`
@@ -98,16 +99,50 @@ const resolveException = (file, issuer, rootContext) => {
9899
{
99100
loader: 'css-loader',
100101
options: {
101-
import: false, // disable @import at-rules handling
102+
import: false, ${white`// disable @import at-rules handling`}
102103
},
103104
},
104105
],
105106
},`;
107+
} else if (pluginOptions.isStyle(file) && hasSplitChunksCacheGroups(pluginOptions.webpackOptions)) {
108+
message += `\n
109+
${whiteBright.bgGreen` Tip `}
110+
Add the ${white`'splitChunks.cacheGroups.{cacheGroup}.test'`} option as a RegExp to each cache group to split only script files, excluding styles.
111+
For example:
112+
113+
optimization: {
114+
splitChunks: {
115+
minSize: 1000,
116+
cacheGroups: {
117+
default: {
118+
test: /.+\.(js|ts)$/, ${white`// split only scripts, excluding style files`}
119+
name: 'common',
120+
chunks: 'all',
121+
},
122+
vendors: {
123+
test: /[\\/]node_modules[\\/].+\.(js|ts)$/, ${white`// split only scripts, excluding style files`}
124+
name: 'vendor',
125+
chunks: 'all',
126+
},
127+
},
128+
},
129+
},
130+
`;
106131
}
107132

108133
throw new PluginException(message);
109134
};
110135

136+
/**
137+
* Detect whether Webpack config contains `splitChunks.cacheGroups` option.
138+
*
139+
* @param webpackOptions
140+
* @return {boolean}
141+
*/
142+
const hasSplitChunksCacheGroups = (webpackOptions) => {
143+
return webpackOptions?.optimization?.splitChunks?.cacheGroups != null;
144+
};
145+
111146
/**
112147
* @param {Error} error
113148
* @param {string} sourceFile

src/Plugin/Resolver.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ class Resolver {
222222
const { issuer, issuerFile } = this;
223223

224224
// @import CSS rule is not supported
225-
if (rawRequest.indexOf('??ruleSet') > 0) {
226-
resolveException(rawRequest, issuer.resource, this.rootContext);
225+
if (rawRequest.includes('??ruleSet')) {
226+
resolveException(rawRequest, issuer.resource, this.rootContext, this.pluginOption);
227227
}
228228
// bypass the asset contained data-URL
229229
if (this.assetInline.isDataUrl(rawRequest)) return rawRequest;
@@ -249,6 +249,7 @@ class Resolver {
249249
}
250250

251251
const assetFile = this.resolveAsset(resource);
252+
252253
if (assetFile != null) return assetFile;
253254

254255
// try to resolve inline data url
@@ -266,13 +267,13 @@ class Resolver {
266267
if (this.pluginOption.js.test.test(file) && this.assetEntry.isEntryResource(issuer.resource)) {
267268
// occur after rename/delete of a js file when the entry module was already rebuilt
268269
Snapshot.addMissingFile(issuer.resource, file);
269-
resolveException(file, issuer.resource, this.rootContext);
270+
resolveException(file, issuer.resource, this.rootContext, this.pluginOption);
270271
}
271272

272273
// require a native JavaScript, JSON or css-loader API file
273274
if (/\.js[a-z0-9]*$/i.test(resource)) return require(resource);
274275

275-
resolveException(rawRequest, issuer.resource, this.rootContext);
276+
resolveException(rawRequest, issuer.resource, this.rootContext, this.pluginOption);
276277
}
277278

278279
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>About</title>
5+
<link href="css/style.2eb0c66b.css" rel="stylesheet">
6+
<script src="js/main.7693a3d5.js" defer="defer"></script>
7+
</head>
8+
<body>
9+
<h1>About</h1>
10+
</body>
11+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
font-family: Arial, Helvetica, sans-serif;
3+
}
4+
5+
h1 {
6+
color: orangered;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
<link href="css/style.2eb0c66b.css" rel="stylesheet">
6+
<script src="js/main.7693a3d5.js" defer="defer"></script>
7+
</head>
8+
<body>
9+
<h1>Home</h1>
10+
</body>
11+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(">> main.js");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>About</title>
5+
<link href="./css/style.scss" rel="stylesheet">
6+
<script src="./js/main.js" defer="defer"></script>
7+
</head>
8+
<body>
9+
<h1>About</h1>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)