Skip to content

Commit b0cffba

Browse files
authored
feat: inline external type syntax (#2437)
1 parent 9589324 commit b0cffba

File tree

17 files changed

+246
-106
lines changed

17 files changed

+246
-106
lines changed

.changeset/silent-seals-rhyme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@rspack/binding": patch
3+
"@rspack/binding-darwin-arm64": patch
4+
"@rspack/binding-darwin-x64": patch
5+
"@rspack/binding-linux-x64-gnu": patch
6+
"@rspack/binding-win32-x64-msvc": patch
7+
"@rspack/core": patch
8+
---
9+
10+
feat: inline external type syntax

Cargo.lock

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

crates/node_binding/Cargo.lock

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

crates/node_binding/binding.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ export interface RawExternalItem {
165165
type: "string" | "regexp" | "object"
166166
stringPayload?: string
167167
regexpPayload?: string
168-
objectPayload?: Record<string, string>
168+
objectPayload?: Record<string, RawExternalItemValue>
169+
}
170+
export interface RawExternalItemValue {
171+
type: "string" | "bool"
172+
stringPayload?: string
173+
boolPayload?: boolean
169174
}
170175
export interface RawExternalsPresets {
171176
node: boolean

crates/rspack_binding_options/src/options/mod.rs

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::{collections::HashMap, fmt::Debug};
22

33
use napi_derive::napi;
44
use rspack_core::{
5-
BoxPlugin, CompilerOptions, DevServerOptions, Devtool, EntryItem, Experiments, ExternalItem,
6-
ModuleOptions, OutputOptions, PluginExt, TargetPlatform,
5+
BoxPlugin, CompilerOptions, DevServerOptions, Devtool, EntryItem, Experiments, ModuleOptions,
6+
OutputOptions, PluginExt, TargetPlatform,
77
};
8-
use rspack_regex::RspackRegex;
98
use serde::Deserialize;
109

1110
mod raw_builtins;
@@ -145,7 +144,7 @@ impl RawOptionsApply for RawOptions {
145144
);
146145
}
147146
if self.externals_presets.node {
148-
plugins.push(node_target_plugin());
147+
plugins.push(rspack_plugin_externals::node_target_plugin());
149148
}
150149
plugins.push(rspack_plugin_javascript::JsPlugin::new().boxed());
151150
plugins.push(
@@ -185,66 +184,3 @@ impl RawOptionsApply for RawOptions {
185184
})
186185
}
187186
}
188-
189-
fn node_target_plugin() -> BoxPlugin {
190-
rspack_plugin_externals::ExternalPlugin::new(
191-
"commonjs".to_string(), // TODO: should be "node-commonjs"
192-
vec![
193-
ExternalItem::from("assert".to_string()),
194-
ExternalItem::from("assert/strict".to_string()),
195-
ExternalItem::from("async_hooks".to_string()),
196-
ExternalItem::from("buffer".to_string()),
197-
ExternalItem::from("child_process".to_string()),
198-
ExternalItem::from("cluster".to_string()),
199-
ExternalItem::from("console".to_string()),
200-
ExternalItem::from("constants".to_string()),
201-
ExternalItem::from("crypto".to_string()),
202-
ExternalItem::from("dgram".to_string()),
203-
ExternalItem::from("diagnostics_channel".to_string()),
204-
ExternalItem::from("dns".to_string()),
205-
ExternalItem::from("dns/promises".to_string()),
206-
ExternalItem::from("domain".to_string()),
207-
ExternalItem::from("events".to_string()),
208-
ExternalItem::from("fs".to_string()),
209-
ExternalItem::from("fs/promises".to_string()),
210-
ExternalItem::from("http".to_string()),
211-
ExternalItem::from("http2".to_string()),
212-
ExternalItem::from("https".to_string()),
213-
ExternalItem::from("inspector".to_string()),
214-
ExternalItem::from("module".to_string()),
215-
ExternalItem::from("net".to_string()),
216-
ExternalItem::from("os".to_string()),
217-
ExternalItem::from("path".to_string()),
218-
ExternalItem::from("path/posix".to_string()),
219-
ExternalItem::from("path/win32".to_string()),
220-
ExternalItem::from("perf_hooks".to_string()),
221-
ExternalItem::from("process".to_string()),
222-
ExternalItem::from("punycode".to_string()),
223-
ExternalItem::from("querystring".to_string()),
224-
ExternalItem::from("readline".to_string()),
225-
ExternalItem::from("repl".to_string()),
226-
ExternalItem::from("stream".to_string()),
227-
ExternalItem::from("stream/promises".to_string()),
228-
ExternalItem::from("stream/web".to_string()),
229-
ExternalItem::from("string_decoder".to_string()),
230-
ExternalItem::from("sys".to_string()),
231-
ExternalItem::from("timers".to_string()),
232-
ExternalItem::from("timers/promises".to_string()),
233-
ExternalItem::from("tls".to_string()),
234-
ExternalItem::from("trace_events".to_string()),
235-
ExternalItem::from("tty".to_string()),
236-
ExternalItem::from("url".to_string()),
237-
ExternalItem::from("util".to_string()),
238-
ExternalItem::from("util/types".to_string()),
239-
ExternalItem::from("v8".to_string()),
240-
ExternalItem::from("vm".to_string()),
241-
ExternalItem::from("wasi".to_string()),
242-
ExternalItem::from("worker_threads".to_string()),
243-
ExternalItem::from("zlib".to_string()),
244-
ExternalItem::from(RspackRegex::new("^node:").expect("Invalid regexp")),
245-
// Yarn PnP adds pnpapi as "builtin"
246-
ExternalItem::from("pnpapi".to_string()),
247-
],
248-
)
249-
.boxed()
250-
}

crates/rspack_binding_options/src/options/raw_external.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use napi_derive::napi;
4-
use rspack_core::ExternalItem;
4+
use rspack_core::{ExternalItem, ExternalItemObject, ExternalItemValue};
55
use rspack_regex::RspackRegex;
66
use serde::Deserialize;
77

@@ -13,7 +13,35 @@ pub struct RawExternalItem {
1313
pub r#type: String,
1414
pub string_payload: Option<String>,
1515
pub regexp_payload: Option<String>,
16-
pub object_payload: Option<HashMap<String, String>>,
16+
pub object_payload: Option<HashMap<String, RawExternalItemValue>>,
17+
}
18+
19+
#[derive(Deserialize, Debug, Clone)]
20+
#[serde(rename_all = "camelCase")]
21+
#[napi(object)]
22+
pub struct RawExternalItemValue {
23+
#[napi(ts_type = r#""string" | "bool""#)]
24+
pub r#type: String,
25+
pub string_payload: Option<String>,
26+
pub bool_payload: Option<bool>,
27+
}
28+
29+
impl From<RawExternalItemValue> for ExternalItemValue {
30+
fn from(value: RawExternalItemValue) -> Self {
31+
match value.r#type.as_str() {
32+
"string" => Self::String(
33+
value
34+
.string_payload
35+
.expect("should have a string_payload when RawExternalItemValue.type is \"string\""),
36+
),
37+
"bool" => Self::Bool(
38+
value
39+
.bool_payload
40+
.expect("should have a bool_payload when RawExternalItemValue.type is \"bool\""),
41+
),
42+
_ => unreachable!(),
43+
}
44+
}
1745
}
1846

1947
impl From<RawExternalItem> for ExternalItem {
@@ -32,11 +60,15 @@ impl From<RawExternalItem> for ExternalItem {
3260
RspackRegex::new(&payload).expect("regex_payload is not a legal regex in rust side");
3361
Self::from(reg)
3462
}
35-
"object" => Self::from(
36-
value
63+
"object" => {
64+
let payload: ExternalItemObject = value
3765
.object_payload
38-
.expect("should have a object_payload when RawExternalItem.type is \"object\""),
39-
),
66+
.expect("should have a object_payload when RawExternalItem.type is \"object\"")
67+
.into_iter()
68+
.map(|(k, v)| (k, v.into()))
69+
.collect();
70+
payload.into()
71+
}
4072
_ => unreachable!(),
4173
}
4274
}

crates/rspack_core/src/options/externals.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
use std::collections::HashMap;
2-
31
use rspack_regex::RspackRegex;
2+
use rustc_hash::FxHashMap as HashMap;
43

54
pub type Externals = Vec<ExternalItem>;
65

6+
#[derive(Debug)]
7+
pub enum ExternalItemValue {
8+
String(String),
9+
Bool(bool),
10+
// TODO: string[] | Record<string, string|string[]>
11+
}
12+
13+
pub type ExternalItemObject = HashMap<String, ExternalItemValue>;
14+
715
#[derive(Debug)]
816
pub enum ExternalItem {
9-
Object(HashMap<String, String>),
17+
Object(ExternalItemObject),
1018
String(String),
1119
RegExp(RspackRegex),
1220
}
1321

14-
impl From<HashMap<String, String>> for ExternalItem {
15-
fn from(value: HashMap<String, String>) -> Self {
22+
impl From<ExternalItemObject> for ExternalItem {
23+
fn from(value: ExternalItemObject) -> Self {
1624
Self::Object(value)
1725
}
1826
}

crates/rspack_plugin_externals/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ version = "0.1.0"
99

1010
[dependencies]
1111
async-trait = { workspace = true }
12+
once_cell = { workspace = true }
13+
regex = { workspace = true }
1214
rspack_core = { path = "../rspack_core" }
1315
rspack_error = { path = "../rspack_error" }
1416
rspack_identifier = { path = "../rspack_identifier" }
17+
rspack_regex = { path = "../rspack_regex" }
1518
tracing = { workspace = true }
1619
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![feature(let_chains)]
2+
3+
mod node_target_plugin;
14
mod plugin;
25

6+
pub use node_target_plugin::*;
37
pub use plugin::ExternalPlugin;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use rspack_core::{BoxPlugin, ExternalItem, PluginExt};
2+
use rspack_regex::RspackRegex;
3+
4+
pub fn node_target_plugin() -> BoxPlugin {
5+
crate::ExternalPlugin::new(
6+
"commonjs".to_string(), // TODO: should be "node-commonjs"
7+
vec![
8+
ExternalItem::from("assert".to_string()),
9+
ExternalItem::from("assert/strict".to_string()),
10+
ExternalItem::from("async_hooks".to_string()),
11+
ExternalItem::from("buffer".to_string()),
12+
ExternalItem::from("child_process".to_string()),
13+
ExternalItem::from("cluster".to_string()),
14+
ExternalItem::from("console".to_string()),
15+
ExternalItem::from("constants".to_string()),
16+
ExternalItem::from("crypto".to_string()),
17+
ExternalItem::from("dgram".to_string()),
18+
ExternalItem::from("diagnostics_channel".to_string()),
19+
ExternalItem::from("dns".to_string()),
20+
ExternalItem::from("dns/promises".to_string()),
21+
ExternalItem::from("domain".to_string()),
22+
ExternalItem::from("events".to_string()),
23+
ExternalItem::from("fs".to_string()),
24+
ExternalItem::from("fs/promises".to_string()),
25+
ExternalItem::from("http".to_string()),
26+
ExternalItem::from("http2".to_string()),
27+
ExternalItem::from("https".to_string()),
28+
ExternalItem::from("inspector".to_string()),
29+
ExternalItem::from("inspector/promises".to_string()),
30+
ExternalItem::from("module".to_string()),
31+
ExternalItem::from("net".to_string()),
32+
ExternalItem::from("os".to_string()),
33+
ExternalItem::from("path".to_string()),
34+
ExternalItem::from("path/posix".to_string()),
35+
ExternalItem::from("path/win32".to_string()),
36+
ExternalItem::from("perf_hooks".to_string()),
37+
ExternalItem::from("process".to_string()),
38+
ExternalItem::from("punycode".to_string()),
39+
ExternalItem::from("querystring".to_string()),
40+
ExternalItem::from("readline".to_string()),
41+
ExternalItem::from("readline/promises".to_string()),
42+
ExternalItem::from("repl".to_string()),
43+
ExternalItem::from("stream".to_string()),
44+
ExternalItem::from("stream/consumers".to_string()),
45+
ExternalItem::from("stream/promises".to_string()),
46+
ExternalItem::from("stream/web".to_string()),
47+
ExternalItem::from("string_decoder".to_string()),
48+
ExternalItem::from("sys".to_string()),
49+
ExternalItem::from("timers".to_string()),
50+
ExternalItem::from("timers/promises".to_string()),
51+
ExternalItem::from("tls".to_string()),
52+
ExternalItem::from("trace_events".to_string()),
53+
ExternalItem::from("tty".to_string()),
54+
ExternalItem::from("url".to_string()),
55+
ExternalItem::from("util".to_string()),
56+
ExternalItem::from("util/types".to_string()),
57+
ExternalItem::from("v8".to_string()),
58+
ExternalItem::from("vm".to_string()),
59+
ExternalItem::from("wasi".to_string()),
60+
ExternalItem::from("worker_threads".to_string()),
61+
ExternalItem::from("zlib".to_string()),
62+
ExternalItem::from(RspackRegex::new("^node:").expect("Invalid regexp")),
63+
// Yarn PnP adds pnpapi as "builtin"
64+
ExternalItem::from("pnpapi".to_string()),
65+
],
66+
)
67+
.boxed()
68+
}

0 commit comments

Comments
 (0)