Skip to content

Commit 7ddaa9a

Browse files
authored
fix: use entry arg with match resource (#9235)
1 parent dcc4850 commit 7ddaa9a

File tree

8 files changed

+62
-10
lines changed

8 files changed

+62
-10
lines changed

crates/node_binding/binding.d.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1669,8 +1669,10 @@ export interface RawFlagAllModulesAsUsedPluginOptions {
16691669
export interface RawFuncUseCtx {
16701670
resource?: string
16711671
realResource?: string
1672-
resourceQuery?: string
1673-
issuer?: string
1672+
resourceQuery: string
1673+
resourceFragment: string
1674+
issuer: string
1675+
issuerLayer: string
16741676
}
16751677

16761678
export interface RawGeneratorOptions {

crates/rspack_binding_values/src/raw_options/raw_module/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,21 @@ impl Debug for RawModuleOptions {
745745
pub struct RawFuncUseCtx {
746746
pub resource: Option<String>,
747747
pub real_resource: Option<String>,
748-
pub resource_query: Option<String>,
749-
pub issuer: Option<String>,
748+
pub resource_query: String,
749+
pub resource_fragment: String,
750+
pub issuer: String,
751+
pub issuer_layer: String,
750752
}
751753

752754
impl From<FuncUseCtx> for RawFuncUseCtx {
753755
fn from(value: FuncUseCtx) -> Self {
754756
Self {
755757
resource: value.resource,
756758
real_resource: value.real_resource,
757-
resource_query: value.resource_query,
758-
issuer: value.issuer.map(|s| s.to_string()),
759+
resource_query: value.resource_query.unwrap_or_default(),
760+
resource_fragment: value.resource_fragment.unwrap_or_default(),
761+
issuer: value.issuer.map(|s| s.to_string()).unwrap_or_default(),
762+
issuer_layer: value.issuer_layer.unwrap_or_default(),
759763
}
760764
}
761765
}

crates/rspack_core/src/normal_module_factory.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,18 @@ impl NormalModuleFactory {
418418
let rule_use = match &rule.r#use {
419419
ModuleRuleUse::Array(array_use) => Cow::Borrowed(array_use),
420420
ModuleRuleUse::Func(func_use) => {
421+
let resource_data_for_rules = match_resource_data.as_ref().unwrap_or(&resource_data);
421422
let context = FuncUseCtx {
422423
// align with webpack https://github.com/webpack/webpack/blob/899f06934391baede59da3dcd35b5ef51c675dbe/lib/NormalModuleFactory.js#L576
423-
// resource shouldn't contain query otherwise it will cause duplicate query in https://github.com/unjs/unplugin/blob/62fdc5ae361d86a6ec39eaef5d8f01e12c6a794d/src/utils.ts#L58
424-
resource: resource_data.resource_path.clone().map(|x| x.to_string()),
425-
real_resource: Some(user_request.clone()),
424+
resource: resource_data_for_rules
425+
.resource_path
426+
.as_ref()
427+
.map(|x| x.to_string()),
428+
resource_query: resource_data_for_rules.resource_query.clone(),
429+
resource_fragment: resource_data_for_rules.resource_fragment.clone(),
430+
real_resource: resource_data.resource_path.as_ref().map(|p| p.to_string()),
426431
issuer: data.issuer.clone(),
427-
resource_query: resource_data.resource_query.clone(),
432+
issuer_layer: data.issuer_layer.clone(),
428433
};
429434
Cow::Owned(func_use(context).await?)
430435
}

crates/rspack_core/src/options/module.rs

+2
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,9 @@ pub struct FuncUseCtx {
908908
pub resource: Option<String>,
909909
pub real_resource: Option<String>,
910910
pub resource_query: Option<String>,
911+
pub resource_fragment: Option<String>,
911912
pub issuer: Option<Box<str>>,
913+
pub issuer_layer: Option<String>,
912914
}
913915

914916
#[derive(Debug, Clone)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { A, B, __info__, __context__ } from "__label__?A=A#B=B!=!./lib";
2+
3+
it("use entry arguments should be correct", () => {
4+
expect(A).toBe('A');
5+
expect(B).toBe('B');
6+
expect(__info__.resource).toBe("__label__");
7+
expect(__info__.realResource.includes("lib.js")).toBe(true);
8+
expect(__info__.resourceQuery).toBe("?A=A");
9+
expect(__info__.resourceFragment).toBe("#B=B");
10+
expect(__info__.issuer.includes("index.js")).toBe(true);
11+
expect(__info__.issuerLayer).toBe("");
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const A = 'A';
2+
export const B = 'B';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function (source) {
2+
const info = this.getOptions();
3+
delete info.compiler;
4+
return source + `
5+
export const __info__ = ${JSON.stringify(info, null, 2)};
6+
`
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
module: {
4+
rules: [
5+
{
6+
test: /__label__/,
7+
use: (info) => {
8+
return [
9+
{
10+
loader: "./loader.js",
11+
options: info,
12+
}
13+
]
14+
},
15+
}
16+
]
17+
}
18+
};

0 commit comments

Comments
 (0)