Skip to content

Commit 972c005

Browse files
authored
fix(html): not to override plugin options when using multiple instances (#10284)
* fix(html): should not override plugin options when using multiple plugin instances * fix(html): should not override plugin options when using multiple plugin instances
1 parent 4234ceb commit 972c005

File tree

15 files changed

+198
-44
lines changed

15 files changed

+198
-44
lines changed

crates/node_binding/binding.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ export interface JsAdditionalTreeRuntimeRequirementsResult {
498498
export interface JsAfterEmitData {
499499
outputName: string
500500
compilationId: number
501+
uid?: number
501502
}
502503

503504
export interface JsAfterResolveData {
@@ -517,6 +518,7 @@ export interface JsAfterTemplateExecutionData {
517518
bodyTags: Array<JsHtmlPluginTag>
518519
outputName: string
519520
compilationId: number
521+
uid?: number
520522
}
521523

522524
export interface JsAlterAssetTagGroupsData {
@@ -525,13 +527,15 @@ export interface JsAlterAssetTagGroupsData {
525527
publicPath: string
526528
outputName: string
527529
compilationId: number
530+
uid?: number
528531
}
529532

530533
export interface JsAlterAssetTagsData {
531534
assetTags: JsHtmlPluginAssetTags
532535
outputName: string
533536
publicPath: string
534537
compilationId: number
538+
uid?: number
535539
}
536540

537541
export interface JsAsset {
@@ -559,12 +563,14 @@ export interface JsBeforeAssetTagGenerationData {
559563
assets: JsHtmlPluginAssets
560564
outputName: string
561565
compilationId: number
566+
uid?: number
562567
}
563568

564569
export interface JsBeforeEmitData {
565570
html: string
566571
outputName: string
567572
compilationId: number
573+
uid?: number
568574
}
569575

570576
export interface JsBeforeResolveArgs {
@@ -1837,6 +1843,7 @@ export interface RawHtmlRspackPluginOptions {
18371843
meta?: Record<string, Record<string, string>>
18381844
hash?: boolean
18391845
base?: RawHtmlRspackPluginBaseOptions
1846+
uid?: number
18401847
}
18411848

18421849
export interface RawHttpExternalsRspackPluginOptions {

crates/node_binding/src/html.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub struct JsBeforeAssetTagGenerationData {
121121
pub assets: JsHtmlPluginAssets,
122122
pub output_name: String,
123123
pub compilation_id: u32,
124+
pub uid: Option<u32>,
124125
}
125126

126127
impl From<JsBeforeAssetTagGenerationData> for BeforeAssetTagGenerationData {
@@ -129,6 +130,7 @@ impl From<JsBeforeAssetTagGenerationData> for BeforeAssetTagGenerationData {
129130
assets: value.assets.into(),
130131
output_name: value.output_name,
131132
compilation_id: CompilationId(value.compilation_id),
133+
uid: value.uid,
132134
}
133135
}
134136
}
@@ -139,6 +141,7 @@ impl From<BeforeAssetTagGenerationData> for JsBeforeAssetTagGenerationData {
139141
assets: value.assets.into(),
140142
output_name: value.output_name,
141143
compilation_id: value.compilation_id.0,
144+
uid: value.uid,
142145
}
143146
}
144147
}
@@ -200,6 +203,7 @@ pub struct JsAlterAssetTagsData {
200203
pub output_name: String,
201204
pub public_path: String,
202205
pub compilation_id: u32,
206+
pub uid: Option<u32>,
203207
}
204208

205209
impl From<AlterAssetTagsData> for JsAlterAssetTagsData {
@@ -209,6 +213,7 @@ impl From<AlterAssetTagsData> for JsAlterAssetTagsData {
209213
output_name: value.output_name,
210214
public_path: value.public_path,
211215
compilation_id: value.compilation_id.0,
216+
uid: value.uid,
212217
}
213218
}
214219
}
@@ -220,6 +225,7 @@ impl From<JsAlterAssetTagsData> for AlterAssetTagsData {
220225
output_name: value.output_name,
221226
public_path: value.public_path,
222227
compilation_id: CompilationId(value.compilation_id),
228+
uid: value.uid,
223229
}
224230
}
225231
}
@@ -231,6 +237,7 @@ pub struct JsAlterAssetTagGroupsData {
231237
pub public_path: String,
232238
pub output_name: String,
233239
pub compilation_id: u32,
240+
pub uid: Option<u32>,
234241
}
235242

236243
impl From<AlterAssetTagGroupsData> for JsAlterAssetTagGroupsData {
@@ -249,6 +256,7 @@ impl From<AlterAssetTagGroupsData> for JsAlterAssetTagGroupsData {
249256
public_path: value.public_path,
250257
output_name: value.output_name,
251258
compilation_id: value.compilation_id.0,
259+
uid: value.uid,
252260
}
253261
}
254262
}
@@ -269,6 +277,7 @@ impl From<JsAlterAssetTagGroupsData> for AlterAssetTagGroupsData {
269277
public_path: value.public_path,
270278
output_name: value.output_name,
271279
compilation_id: CompilationId(value.compilation_id),
280+
uid: value.uid,
272281
}
273282
}
274283
}
@@ -280,6 +289,7 @@ pub struct JsAfterTemplateExecutionData {
280289
pub body_tags: Vec<JsHtmlPluginTag>,
281290
pub output_name: String,
282291
pub compilation_id: u32,
292+
pub uid: Option<u32>,
283293
}
284294

285295
impl From<AfterTemplateExecutionData> for JsAfterTemplateExecutionData {
@@ -298,6 +308,7 @@ impl From<AfterTemplateExecutionData> for JsAfterTemplateExecutionData {
298308
.collect::<Vec<_>>(),
299309
output_name: value.output_name,
300310
compilation_id: value.compilation_id.0,
311+
uid: value.uid,
301312
}
302313
}
303314
}
@@ -318,6 +329,7 @@ impl From<JsAfterTemplateExecutionData> for AfterTemplateExecutionData {
318329
.collect::<Vec<_>>(),
319330
output_name: value.output_name,
320331
compilation_id: CompilationId(value.compilation_id),
332+
uid: value.uid,
321333
}
322334
}
323335
}
@@ -327,6 +339,7 @@ pub struct JsBeforeEmitData {
327339
pub html: String,
328340
pub output_name: String,
329341
pub compilation_id: u32,
342+
pub uid: Option<u32>,
330343
}
331344

332345
impl From<BeforeEmitData> for JsBeforeEmitData {
@@ -335,6 +348,7 @@ impl From<BeforeEmitData> for JsBeforeEmitData {
335348
html: value.html,
336349
output_name: value.output_name,
337350
compilation_id: value.compilation_id.0,
351+
uid: value.uid,
338352
}
339353
}
340354
}
@@ -345,6 +359,7 @@ impl From<JsBeforeEmitData> for BeforeEmitData {
345359
html: value.html,
346360
output_name: value.output_name,
347361
compilation_id: CompilationId(value.compilation_id),
362+
uid: value.uid,
348363
}
349364
}
350365
}
@@ -353,13 +368,15 @@ impl From<JsBeforeEmitData> for BeforeEmitData {
353368
pub struct JsAfterEmitData {
354369
pub output_name: String,
355370
pub compilation_id: u32,
371+
pub uid: Option<u32>,
356372
}
357373

358374
impl From<AfterEmitData> for JsAfterEmitData {
359375
fn from(value: AfterEmitData) -> Self {
360376
Self {
361377
output_name: value.output_name,
362378
compilation_id: value.compilation_id.0,
379+
uid: value.uid,
363380
}
364381
}
365382
}
@@ -369,6 +386,7 @@ impl From<JsAfterEmitData> for AfterEmitData {
369386
Self {
370387
output_name: value.output_name,
371388
compilation_id: CompilationId(value.compilation_id),
389+
uid: value.uid,
372390
}
373391
}
374392
}

crates/node_binding/src/raw_options/raw_builtins/raw_html.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct RawHtmlRspackPluginOptions {
5858
pub meta: Option<HashMap<String, HashMap<String, String>>>,
5959
pub hash: Option<bool>,
6060
pub base: Option<RawHtmlRspackPluginBaseOptions>,
61+
pub uid: Option<u32>,
6162
}
6263

6364
impl From<RawHtmlRspackPluginOptions> for HtmlRspackPluginOptions {
@@ -118,6 +119,7 @@ impl From<RawHtmlRspackPluginOptions> for HtmlRspackPluginOptions {
118119
meta: value.meta,
119120
hash: value.hash,
120121
base: value.base.map(|v| v.into()),
122+
uid: value.uid,
121123
}
122124
}
123125
}

crates/rspack_plugin_html/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ pub struct HtmlRspackPluginOptions {
176176
pub meta: Option<HashMap<String, HashMap<String, String>>>,
177177
pub hash: Option<bool>,
178178
pub base: Option<HtmlRspackPluginBaseOptions>,
179+
/// uid is used to identify the plugin instance on javascript side
180+
pub uid: Option<u32>,
179181
}
180182

181183
fn default_filename() -> Vec<String> {
@@ -215,6 +217,7 @@ impl Default for HtmlRspackPluginOptions {
215217
meta: None,
216218
hash: None,
217219
base: None,
220+
uid: None,
218221
}
219222
}
220223
}

crates/rspack_plugin_html/src/drive.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct BeforeAssetTagGenerationData {
1111
pub assets: HtmlPluginAssets,
1212
pub output_name: String,
1313
pub compilation_id: CompilationId,
14+
pub uid: Option<u32>,
1415
}
1516

1617
#[derive(Clone, Debug)]
@@ -19,6 +20,7 @@ pub struct AlterAssetTagsData {
1920
pub output_name: String,
2021
pub public_path: String,
2122
pub compilation_id: CompilationId,
23+
pub uid: Option<u32>,
2224
}
2325

2426
#[derive(Clone, Debug)]
@@ -28,6 +30,7 @@ pub struct AlterAssetTagGroupsData {
2830
pub public_path: String,
2931
pub output_name: String,
3032
pub compilation_id: CompilationId,
33+
pub uid: Option<u32>,
3134
}
3235

3336
#[derive(Clone, Debug)]
@@ -37,19 +40,22 @@ pub struct AfterTemplateExecutionData {
3740
pub body_tags: Vec<HtmlPluginTag>,
3841
pub output_name: String,
3942
pub compilation_id: CompilationId,
43+
pub uid: Option<u32>,
4044
}
4145

4246
#[derive(Clone, Debug)]
4347
pub struct BeforeEmitData {
4448
pub html: String,
4549
pub output_name: String,
4650
pub compilation_id: CompilationId,
51+
pub uid: Option<u32>,
4752
}
4853

4954
#[derive(Clone, Debug)]
5055
pub struct AfterEmitData {
5156
pub output_name: String,
5257
pub compilation_id: CompilationId,
58+
pub uid: Option<u32>,
5359
}
5460

5561
define_hook!(HtmlPluginBeforeAssetTagGeneration: SeriesWaterfall(data: BeforeAssetTagGenerationData) -> BeforeAssetTagGenerationData);

crates/rspack_plugin_html/src/plugin.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ async fn generate_html(
8686
assets: assets_info.0,
8787
output_name: html_file_name.as_str().to_string(),
8888
compilation_id: compilation.id(),
89+
uid: config.uid,
8990
})
9091
.await?;
9192

@@ -99,6 +100,7 @@ async fn generate_html(
99100
public_path: public_path.clone(),
100101
output_name: html_file_name.as_str().to_string(),
101102
compilation_id: compilation.id(),
103+
uid: config.uid,
102104
})
103105
.await?;
104106

@@ -113,6 +115,7 @@ async fn generate_html(
113115
public_path: public_path.clone(),
114116
output_name: html_file_name.as_str().to_string(),
115117
compilation_id: compilation.id(),
118+
uid: config.uid,
116119
})
117120
.await?;
118121

@@ -137,6 +140,7 @@ async fn generate_html(
137140
body_tags: alter_asset_tag_groups_data.body_tags,
138141
output_name: html_file_name.as_str().to_string(),
139142
compilation_id: compilation.id(),
143+
uid: config.uid,
140144
})
141145
.await?;
142146

@@ -237,6 +241,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
237241
html,
238242
output_name: output_file_name.as_str().to_string(),
239243
compilation_id: compilation.id(),
244+
uid: config.uid,
240245
})
241246
.await?;
242247

@@ -266,6 +271,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
266271
.call(AfterEmitData {
267272
output_name: html_asset.0.to_string(),
268273
compilation_id: compilation.id(),
274+
uid: config.uid,
269275
})
270276
.await?;
271277
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("html template content", () => {
5+
const htmlPath = path.join(__dirname, "./index.html");
6+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
7+
expect(htmlContent.includes("<div>production</div>")).toBe(true);
8+
});
9+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { rspack } = require("@rspack/core");
2+
3+
/** @type {import("@rspack/core").Configuration} */
4+
module.exports = {
5+
plugins: [
6+
new rspack.HtmlRspackPlugin({
7+
templateContent:
8+
"<!DOCTYPE html><html><body><div><%= env %></div></body></html>",
9+
templateParameters: {
10+
env: "production"
11+
},
12+
filename: "index.html",
13+
customOptions: {
14+
property: "value"
15+
}
16+
}),
17+
{
18+
apply: compiler => {
19+
compiler.hooks.thisCompilation.tap("HtmlRspackPlugin", compilation => {
20+
const hooks =
21+
rspack.HtmlRspackPlugin.getCompilationHooks(compilation);
22+
hooks.beforeEmit.tap("HtmlRspackPlugin", htmlPluginData => {
23+
expect(htmlPluginData.plugin.options.customOptions.property).toBe(
24+
"value"
25+
);
26+
});
27+
});
28+
}
29+
}
30+
]
31+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("html template content", () => {
5+
const htmlPath = path.join(__dirname, "./index.html");
6+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
7+
expect(htmlContent.includes("<div>production</div>")).toBe(true);
8+
});
9+
10+
it("html template content", () => {
11+
const htmlPath = path.join(__dirname, "./index2.html");
12+
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
13+
expect(htmlContent.includes("<div>development</div>")).toBe(true);
14+
});

0 commit comments

Comments
 (0)