Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support object external item value #4234

Merged
merged 2 commits into from
Sep 25, 2023
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
3 changes: 2 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,11 @@ export interface RawExternalItemFnResult {
}

export interface RawExternalItemValue {
type: "string" | "bool" | "array"
type: "string" | "bool" | "array" | "object"
stringPayload?: string
boolPayload?: boolean
arrayPayload?: Array<string>
objectPayload?: Record<string, Array<string>>
}

export interface RawExternalsPluginOptions {
Expand Down
10 changes: 9 additions & 1 deletion crates/rspack_binding_options/src/options/raw_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ impl Debug for RawExternalItem {
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawExternalItemValue {
#[napi(ts_type = r#""string" | "bool" | "array""#)]
#[napi(ts_type = r#""string" | "bool" | "array" | "object""#)]
pub r#type: String,
pub string_payload: Option<String>,
pub bool_payload: Option<bool>,
pub array_payload: Option<Vec<String>>,
pub object_payload: Option<HashMap<String, Vec<String>>>,
}

impl From<RawExternalItemValue> for ExternalItemValue {
Expand All @@ -87,6 +88,13 @@ impl From<RawExternalItemValue> for ExternalItemValue {
.array_payload
.expect("should have a array_payload when RawExternalItemValue.type is \"array\""),
),
"object" => Self::Object(
value
.object_payload
.expect("should have a object_payload when RawExternalItemValue.type is \"object\"")
.into_iter()
.collect(),
),
_ => unreachable!(),
}
}
Expand Down
74 changes: 5 additions & 69 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use once_cell::sync::Lazy;
use regex::Regex;
use swc_core::ecma::atoms::JsWord;

use crate::{
Compilation, DependencyId, ExportsType, FakeNamespaceObjectMode, InitFragmentStage, ModuleGraph,
ModuleIdentifier, NormalInitFragment, RuntimeGlobals, TemplateContext,
property_access, Compilation, DependencyId, ExportsType, FakeNamespaceObjectMode,
InitFragmentStage, ModuleGraph, ModuleIdentifier, NormalInitFragment, RuntimeGlobals,
TemplateContext,
};

pub fn export_from_import(
Expand Down Expand Up @@ -32,7 +31,7 @@ pub fn export_from_import(
{
match exports_type {
ExportsType::Dynamic => {
return format!("{import_var}_default{}", property_access(&export_name, 1));
return format!("{import_var}_default{}", property_access(export_name, 1));
}
ExportsType::DefaultOnly | ExportsType::DefaultWithNamed => {
export_name = export_name[1..].to_vec();
Expand All @@ -41,7 +40,7 @@ pub fn export_from_import(
}
} else if !export_name.is_empty() {
if matches!(exports_type, ExportsType::DefaultOnly) {
return format!("/* non-default import from non-esm module */undefined\n{}", property_access(&export_name, 1));
return format!("/* non-default import from non-esm module */undefined\n{}", property_access(export_name, 1));
} else if !matches!(exports_type, ExportsType::Namespace) && let Some(first_export_name) = export_name.get(0) && first_export_name == "__esModule" {
return "/* __esModule */true".to_string();
}
Expand Down Expand Up @@ -102,69 +101,6 @@ pub fn get_exports_type_with_strict(
.get_exports_type(strict)
}

static SAFE_IDENTIFIER_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[_a-zA-Z$][_a-zA-Z$0-9]*$").expect("should init regex"));
const RESERVED_IDENTIFIER: [&str; 37] = [
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"package",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
];

fn property_access(o: &Vec<JsWord>, mut start: usize) -> String {
let mut str = String::default();
while start < o.len() {
let property = &o[start];
if SAFE_IDENTIFIER_REGEX.is_match(property) && !RESERVED_IDENTIFIER.contains(&property.as_ref())
{
str.push_str(format!(".{property}").as_str());
} else {
str.push_str(
format!(
"[{}]",
serde_json::to_string(property).expect("should render property")
)
.as_str(),
);
}
start += 1;
}
str
}

pub fn module_id_expr(request: &str, module_id: &str) -> String {
format!(
"/* {} */{}",
Expand Down
Loading