Skip to content

Commit bcbf4ac

Browse files
committed
fix(pug): correct resolving required resources in multiple pages
1 parent c8941eb commit bcbf4ac

25 files changed

Lines changed: 150 additions & 10 deletions

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+
## 3.5.1 (2024-02-20)
4+
5+
- fix(pug): correct resolving required resources in multiple pages
6+
37
## 3.5.0 (2024-02-18)
48

59
- feat: add support for the `Pug` template engine.

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": "3.5.0",
3+
"version": "3.5.1",
44
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
55
"keywords": [
66
"html",

src/Loader/Preprocessors/Pug/PugCompiler.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PugCompiler {
6565
* @param {{}} loaderOptions
6666
*/
6767
constructor(loaderContext, loaderOptions) {
68-
const { rootContext: context, resource, resourcePath: filename, resourceQuery } = loaderContext;
68+
const { rootContext: context } = loaderContext;
6969
let basedir = loaderOptions.basedir || context;
7070

7171
this.pug = loadModule('pug');
@@ -74,9 +74,6 @@ class PugCompiler {
7474
Filter.loadFilters(loaderOptions);
7575

7676
this.pugOptions = {
77-
// used to resolve import/extends and to improve errors
78-
filename,
79-
8077
// the root directory of all absolute inclusions, defaults is `/`.
8178
basedir,
8279

@@ -113,15 +110,19 @@ class PugCompiler {
113110
};
114111
}
115112

116-
_compile(source) {
113+
_compile(source, file) {
114+
// note: for each new compilation must be defined the `filename` with current template filename
115+
// used to resolve import/extends and to improve errors
116+
this.pugOptions.filename = file;
117+
117118
return this.pug.compileClientWithDependenciesTracked(source, this.pugOptions).body;
118119
}
119120

120-
compile(source) {
121+
compile(source, { file }) {
121122
ResolvePlugin.mode = 'compile';
122123

123124
const exportFunctionName = 'templateFn';
124-
const templateFunctionSource = this._compile(source);
125+
const templateFunctionSource = this._compile(source, file);
125126

126127
return templateFunctionSource + `;var ${exportFunctionName}=${this.pugOptions.name};`;
127128
}
@@ -136,7 +137,7 @@ class PugCompiler {
136137
render(source, { file, data, esModule }) {
137138
ResolvePlugin.mode = 'render';
138139

139-
const templateFunctionSource = this._compile(source);
140+
const templateFunctionSource = this._compile(source, file);
140141
const name = this.pugOptions.name;
141142

142143
const vmScript = new VMScript(

src/Loader/Preprocessors/Pug/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const PugCompiler = require('./PugCompiler');
1313
const preprocessor = (loaderContext, options, { esModule, watch }) => {
1414
const exportCode = esModule ? 'export default ' : 'module.exports=';
1515
const Pug = new PugCompiler(loaderContext, options);
16-
const { rootContext } = loaderContext;
1716

1817
return {
1918
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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.5317c1f6.js"></script></head><body><h4>Default layout</h4><h1>About</h1><script src="js/script.1.e55bdc4a.js"></script></body></html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
body {
2+
font-size: 18px;
3+
font-family: Arial, sans-serif;
4+
}
5+
6+
h1 {
7+
color: deepskyblue;
8+
}
9+
10+
h4 {
11+
color: #9f9f9f;
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: orangered;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: seagreen;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(">> main");

0 commit comments

Comments
 (0)