Skip to content

Commit beb28f7

Browse files
committed
fix: avoid recompiling all entry templates after changes of a non-entry partial file
1 parent 4732af5 commit beb28f7

25 files changed

Lines changed: 269 additions & 14 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+
## 3.6.2 (2024-03-11)
4+
5+
- fix: avoid recompiling all entry templates after changes of a non-entry partial file, [pug-plugin issue](https://github.com/webdiscus/pug-plugin/issues/66)
6+
37
## 3.6.1 (2024-03-08)
48

59
- fix: cannot find module 'nunjucks/src/object', introduced in v3.6.0

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.6.1",
3+
"version": "3.6.2",
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/Dependency.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const { readDirRecursiveSync } = require('../Common/FileUtils');
23
const PluginService = require('../Plugin/PluginService');
34
const AssetEntry = require('../Plugin/AssetEntry');
@@ -11,18 +12,28 @@ class Dependency {
1112
static fileSystem = null;
1213
static files = new Set();
1314
static directories = new Set();
14-
static loaderContext = null;
1515
static watchFiles = {};
16-
static #entryFiles = new Set();
16+
static entryFiles = new Set();
17+
static excludeDirs = [];
18+
static loaderContext = null;
1719

1820
static init(loaderContext) {
1921
if (!PluginService.isWatchMode()) return;
2022

23+
let watchFile = loaderContext.resourcePath;
24+
2125
PluginService.setDependencyInstance(this);
2226
this.loaderContext = loaderContext;
2327
this.fileSystem = loaderContext.fs.fileSystem;
2428
this.watchFiles = Option.getWatchFiles();
25-
this.#entryFiles = AssetEntry.getEntryFiles();
29+
this.entryFiles = AssetEntry.getEntryFiles();
30+
31+
this.excludeDirs = [];
32+
this.entryFiles.forEach((entryFile) => {
33+
let entryDir = path.dirname(entryFile) + path.sep;
34+
if (!watchFile.startsWith(entryDir)) this.excludeDirs.push(entryDir);
35+
});
36+
2637
this.addFile = this.addFile.bind(this);
2738

2839
const fs = this.fileSystem;
@@ -69,18 +80,18 @@ class Dependency {
6980
static watch() {
7081
if (!PluginService.isWatchMode()) return;
7182

72-
const { loaderContext } = this;
83+
const { loaderContext, excludeDirs } = this;
7384

74-
for (let dir of this.directories) {
75-
loaderContext.addContextDependency(dir);
76-
}
85+
this.directories.forEach(loaderContext.addContextDependency);
7786

7887
for (let file of this.files) {
79-
if (!this.#entryFiles.has(file)) {
80-
// the dependency already contains the current resource file,
81-
// add for watching only files not defined in the entry to avoid unnecessary rebuilding of all templates
82-
loaderContext.addDependency(file);
88+
let isExcluded = excludeDirs.findIndex((dirname) => file.startsWith(dirname)) > -1;
89+
90+
if (isExcluded || this.entryFiles.has(file)) {
91+
// ignore all files from other entry directory or the current entry file, because it is already in the list
92+
continue;
8393
}
94+
loaderContext.addDependency(file);
8495
}
8596
}
8697

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Problem `FIXED` in v3.6.2
2+
3+
Compiling all entrypoint files after changes of a non-entry file.
4+
5+
**Project files**
6+
7+
```
8+
pages/home/index.html <= entrypoint
9+
pages/home/includes/header.html <= non entry partial
10+
11+
pages/about/index.html <= entrypoint
12+
pages/about/includes/header.html <= non entry partial
13+
```
14+
15+
### Reproduce
16+
17+
- change `pages/home/index.html` => will be rendered only this file. OK
18+
- change `pages/home/includes/header.html` => will be recompiled pages:
19+
- pages/home/index.html (with all dependencies), OK
20+
- pages/about/index.html (with all dependencies), NOT OK, should not be recompiled (`FIXED`)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scripts": {
3+
"start": "webpack serve --mode development",
4+
"watch": "webpack watch --mode development",
5+
"build": "webpack --mode=production --progress"
6+
},
7+
"devDependencies": {
8+
"html-bundler-webpack-plugin": "file:../../.."
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="slider">
2+
<div>Slide 1</div>
3+
<div>Slide 2</div>
4+
<div>Slide 3</div>
5+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>About product 123456789</h1>
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 product</title>
5+
</head>
6+
<body>
7+
<%- include('./includes/header.html') %>
8+
<p>Test body: 123 45678</p>
9+
<%- include('../../../components/slider/slider.html') %>
10+
</body>
11+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>About 123</h1>

0 commit comments

Comments
 (0)