Skip to content

Commit de54cc3

Browse files
committed
fix: built fails when used a template variable in srcset with JS-template, #176
1 parent eeba07d commit de54cc3

13 files changed

Lines changed: 134 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 4.20.6 (2025-06-29)
4+
5+
- fix: built fails when used a template variable in srcset with JS-template, #176
6+
```html
7+
<img srcset="{{ img }}" />
8+
```
9+
310
## 4.20.5 (2025-06-29)
411

512
- fix: handle mixed `preprocessorMode` cases consistently, #174

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.20.5",
3+
"version": "4.20.6",
44
"description": "Generates complete single-page or multi-page website from source assets. Built-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",

src/Common/HtmlParser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ const isLinkScript = (attrs) => {
135135
return false;
136136
};
137137

138+
/**
139+
* Check whether a given string is a plain HTML attribute value
140+
* and not a compiled JavaScript expression.
141+
*
142+
* @param {string} value The attribute value to test.
143+
* @returns {boolean} Returns true if the value appears to be a plain HTML attribute value.
144+
*/
145+
const isHtmlAttributeValue = (value) => {
146+
if (!value || typeof value !== 'string') return false;
147+
148+
// allow if value is a simple static require(path) or url(path)
149+
if (value.includes('require(') || value.includes('url(')) return true;
150+
151+
// heuristic check: disallow common JS syntax characters
152+
const forbidden = /[();|!+*<>]/;
153+
154+
return !forbidden.test(value);
155+
};
156+
138157
/**
139158
* Parse tag attributes in a tag string.
140159
*
@@ -367,8 +386,15 @@ class HtmlParser {
367386
startPos: attrData.valueStartPos,
368387
endPos: attrData.valueEndPos,
369388
inEscapedDoubleQuotes,
389+
parsedValue: [''],
370390
};
371391

392+
// check whether the value is JS-template code generated by a template engine
393+
if (!isHtmlAttributeValue(value)) {
394+
//return {};
395+
return result;
396+
}
397+
372398
if (attr.indexOf('srcset') > -1) {
373399
const { values, attrs } = this.parseSrcsetValue(value, attrData.valueStartPos, inEscapedDoubleQuotes);
374400

test/cases/_preprocessor/js-tmpl-hbs-compile-undefined-var/expected/app.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.6 KB
Loading
1.66 KB
Loading
1.72 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My Title</title>
5+
<script src="app.js" defer="defer"></script>
6+
</head>
7+
<body>
8+
<div id="main"></div>
9+
</body>
10+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import tmpl from './partials/content.hbs?lang=en';
2+
import imgPear from '@images/pear.png';
3+
4+
const locals = {
5+
name: 'World',
6+
people: ['Alexa <Amazon>', 'Cortana <MS>', 'Siri <Apple>'],
7+
imgKiwi: require('@images/kiwi.png'), // resolve an asset using require
8+
imgPear, // resolve an asset using import
9+
};
10+
11+
document.getElementById('main').innerHTML = tmpl(locals);
12+
13+
console.log('>> app');
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>{{ title }}</title>
5+
{{!-- load the rendered template in JS and add it into HTML in runtime --}}
6+
<script src="app.js" defer="defer"></script>
7+
</head>
8+
<body>
9+
<div id="main"></div>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)