Skip to content

Commit c459eaa

Browse files
committed
fix: ERROR in RealContentHashPlugin in serv/watch mode after adding new import file
1 parent d245481 commit c459eaa

3 files changed

Lines changed: 42 additions & 20 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+
## 4.0.0-beta.2 (2024-08-15)
4+
5+
- fix: ERROR in RealContentHashPlugin in serv/watch mode after adding new import file
6+
37
## 4.0.0-beta.1 (2024-08-13)
48

59
- fix: when using integrity occurs ERROR in RealContentHashPlugin in serv/watch mode after changes by using dynamic import

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.0.0-beta.1",
3+
"version": "4.0.0-beta.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/Plugin/Extras/Integrity.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class Integrity {
3030
this.chunkByChunkId = new Map();
3131
this.templateByChunkId = new Map();
3232
this.placeholderByChunkId = new Map();
33+
this.Template = null;
3334
}
3435

3536
apply(compiler) {
3637
const { pluginName } = this;
38+
const { Template } = compiler.webpack;
39+
40+
this.Template = Template;
3741

3842
compiler.hooks.afterPlugins.tap(pluginName, (compiler) => {
3943
compiler.hooks.thisCompilation.tap({ name: pluginName, stage: -10000 }, (compilation) => {
@@ -74,7 +78,7 @@ class Integrity {
7478
mainTemplate.hooks.localVars.tap(pluginName, this.getTemplateByChunk.bind(this));
7579

7680
compilation.hooks.beforeRuntimeRequirements.tap(pluginName, () => {
77-
// note: don't clear the cached placeholderByChunkId
81+
this.templateByChunkId.clear();
7882
});
7983

8084
compilation.hooks.processAssets.tap(
@@ -111,6 +115,9 @@ class Integrity {
111115
return chunk ? Integrity.getIntegrity(this.compilation, content[0], chunk) : undefined;
112116
}
113117

118+
/**
119+
* @param {Object} assets
120+
*/
114121
processAssets(assets) {
115122
const { compilation, isRealContentHash } = this;
116123
const { compiler } = compilation;
@@ -122,11 +129,7 @@ class Integrity {
122129
continue;
123130
}
124131

125-
let childChunks = this.chunkChildChunksMap.get(chunk);
126-
if (!childChunks) {
127-
childChunks = chunk.getAllAsyncChunks();
128-
this.chunkChildChunksMap.set(chunk, childChunks);
129-
}
132+
const childChunks = this.getChildChunks(chunk);
130133

131134
for (const childChunk of childChunks) {
132135
if (childChunk.id == null) {
@@ -178,6 +181,21 @@ class Integrity {
178181
}
179182
}
180183

184+
/**
185+
* @param {Chunk} chunk The webpack chunk.
186+
* @return {Set<Chunk>} The child chunks.
187+
*/
188+
getChildChunks(chunk) {
189+
let childChunks = this.chunkChildChunksMap.get(chunk);
190+
191+
if (!childChunks) {
192+
childChunks = chunk.getAllAsyncChunks();
193+
this.chunkChildChunksMap.set(chunk, childChunks);
194+
}
195+
196+
return childChunks;
197+
}
198+
181199
/**
182200
* Create the integrity template by the tag.
183201
*
@@ -209,31 +227,31 @@ class Integrity {
209227
* @return {string}
210228
*/
211229
getTemplateByChunk(source, chunk) {
212-
const { Template } = this.compilation.compiler.webpack;
213-
214230
if (this.templateByChunkId.has(chunk.id)) {
215231
return this.templateByChunkId.get(chunk.id);
216232
}
217233

218-
const childChunks = chunk.getAllAsyncChunks();
219-
this.chunkChildChunksMap.set(chunk, childChunks);
234+
const childChunks = this.getChildChunks(chunk);
235+
236+
if (childChunks.size < 1) {
237+
return source;
238+
}
220239

221240
const placeholders = {};
222241
for (const childChunk of childChunks) {
223-
const placeholder = getPlaceholder(childChunk.id);
242+
let placeholder = this.placeholderByChunkId.get(childChunk.id);
243+
if (!placeholder) {
244+
placeholder = getPlaceholder(childChunk.id);
245+
this.placeholderByChunkId.set(childChunk.id, placeholder);
246+
}
224247
placeholders[childChunk.id] = placeholder;
225-
this.placeholderByChunkId.set(childChunk.id, placeholder);
226248
this.chunkByChunkId.set(childChunk.id, childChunk);
227249
}
228250

229-
if (Object.keys(placeholders).length > 0) {
230-
const template = Template.asString([source, `${hashesReference} = ${JSON.stringify(placeholders)};`]);
231-
this.templateByChunkId.set(chunk.id, template);
232-
233-
return template;
234-
}
251+
const template = this.Template.asString([source, `${hashesReference} = ${JSON.stringify(placeholders)};`]);
252+
this.templateByChunkId.set(chunk.id, template);
235253

236-
return source;
254+
return template;
237255
}
238256

239257
/**

0 commit comments

Comments
 (0)