Skip to content

Commit 9cac130

Browse files
authored
fix: update asset can return undefined (#9530)
1 parent 9729f37 commit 9cac130

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

crates/node_binding/binding.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export declare class JsChunkGroup {
115115
}
116116

117117
export declare class JsCompilation {
118-
updateAsset(filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo)): void
118+
updateAsset(filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo | undefined)): void
119119
getAssets(): Readonly<JsAsset>[]
120120
getAsset(name: string): JsAsset | null
121121
getAssetSource(name: string): JsCompatSource | null

crates/node_binding/src/compilation/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ impl JsCompilation {
8080
#[napi]
8181
impl JsCompilation {
8282
#[napi(
83-
ts_args_type = r#"filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo)"#
83+
ts_args_type = r#"filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo | undefined)"#
8484
)]
8585
pub fn update_asset(
8686
&mut self,
8787
env: &Env,
8888
filename: String,
8989
new_source_or_function: Either<JsCompatSource, Function<'_, JsCompatSource, JsCompatSource>>,
90-
asset_info_update_or_function: Option<Either<AssetInfo, Function<'_, AssetInfo, AssetInfo>>>,
90+
asset_info_update_or_function: Option<
91+
Either<AssetInfo, Function<'_, AssetInfo, Option<AssetInfo>>>,
92+
>,
9193
) -> Result<()> {
9294
let compilation = self.as_mut()?;
9395

@@ -110,9 +112,12 @@ impl JsCompilation {
110112
.map(
111113
|asset_info_update_or_function| match asset_info_update_or_function {
112114
Either::A(asset_info) => Ok(asset_info.into()),
113-
Either::B(asset_info_fn) => {
114-
Ok(asset_info_fn.call(original_info.clone().into())?.into())
115-
}
115+
Either::B(asset_info_fn) => Ok(
116+
asset_info_fn
117+
.call(original_info.clone().into())?
118+
.map(Into::into)
119+
.unwrap_or_default(),
120+
),
116121
},
117122
)
118123
.transpose();

packages/rspack-test-tools/tests/configCases/hooks/update-asset-return-undefined/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
output: {
4+
filename: "[name].[contenthash].js"
5+
},
6+
plugins: [
7+
function plugin(compiler) {
8+
compiler.hooks.compilation.tap("test", compilation => {
9+
compilation.hooks.processAssets.tap(
10+
{
11+
name: "test",
12+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
13+
},
14+
assets => {
15+
Object.entries(assets).forEach(([filename, asset]) => {
16+
const newContent = `// UPDATED\n${asset.source()}`;
17+
compilation.updateAsset(
18+
filename,
19+
new compiler.webpack.sources.RawSource(newContent),
20+
() => { }
21+
);
22+
});
23+
}
24+
);
25+
compilation.hooks.processAssets.tap(
26+
{
27+
name: "test",
28+
stage:
29+
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
30+
},
31+
assets => {
32+
compilation.getAssets().forEach(({ info }) => {
33+
expect(info.contenthash.length).toBeGreaterThan(0);
34+
});
35+
}
36+
);
37+
});
38+
}
39+
]
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import("../../../..").TConfigCaseConfig} */
2+
module.exports = {
3+
noTests: true
4+
};

packages/rspack/etc/core.api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ export class Compilation {
851851
startTime?: number;
852852
// (undocumented)
853853
unseal(): void;
854-
updateAsset(filename: string, newSourceOrFunction: Source | ((source: Source) => Source), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo)): void;
854+
updateAsset(filename: string, newSourceOrFunction: Source | ((source: Source) => Source), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo | undefined)): void;
855855
// (undocumented)
856856
get warnings(): RspackError[];
857857
set warnings(warnings: RspackError[]);

packages/rspack/src/Compilation.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
576576
newSourceOrFunction: Source | ((source: Source) => Source),
577577
assetInfoUpdateOrFunction?:
578578
| AssetInfo
579-
| ((assetInfo: AssetInfo) => AssetInfo)
579+
| ((assetInfo: AssetInfo) => AssetInfo | undefined)
580580
) {
581581
let compatNewSourceOrFunction:
582582
| JsCompatSourceOwned
@@ -597,11 +597,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
597597
this.#inner.updateAsset(
598598
filename,
599599
compatNewSourceOrFunction,
600-
assetInfoUpdateOrFunction === undefined
601-
? assetInfoUpdateOrFunction
602-
: typeof assetInfoUpdateOrFunction === "function"
603-
? assetInfo => assetInfoUpdateOrFunction(assetInfo)
604-
: assetInfoUpdateOrFunction
600+
assetInfoUpdateOrFunction
605601
);
606602
}
607603

0 commit comments

Comments
 (0)