Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c6cae4

Browse files
authored
feat(rust/plugin): Support custom in PluginContextResolveOptions (#1876)
<!-- Thank you for contributing! --> ### Description Base infra for #1874. - `typedmap` is chosen due to have builtin support for `dashmap`. `anymap` requires wrapping in `Arc<Mutex<>>` and I don't want to do that. <!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
1 parent 6a61bad commit 0c6cae4

19 files changed

Lines changed: 168 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ bitflags = { version = "2.6.0" }
147147
oxc = { version = "0.23.0", features = ["sourcemap_concurrent", "transformer", "minifier", "semantic", "codegen"] }
148148
oxc_index = { version = "0.23.0" }
149149
oxc_transform_napi = { version = "0.23.0" }
150+
typedmap = "0.5.0"
150151
[profile.release]
151152
codegen-units = 1
152153
lto = true

crates/rolldown/src/stages/scan_stage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl ScanStage {
114114
true,
115115
ImportKind::Import,
116116
None,
117-
None,
117+
Arc::default(),
118118
)
119119
.await;
120120

crates/rolldown/src/types/module_factory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a> CreateModuleContext<'a> {
7070
false,
7171
kind,
7272
None,
73-
None,
73+
Arc::default(),
7474
)
7575
.await?;
7676

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod errors;
22
mod issues;
3+
mod plugin;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod plugin_context;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/rolldown_testing/src/integration_test.rs
3+
---
4+
# Assets
5+
6+
## entry.mjs
7+
8+
```js
9+
10+
//#region entry.js
11+
console.log(require("hello, world"));
12+
13+
//#endregion
14+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(require('foo'))
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use std::{borrow::Cow, sync::Arc};
2+
3+
use rolldown::{BundlerOptions, InputItem};
4+
use rolldown_plugin::{
5+
typedmap::{TypedDashMap, TypedMapKey},
6+
HookResolveIdArgs, HookResolveIdOutput, HookResolveIdReturn, Plugin, PluginContextResolveOptions,
7+
SharedPluginContext,
8+
};
9+
use rolldown_testing::{abs_file_dir, integration_test::IntegrationTest, test_config::TestMeta};
10+
#[derive(Debug)]
11+
struct TestPluginCaller;
12+
13+
#[derive(Hash, PartialEq, Eq)]
14+
struct MyArg {
15+
id: usize,
16+
}
17+
18+
impl TypedMapKey for MyArg {
19+
type Value = String;
20+
}
21+
22+
impl Plugin for TestPluginCaller {
23+
fn name(&self) -> Cow<'static, str> {
24+
"TestPluginCaller".into()
25+
}
26+
27+
async fn resolve_id(
28+
&self,
29+
ctx: &SharedPluginContext,
30+
args: &HookResolveIdArgs<'_>,
31+
) -> HookResolveIdReturn {
32+
if args.specifier == "foo" {
33+
let custom = TypedDashMap::default();
34+
custom.insert(MyArg { id: 0 }, "hello, world".to_string());
35+
let custom_resolve_ret = ctx
36+
.resolve(
37+
"test",
38+
None,
39+
Some(PluginContextResolveOptions { custom: Arc::new(custom), ..Default::default() }),
40+
)
41+
.await??;
42+
43+
if custom_resolve_ret.id == "hello, world" {
44+
Ok(Some(HookResolveIdOutput {
45+
id: "hello, world".to_string(),
46+
external: Some(true),
47+
..Default::default()
48+
}))
49+
} else {
50+
panic!("test")
51+
}
52+
} else {
53+
Ok(None)
54+
}
55+
}
56+
}
57+
58+
#[derive(Debug)]
59+
struct TestPluginReceiver;
60+
61+
impl Plugin for TestPluginReceiver {
62+
fn name(&self) -> Cow<'static, str> {
63+
"TestPluginReceiver".into()
64+
}
65+
66+
async fn resolve_id(
67+
&self,
68+
_ctx: &SharedPluginContext,
69+
args: &HookResolveIdArgs<'_>,
70+
) -> HookResolveIdReturn {
71+
if let Some(value) = args.custom.get::<MyArg>(&MyArg { id: 0 }) {
72+
assert_eq!(value.as_str(), "hello, world");
73+
return Ok(Some(HookResolveIdOutput { id: value.to_string(), ..Default::default() }));
74+
}
75+
Ok(None)
76+
}
77+
}
78+
79+
#[tokio::test(flavor = "multi_thread")]
80+
async fn allow_pass_custom_arg() {
81+
let cwd = abs_file_dir!();
82+
83+
IntegrationTest::new(TestMeta { expect_executed: false, ..Default::default() })
84+
.run_with_plugins(
85+
BundlerOptions {
86+
input: Some(vec![InputItem {
87+
name: Some("entry".to_string()),
88+
import: "./entry.js".to_string(),
89+
}]),
90+
cwd: Some(cwd),
91+
..Default::default()
92+
},
93+
vec![Arc::new(TestPluginCaller), Arc::new(TestPluginReceiver)],
94+
)
95+
.await;
96+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod custom_arg_in_resolve;

0 commit comments

Comments
 (0)