Skip to content

Commit 165de55

Browse files
committed
fix: issue if used multiple config and cache filesystem
1 parent 6fa672d commit 165de55

23 files changed

Lines changed: 257 additions & 30 deletions

File tree

CHANGELOG.md

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

3+
## 4.4.1 (2024-11-05)
4+
5+
- fix: if used multiple config and cache `filesystem`, occurs the error 'PersistentCache is already registered'
6+
- test: add test for multiple config when used cache `filesystem`
7+
38
## 4.4.0 (2024-11-04)
49

510
- feat: add `context` loader option to resolve assets w/o leading `/` in a directory outer your project:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div align="center">
2-
<img height="200" src="https://raw.githubusercontent.com/webdiscus/html-bundler-webpack-plugin/master/images/plugin-logo.png">
2+
<img height="200" src="images/plugin-logo.png">
33
<h1 align="center">
44
<a href="https://github.com/webdiscus/html-bundler-webpack-plugin">HTML Bundler Plugin for Webpack</a>
55
</h1>
@@ -56,7 +56,7 @@ See [full list of features](#features).
5656
## HTML template as entry point
5757

5858
<center>
59-
<img width="830" style="max-width: 100%;" src="https://raw.githubusercontent.com/webdiscus/html-bundler-webpack-plugin/master/images/assets-graph.png" alt="assets graph">
59+
<img width="830" style="max-width: 100%;" src="images/assets-graph.png" alt="assets graph">
6060
</center>
6161

6262
The **HTML Bundler** generates static HTML or [template function](#template-in-js) from [various templates](#template-engine) containing source files of scripts, styles, images, fonts and other resources, similar to how it works in [Vite](https://vitejs.dev/guide/#index-html-and-project-root).
@@ -190,7 +190,7 @@ The plugin resolves references in the HTML template and adds them to the Webpack
190190
Webpack will automatically process the source files, and the plugin replaces the references with their output filenames in the generated HTML.
191191
See [how the plugin works under the hood](#plugin-hooks-and-callbacks).
192192

193-
<img width="830" style="max-width: 100%;" src="https://raw.githubusercontent.com/webdiscus/html-bundler-webpack-plugin/master/images/workflow.png">
193+
<img width="830" style="max-width: 100%;" src="images/workflow.png">
194194

195195
## ❓Question / Feature Request / Bug
196196

@@ -911,7 +911,7 @@ Using hooks you can create your own plugin.
911911

912912
_How the plugin works under the hood._
913913
<center>
914-
<img width="765" style="max-width: 100%;" src="https://raw.githubusercontent.com/webdiscus/html-bundler-webpack-plugin/master/images/hooks.png" alt="HTMLBundlerPlugin hooks & callbacks">
914+
<img width="765" style="max-width: 100%;" src="images/hooks.png" alt="HTMLBundlerPlugin hooks & callbacks">
915915
</center>
916916

917917
### How to use hooks

package.json

Lines changed: 2 additions & 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.0",
3+
"version": "4.4.1",
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",
@@ -17,6 +17,7 @@
1717
"eta",
1818
"pug",
1919
"pugjs",
20+
"tempura",
2021
"twig",
2122
"twigjs",
2223
"integrity",

src/Plugin/AssetCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { baseUri, urlPathPrefix, cssLoaderName } = require('../Loader/Utils');
2222
const { findRootIssuer } = require('../Common/CompilationHelpers');
2323
const { isDir } = require('../Common/FileUtils');
2424
const { parseVersion, compareVersions } = require('../Common/Helpers');
25-
const createPersistentCache = require('./createPersistentCache');
25+
const createPersistentCache = require('./createPersistentCache')();
2626

2727
const CssExtractModule = require('./Modules/CssExtractModule');
2828
const Option = require('./Option');
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
const makeSerializable = require('webpack/lib/util/makeSerializable');
22

3-
const classCache = new WeakMap();
3+
const createPersistentCache = () => {
4+
const memorizedCache = new WeakMap();
5+
let cacheIndex = 0;
46

5-
/* istanbul ignore next: test it manual using `cache.type` as `filesystem` after 2nd run the same project */
6-
const createPersistentCache = (instance) => {
7-
if (classCache.has(instance)) {
8-
return classCache.get(instance);
9-
}
7+
return (instance) => {
8+
if (memorizedCache.has(instance)) {
9+
return memorizedCache.get(instance);
10+
}
1011

11-
class PersistentCache {
12-
instance = instance;
12+
class PersistentCache {
13+
instance = instance;
1314

14-
static getData(instance = {}) {
15-
return new PersistentCache(instance);
16-
}
15+
static getData(instance = {}) {
16+
return new PersistentCache(instance);
17+
}
1718

18-
constructor() {}
19+
constructor() {}
1920

20-
serialize(context) {
21-
this.instance.serialize(context);
22-
}
21+
serialize(context) {
22+
this.instance.serialize(context);
23+
}
2324

24-
deserialize(context) {
25-
this.instance.deserialize(context);
25+
deserialize(context) {
26+
this.instance.deserialize(context);
27+
}
2628
}
27-
}
2829

29-
makeSerializable(PersistentCache, __filename, 'PersistentCache');
30-
classCache.set(instance, PersistentCache);
30+
// the cache index is needed for multiple configuration
31+
makeSerializable(PersistentCache, __filename, `PersistentCache_${cacheIndex}`);
32+
memorizedCache.set(instance, PersistentCache);
33+
cacheIndex++;
3134

32-
return PersistentCache;
35+
return PersistentCache;
36+
};
3337
};
3438

3539
module.exports = createPersistentCache;

test/cases/cache-filesystem-js/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111

1212
output: {
1313
path: path.join(__dirname, 'dist/'),
14-
clean: true,
14+
clean: false,
1515
},
1616

1717
plugins: [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>About</title>
5+
</head>
6+
<body>
7+
<h1>About</h1>
8+
</body>
9+
</html>
139 Bytes
Loading
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>Home</title>
5+
<script src="index.js" defer></script>
6+
</head>
7+
<body>
8+
<h1>Home</h1>
9+
<img src="e7d4ced06af282ab4474.png" alt="iphone" />
10+
</body>
11+
</html>

test/cases/cache-filesystem-multi-config/expected/index.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.

0 commit comments

Comments
 (0)