Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/silent-seals-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@rspack/binding": patch
"@rspack/binding-darwin-arm64": patch
"@rspack/binding-darwin-x64": patch
"@rspack/binding-linux-x64-gnu": patch
"@rspack/binding-win32-x64-msvc": patch
"@rspack/core": patch
---

feat: inline external type syntax
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/node_binding/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ export interface RawExternalItem {
type: "string" | "regexp" | "object"
stringPayload?: string
regexpPayload?: string
objectPayload?: Record<string, string>
objectPayload?: Record<string, RawExternalItemValue>
}
export interface RawExternalItemValue {
type: "string" | "bool"
stringPayload?: string
boolPayload?: boolean
}
export interface RawExternalsPresets {
node: boolean
Expand Down
70 changes: 3 additions & 67 deletions crates/rspack_binding_options/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::{collections::HashMap, fmt::Debug};

use napi_derive::napi;
use rspack_core::{
BoxPlugin, CompilerOptions, DevServerOptions, Devtool, EntryItem, Experiments, ExternalItem,
ModuleOptions, OutputOptions, PluginExt, TargetPlatform,
BoxPlugin, CompilerOptions, DevServerOptions, Devtool, EntryItem, Experiments, ModuleOptions,
OutputOptions, PluginExt, TargetPlatform,
};
use rspack_regex::RspackRegex;
use serde::Deserialize;

mod raw_builtins;
Expand Down Expand Up @@ -145,7 +144,7 @@ impl RawOptionsApply for RawOptions {
);
}
if self.externals_presets.node {
plugins.push(node_target_plugin());
plugins.push(rspack_plugin_externals::node_target_plugin());
}
plugins.push(rspack_plugin_javascript::JsPlugin::new().boxed());
plugins.push(
Expand Down Expand Up @@ -185,66 +184,3 @@ impl RawOptionsApply for RawOptions {
})
}
}

fn node_target_plugin() -> BoxPlugin {
rspack_plugin_externals::ExternalPlugin::new(
"commonjs".to_string(), // TODO: should be "node-commonjs"
vec![
ExternalItem::from("assert".to_string()),
ExternalItem::from("assert/strict".to_string()),
ExternalItem::from("async_hooks".to_string()),
ExternalItem::from("buffer".to_string()),
ExternalItem::from("child_process".to_string()),
ExternalItem::from("cluster".to_string()),
ExternalItem::from("console".to_string()),
ExternalItem::from("constants".to_string()),
ExternalItem::from("crypto".to_string()),
ExternalItem::from("dgram".to_string()),
ExternalItem::from("diagnostics_channel".to_string()),
ExternalItem::from("dns".to_string()),
ExternalItem::from("dns/promises".to_string()),
ExternalItem::from("domain".to_string()),
ExternalItem::from("events".to_string()),
ExternalItem::from("fs".to_string()),
ExternalItem::from("fs/promises".to_string()),
ExternalItem::from("http".to_string()),
ExternalItem::from("http2".to_string()),
ExternalItem::from("https".to_string()),
ExternalItem::from("inspector".to_string()),
ExternalItem::from("module".to_string()),
ExternalItem::from("net".to_string()),
ExternalItem::from("os".to_string()),
ExternalItem::from("path".to_string()),
ExternalItem::from("path/posix".to_string()),
ExternalItem::from("path/win32".to_string()),
ExternalItem::from("perf_hooks".to_string()),
ExternalItem::from("process".to_string()),
ExternalItem::from("punycode".to_string()),
ExternalItem::from("querystring".to_string()),
ExternalItem::from("readline".to_string()),
ExternalItem::from("repl".to_string()),
ExternalItem::from("stream".to_string()),
ExternalItem::from("stream/promises".to_string()),
ExternalItem::from("stream/web".to_string()),
ExternalItem::from("string_decoder".to_string()),
ExternalItem::from("sys".to_string()),
ExternalItem::from("timers".to_string()),
ExternalItem::from("timers/promises".to_string()),
ExternalItem::from("tls".to_string()),
ExternalItem::from("trace_events".to_string()),
ExternalItem::from("tty".to_string()),
ExternalItem::from("url".to_string()),
ExternalItem::from("util".to_string()),
ExternalItem::from("util/types".to_string()),
ExternalItem::from("v8".to_string()),
ExternalItem::from("vm".to_string()),
ExternalItem::from("wasi".to_string()),
ExternalItem::from("worker_threads".to_string()),
ExternalItem::from("zlib".to_string()),
ExternalItem::from(RspackRegex::new("^node:").expect("Invalid regexp")),
// Yarn PnP adds pnpapi as "builtin"
ExternalItem::from("pnpapi".to_string()),
],
)
.boxed()
}
44 changes: 38 additions & 6 deletions crates/rspack_binding_options/src/options/raw_external.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use napi_derive::napi;
use rspack_core::ExternalItem;
use rspack_core::{ExternalItem, ExternalItemObject, ExternalItemValue};
use rspack_regex::RspackRegex;
use serde::Deserialize;

Expand All @@ -13,7 +13,35 @@ pub struct RawExternalItem {
pub r#type: String,
pub string_payload: Option<String>,
pub regexp_payload: Option<String>,
pub object_payload: Option<HashMap<String, String>>,
pub object_payload: Option<HashMap<String, RawExternalItemValue>>,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawExternalItemValue {
#[napi(ts_type = r#""string" | "bool""#)]
pub r#type: String,
pub string_payload: Option<String>,
pub bool_payload: Option<bool>,
}

impl From<RawExternalItemValue> for ExternalItemValue {
fn from(value: RawExternalItemValue) -> Self {
match value.r#type.as_str() {
"string" => Self::String(
value
.string_payload
.expect("should have a string_payload when RawExternalItemValue.type is \"string\""),
),
"bool" => Self::Bool(
value
.bool_payload
.expect("should have a bool_payload when RawExternalItemValue.type is \"bool\""),
),
_ => unreachable!(),
}
}
}

impl From<RawExternalItem> for ExternalItem {
Expand All @@ -32,11 +60,15 @@ impl From<RawExternalItem> for ExternalItem {
RspackRegex::new(&payload).expect("regex_payload is not a legal regex in rust side");
Self::from(reg)
}
"object" => Self::from(
value
"object" => {
let payload: ExternalItemObject = value
.object_payload
.expect("should have a object_payload when RawExternalItem.type is \"object\""),
),
.expect("should have a object_payload when RawExternalItem.type is \"object\"")
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect();
payload.into()
}
_ => unreachable!(),
}
}
Expand Down
18 changes: 13 additions & 5 deletions crates/rspack_core/src/options/externals.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use std::collections::HashMap;

use rspack_regex::RspackRegex;
use rustc_hash::FxHashMap as HashMap;

pub type Externals = Vec<ExternalItem>;

#[derive(Debug)]
pub enum ExternalItemValue {
String(String),
Bool(bool),
// TODO: string[] | Record<string, string|string[]>
}

pub type ExternalItemObject = HashMap<String, ExternalItemValue>;

#[derive(Debug)]
pub enum ExternalItem {
Object(HashMap<String, String>),
Object(ExternalItemObject),
String(String),
RegExp(RspackRegex),
}

impl From<HashMap<String, String>> for ExternalItem {
fn from(value: HashMap<String, String>) -> Self {
impl From<ExternalItemObject> for ExternalItem {
fn from(value: ExternalItemObject) -> Self {
Self::Object(value)
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_plugin_externals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ version = "0.1.0"

[dependencies]
async-trait = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_identifier = { path = "../rspack_identifier" }
rspack_regex = { path = "../rspack_regex" }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
4 changes: 4 additions & 0 deletions crates/rspack_plugin_externals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![feature(let_chains)]

mod node_target_plugin;
mod plugin;

pub use node_target_plugin::*;
pub use plugin::ExternalPlugin;
68 changes: 68 additions & 0 deletions crates/rspack_plugin_externals/src/node_target_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use rspack_core::{BoxPlugin, ExternalItem, PluginExt};
use rspack_regex::RspackRegex;

pub fn node_target_plugin() -> BoxPlugin {
crate::ExternalPlugin::new(
"commonjs".to_string(), // TODO: should be "node-commonjs"
vec![
ExternalItem::from("assert".to_string()),
ExternalItem::from("assert/strict".to_string()),
ExternalItem::from("async_hooks".to_string()),
ExternalItem::from("buffer".to_string()),
ExternalItem::from("child_process".to_string()),
ExternalItem::from("cluster".to_string()),
ExternalItem::from("console".to_string()),
ExternalItem::from("constants".to_string()),
ExternalItem::from("crypto".to_string()),
ExternalItem::from("dgram".to_string()),
ExternalItem::from("diagnostics_channel".to_string()),
ExternalItem::from("dns".to_string()),
ExternalItem::from("dns/promises".to_string()),
ExternalItem::from("domain".to_string()),
ExternalItem::from("events".to_string()),
ExternalItem::from("fs".to_string()),
ExternalItem::from("fs/promises".to_string()),
ExternalItem::from("http".to_string()),
ExternalItem::from("http2".to_string()),
ExternalItem::from("https".to_string()),
ExternalItem::from("inspector".to_string()),
ExternalItem::from("inspector/promises".to_string()),
ExternalItem::from("module".to_string()),
ExternalItem::from("net".to_string()),
ExternalItem::from("os".to_string()),
ExternalItem::from("path".to_string()),
ExternalItem::from("path/posix".to_string()),
ExternalItem::from("path/win32".to_string()),
ExternalItem::from("perf_hooks".to_string()),
ExternalItem::from("process".to_string()),
ExternalItem::from("punycode".to_string()),
ExternalItem::from("querystring".to_string()),
ExternalItem::from("readline".to_string()),
ExternalItem::from("readline/promises".to_string()),
ExternalItem::from("repl".to_string()),
ExternalItem::from("stream".to_string()),
ExternalItem::from("stream/consumers".to_string()),
ExternalItem::from("stream/promises".to_string()),
ExternalItem::from("stream/web".to_string()),
ExternalItem::from("string_decoder".to_string()),
ExternalItem::from("sys".to_string()),
ExternalItem::from("timers".to_string()),
ExternalItem::from("timers/promises".to_string()),
ExternalItem::from("tls".to_string()),
ExternalItem::from("trace_events".to_string()),
ExternalItem::from("tty".to_string()),
ExternalItem::from("url".to_string()),
ExternalItem::from("util".to_string()),
ExternalItem::from("util/types".to_string()),
ExternalItem::from("v8".to_string()),
ExternalItem::from("vm".to_string()),
ExternalItem::from("wasi".to_string()),
ExternalItem::from("worker_threads".to_string()),
ExternalItem::from("zlib".to_string()),
ExternalItem::from(RspackRegex::new("^node:").expect("Invalid regexp")),
// Yarn PnP adds pnpapi as "builtin"
ExternalItem::from("pnpapi".to_string()),
],
)
.boxed()
}
Loading