Skip to content

Commit 0ad6fc4

Browse files
committed
fix: issue by inline a style when in the tag used single quotes for attribute
1 parent c278340 commit 0ad6fc4

19 files changed

Lines changed: 187 additions & 66 deletions

File tree

CHANGELOG.md

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

3+
## 4.4.3 (2024-11-23)
4+
5+
- fix: issue by inline a style when in the tag used single quotes for attribute
6+
37
## 4.4.2 (2024-11-18)
48

59
- fix: add Exception when used `splitChunks` and occurs the error: Can't resolve a CSS file in template.

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.2",
3+
"version": "4.4.3",
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/Common/HtmlParser.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,21 @@ const indexOfNonSpaceOrTagEnd = (content, tagName, startPos, pos = 0) => {
9696
* Whether the link tag load a style or other assets.
9797
*
9898
* <link href="style.css" type="text/css" />
99+
* <link href="style.css" rel="stylesheet" />
99100
* <link href="style.css" rel="alternate stylesheet" />
100101
* <link href="style.css" rel="preload" as="style" />
101102
* <link href="style.css" rel="preload" as="stylesheet" />
102103
*
103-
* @param {string} tag The tag with attributes.
104+
* @param {{}} attrs The tag attributes.
104105
* @return {boolean}
105106
*/
106-
const isLinkStyle = (tag) => /(?:rel|as)=".*style.*"/.test(tag) || /type="text\/css"/.test(tag);
107+
const isLinkStyle = (attrs) => {
108+
if (attrs?.type === 'text/css') return true;
109+
if (attrs?.as?.indexOf('style') > -1) return true;
110+
if (attrs?.rel?.indexOf('style') > -1) return true;
111+
112+
return false;
113+
};
107114

108115
/**
109116
* Whether the link tag load a script.
@@ -116,14 +123,13 @@ const isLinkStyle = (tag) => /(?:rel|as)=".*style.*"/.test(tag) || /type="text\/
116123
* <link href="script.js" rel="modulepreload" as="serviceworker" />
117124
* <link href="script.js" rel="modulepreload" as="sharedworker" />
118125
*
119-
* @param {string} tag The tag with attributes.
126+
* @param {{}} attrs The tag attributes.
120127
* @return {boolean}
121128
*/
122-
const isLinkScript = (tag) => {
123-
if (tag.indexOf('as="script"') > 0) return true;
124-
if (tag.indexOf('rel="modulepreload"') > 0) {
125-
if (tag.indexOf('as="') < 0) return true;
126-
return /as="(worker|serviceworker|sharedworker)"/.test(tag);
129+
const isLinkScript = (attrs) => {
130+
if (attrs?.as === 'script') return true;
131+
if (attrs?.rel === 'modulepreload') {
132+
return !attrs?.as || /(worker|serviceworker|sharedworker)/.test(attrs.as);
127133
}
128134

129135
return false;
@@ -157,8 +163,8 @@ class HtmlParser {
157163
if (tag === 'script') {
158164
type = 'script';
159165
} else if (tag === 'link') {
160-
if (isLinkStyle(raw)) type = 'style';
161-
else if (isLinkScript(raw)) type = 'script';
166+
if (isLinkStyle(attrs)) type = 'style';
167+
else if (isLinkScript(attrs)) type = 'script';
162168
}
163169

164170
for (let attrName of attrList) {

src/Plugin/Collection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ class Collection {
878878

879879
// TODO: update this.data.assets[].asset.resource after change the filename in a template
880880
// - e.g. src="./main.js?v=1" => ./main.js?v=123 => WRONG filename is replaced
881+
881882
for (const [entryFilename, { entry, assets }] of this.data) {
882883
const rawSource = compilation.assets[entryFilename];
883884

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
<!-- inline CSS, attributes with single quote-->
6+
<style>h1 {
7+
color: orangered;
8+
}
9+
10+
.style-css-inline {
11+
color: green;
12+
}
13+
</style>
14+
15+
<!-- resolve CSS file, attributes with single quote -->
16+
<link href='style-file.e2174e16.css' rel='stylesheet' />
17+
18+
<!-- inline JS, attributes with single quote -->
19+
<script>console.log(">> script.js");</script>
20+
21+
<!-- resolve JS file, attributes with single quote -->
22+
<script src='script-file.d78e8065.js' defer='defer'></script>
23+
</head>
24+
<body>
25+
<h1>Home</h1>
26+
<div class="style-css-inline">Inlined CSS</div>
27+
<div class="style-css-file">File CSS</div>
28+
</body>
29+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(">> script-file.js");

test/cases/inline-style-query2/expected/style-file.e2174e16.css renamed to test/cases/inline-js-css-attr-single-quotes/expected/style-file.e2174e16.css

File renamed without changes.

test/cases/inline-style-query2/src/css/style-file.css renamed to test/cases/inline-js-css-attr-single-quotes/src/css/style-file.css

File renamed without changes.

test/cases/inline-style-query2/src/css/style.css renamed to test/cases/inline-js-css-attr-single-quotes/src/css/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
h1 {
2+
color: orangered;
3+
}
4+
15
.style-css-inline {
26
color: green;
37
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
<!-- inline CSS, attributes with single quote-->
6+
<link href='./css/style.css?inline' rel='stylesheet' />
7+
8+
<!-- resolve CSS file, attributes with single quote -->
9+
<link href='./css/style-file.css' rel='stylesheet' />
10+
11+
<!-- inline JS, attributes with single quote -->
12+
<script src='./js/script.js?inline' defer='defer'></script>
13+
14+
<!-- resolve JS file, attributes with single quote -->
15+
<script src='./js/script-file.js' defer='defer'></script>
16+
</head>
17+
<body>
18+
<h1>Home</h1>
19+
<div class="style-css-inline">Inlined CSS</div>
20+
<div class="style-css-file">File CSS</div>
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)