Skip to content

Commit eeba07d

Browse files
committed
fix: handle mixed preprocessorMode cases consistently, #174
1 parent 4fbe63c commit eeba07d

File tree

12 files changed

+78
-35
lines changed

12 files changed

+78
-35
lines changed

CHANGELOG.md

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

3+
## 4.20.5 (2025-06-29)
4+
5+
- fix: handle mixed `preprocessorMode` cases consistently, #174
6+
37
## 4.20.4 (2025-06-28)
48

59
- fix: template rendering error in serve/watch mode with JS templates, #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.4",
3+
"version": "4.20.5",
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/Loader/Option.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Option {
4040
// rule: the first value is default
4141
preprocessorModes = new Set(['render', 'compile']);
4242
#preprocessorModule;
43+
#originalPreprocessorMode;
4344

4445
constructor() {}
4546

@@ -84,7 +85,7 @@ class Option {
8485
options.esModule = options.esModule === true;
8586

8687
// save the initial value defined in the webpack config
87-
options.originalPreprocessorMode = options.preprocessorMode;
88+
this.#originalPreprocessorMode = options.preprocessorMode;
8889

8990
// set reference to sources defined directly in plugin options
9091
if (this.#pluginOption.options?.sources != null) {
@@ -150,6 +151,7 @@ class Option {
150151
#initPreprocessor(loaderContext) {
151152
const queryData = this.#queryData;
152153
const options = this.#options;
154+
//const resource = loaderContext.resourcePath + loaderContext.resourceQuery;
153155
const issuer = loaderContext._module.resourceResolveData?.context?.issuer || '';
154156
let [defaultPreprocessorMode] = this.preprocessorModes;
155157
let isIssuerScript = false;
@@ -181,13 +183,16 @@ class Option {
181183

182184
// reset the original option value, also no cached state,
183185
// because the loader works in different modes depend on the context
184-
options.preprocessorMode = options.originalPreprocessorMode;
186+
options.preprocessorMode = this.#originalPreprocessorMode;
185187

186188
if (preprocessorMode && this.preprocessorModes.has(preprocessorMode)) {
187189
options.preprocessorMode = preprocessorMode;
188190
} else if (!this.preprocessorModes.has(options.preprocessorMode)) {
189191
options.preprocessorMode = defaultPreprocessorMode;
190192
}
193+
194+
// safe dynamic option directly into module meta data
195+
loaderContext._module.resourceResolveData._bundlerPluginMeta.preprocessorMode = options.preprocessorMode;
191196
}
192197

193198
/**
@@ -444,6 +449,13 @@ class Option {
444449
this.#preprocessorModule._module = preprocessor;
445450
}
446451

452+
/**
453+
* @param {string} mode
454+
*/
455+
setPreprocessorMode(mode) {
456+
this.#options.preprocessorMode = mode;
457+
}
458+
447459
/**
448460
* @return {Object}
449461
*/

src/Loader/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const loader = function (content, map, meta) {
5454
const loader = LoaderFactory.createLoader(pluginCompiler);
5555
const hooks = PluginService.getHooks(pluginCompiler);
5656
const loaderOptionPromise = LoaderFactory.createOption(pluginCompiler, loaderContext);
57+
const preprocessorMode = loaderContext._module?.resourceResolveData?._bundlerPluginMeta?.preprocessorMode;
5758
let loaderOption = null;
5859

5960
loaderOptionPromise
@@ -62,6 +63,9 @@ const loader = function (content, map, meta) {
6263
errorStage = 'init';
6364
loaderOption = result;
6465

66+
// update with correct dynamic value
67+
loaderOption.setPreprocessorMode(preprocessorMode);
68+
6569
// bind the loaderOption before initialisations
6670
PluginService.setLoaderOption(pluginCompiler, loaderOption);
6771

@@ -97,6 +101,9 @@ const loader = function (content, map, meta) {
97101
return hooks.beforePreprocessor.promise(content, loaderContext);
98102
})
99103
.then((value) => {
104+
// update with correct dynamic value
105+
loaderOption.setPreprocessorMode(preprocessorMode);
106+
100107
const beforePreprocessor = loaderOption.get().beforePreprocessor;
101108
if (beforePreprocessor != null) {
102109
errorStage = 'beforePreprocessor';
@@ -105,14 +112,19 @@ const loader = function (content, map, meta) {
105112
return value;
106113
})
107114
.then((value) => {
115+
// update with correct dynamic value
116+
loaderOption.setPreprocessorMode(preprocessorMode);
117+
108118
const loaderOptions = loaderOption.get();
109119
errorStage = 'preprocessor';
110120

111121
// TODO: add to types.d.ts loaderOptions as 3rd param
112122
return hooks.preprocessor.promise(value, loaderContext, loaderOptions);
113123
})
114124
.then((value) => {
115-
// 3
125+
// update with correct dynamic value
126+
loaderOption.setPreprocessorMode(preprocessorMode);
127+
116128
const preprocessor = loaderOption.getPreprocessor();
117129
if (preprocessor) {
118130
const loaderOptions = loaderOption.get();
@@ -181,6 +193,9 @@ const loader = function (content, map, meta) {
181193

182194
const browserErrorMessage = loader?.exportError(error, resource);
183195

196+
// update with correct value
197+
loaderOption.setPreprocessorMode(preprocessorMode);
198+
184199
// initialize dependency when an exception occurs before regular dependency initialisation
185200
dependency.init({ loaderContext, loaderOption });
186201
dependency.watch();

test/manual/watch-hbs-partials-js-tmpl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "module",
23
"scripts": {
34
"start": "webpack serve",
45
"watch": "webpack watch --mode development",

test/manual/watch-hbs-partials-js-tmpl/src/app.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
{{#*inline "footerContainer"}}
99
{{> footer}}
1010
{{/inline}}
11-
{{/layout}}
11+
{{/layout}}

test/manual/watch-hbs-partials-js-tmpl/src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tmpl from './app.hbs';
1+
import tmpl from './app.hbs?lang=de';
22

33
const locals = {
44
helloName: 'World',

test/manual/watch-hbs-partials-js-tmpl/src/data/index.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { productsDB } from './productsDB.js';
2+
3+
export default {
4+
projectName: 'Test',
5+
copyright: 'ABC copyright',
6+
smallSliderProductCards: [productsDB[874], productsDB[8711], productsDB[8711], productsDB[874], productsDB[8711]],
7+
};
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export const productsDB = {
2-
874: {
3-
id: '874',
4-
currentPrice: '5 489',
5-
prevPrice: false,
6-
},
7-
8711: {
8-
id: '8711',
9-
currentPrice: '7 103',
10-
prevPrice: '7 896',
11-
},
2+
874: {
3+
id: '874',
4+
currentPrice: '5 489',
5+
prevPrice: false,
6+
},
7+
8711: {
8+
id: '8711',
9+
currentPrice: '7 103.09',
10+
prevPrice: '7 896',
11+
},
1212
};

0 commit comments

Comments
 (0)