Skip to content

Commit

Permalink
feat: support entryDescription.library (#4547)
Browse files Browse the repository at this point in the history
* feat: support entryDescription.library

* fix
  • Loading branch information
ahabhgk authored Nov 8, 2023
1 parent fa4a28b commit f3b27b9
Show file tree
Hide file tree
Showing 34 changed files with 795 additions and 425 deletions.
11 changes: 10 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ export interface RawEntryOptions {
publicPath?: string
baseUri?: string
filename?: string
library?: RawLibraryOptions
}

export interface RawEntryPluginOptions {
Expand Down Expand Up @@ -789,18 +790,26 @@ export interface RawLibraryAuxiliaryComment {
amd?: string
}

export interface RawLibraryName {
export interface RawLibraryCustomUmdObject {
amd?: string
commonjs?: string
root?: Array<string>
}

export interface RawLibraryName {
type: "string" | "array" | "umdObject"
stringPayload?: string
arrayPayload?: Array<string>
umdObjectPayload?: RawLibraryCustomUmdObject
}

export interface RawLibraryOptions {
name?: RawLibraryName
export?: Array<string>
libraryType: string
umdNamedDefine?: boolean
auxiliaryComment?: RawLibraryAuxiliaryComment
amdContainer?: string
}

export interface RawLimitChunkCountPluginOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl RawOptionsApply for BuiltinPlugin {
}

fn downcast_into<T: FromNapiValue + 'static>(o: JsUnknown) -> Result<T> {
<T as FromNapiValue>::from_unknown(o).into_rspack_result()
rspack_napi_shared::downcast_into(o).into_rspack_result()
}

// TO BE DEPRECATED
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_options/src/options/raw_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use napi_derive::napi;
use rspack_core::EntryOptions;
use serde::Deserialize;

use crate::RawLibraryOptions;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[napi(object)]
Expand All @@ -22,6 +24,7 @@ pub struct RawEntryOptions {
pub public_path: Option<String>,
pub base_uri: Option<String>,
pub filename: Option<String>,
pub library: Option<RawLibraryOptions>,
}

impl From<RawEntryOptions> for EntryOptions {
Expand All @@ -34,6 +37,7 @@ impl From<RawEntryOptions> for EntryOptions {
public_path: value.public_path.map(Into::into),
base_uri: value.base_uri,
filename: value.filename.map(Into::into),
library: value.library.map(Into::into),
}
}
}
46 changes: 43 additions & 3 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use napi_derive::napi;
use rspack_core::{BoxPlugin, CrossOriginLoading, LibraryName, LibraryOptions};
use rspack_core::{
BoxPlugin, CrossOriginLoading, LibraryCustomUmdObject, LibraryName, LibraryNonUmdObject,
LibraryOptions,
};
use rspack_core::{LibraryAuxiliaryComment, OutputOptions, TrustedTypes};
use serde::Deserialize;

Expand All @@ -24,13 +27,48 @@ impl From<RawTrustedTypes> for TrustedTypes {
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawLibraryName {
#[napi(ts_type = r#""string" | "array" | "umdObject""#)]
pub r#type: String,
pub string_payload: Option<String>,
pub array_payload: Option<Vec<String>>,
pub umd_object_payload: Option<RawLibraryCustomUmdObject>,
}

impl From<RawLibraryName> for LibraryName {
fn from(value: RawLibraryName) -> Self {
match value.r#type.as_str() {
"string" => {
Self::NonUmdObject(LibraryNonUmdObject::String(value.string_payload.expect(
"should have a string_payload when RawLibraryName.type is \"string\"",
)))
}
"array" => Self::NonUmdObject(LibraryNonUmdObject::Array(
value
.array_payload
.expect("should have a array_payload when RawLibraryName.type is \"array\""),
)),
"umdObject" => Self::UmdObject(
value
.umd_object_payload
.expect("should have a umd_object_payload when RawLibraryName.type is \"umdObject\"")
.into(),
),
_ => unreachable!(),
}
}
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawLibraryCustomUmdObject {
pub amd: Option<String>,
pub commonjs: Option<String>,
pub root: Option<Vec<String>>,
}

impl From<RawLibraryName> for LibraryName {
fn from(value: RawLibraryName) -> Self {
impl From<RawLibraryCustomUmdObject> for LibraryCustomUmdObject {
fn from(value: RawLibraryCustomUmdObject) -> Self {
Self {
amd: value.amd,
commonjs: value.commonjs,
Expand Down Expand Up @@ -70,6 +108,7 @@ pub struct RawLibraryOptions {
pub library_type: String,
pub umd_named_define: Option<bool>,
pub auxiliary_comment: Option<RawLibraryAuxiliaryComment>,
pub amd_container: Option<String>,
}

impl From<RawLibraryOptions> for LibraryOptions {
Expand All @@ -80,6 +119,7 @@ impl From<RawLibraryOptions> for LibraryOptions {
library_type: value.library_type,
umd_named_define: value.umd_named_define,
auxiliary_comment: value.auxiliary_comment.map(Into::into),
amd_container: value.amd_container,
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hash::FxHashSet as HashSet;

use crate::{
Chunk, ChunkByUkey, ChunkGroupByUkey, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation,
Filename, ModuleIdentifier, PublicPath, RuntimeSpec,
Filename, LibraryOptions, ModuleIdentifier, PublicPath, RuntimeSpec,
};

impl DatabaseItem for ChunkGroup {
Expand Down Expand Up @@ -241,11 +241,11 @@ impl ChunkGroup {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub enum ChunkGroupKind {
Entrypoint {
initial: bool,
options: EntryOptions,
options: Box<EntryOptions>,
},
Normal {
options: ChunkGroupOptions,
Expand All @@ -254,7 +254,10 @@ pub enum ChunkGroupKind {

impl ChunkGroupKind {
pub fn new_entrypoint(initial: bool, options: EntryOptions) -> Self {
Self::Entrypoint { initial, options }
Self::Entrypoint {
initial,
options: Box::new(options),
}
}

pub fn is_entrypoint(&self) -> bool {
Expand All @@ -276,7 +279,7 @@ impl ChunkGroupKind {
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone)]
pub struct EntryOptions {
pub name: Option<String>,
pub runtime: Option<String>,
Expand All @@ -285,6 +288,7 @@ pub struct EntryOptions {
pub public_path: Option<PublicPath>,
pub base_uri: Option<String>,
pub filename: Option<Filename>,
pub library: Option<LibraryOptions>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
Expand Down
29 changes: 23 additions & 6 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,26 +514,43 @@ pub fn get_js_chunk_filename_template<'filename>(
}
}

#[derive(Debug, Hash)]
#[derive(Debug, Clone, Hash)]
pub struct LibraryOptions {
pub name: Option<LibraryName>,
pub export: Option<Vec<String>>,
pub export: Option<LibraryExport>,
// webpack type
pub library_type: String,
pub library_type: LibraryType,
pub umd_named_define: Option<bool>,
pub auxiliary_comment: Option<LibraryAuxiliaryComment>,
pub amd_container: Option<String>,
}

#[derive(Debug, Hash)]
pub type LibraryType = String;

pub type LibraryExport = Vec<String>;

#[derive(Debug, Clone, Hash)]
pub struct LibraryAuxiliaryComment {
pub root: Option<String>,
pub commonjs: Option<String>,
pub commonjs2: Option<String>,
pub amd: Option<String>,
}

#[derive(Debug, Hash)]
pub struct LibraryName {
#[derive(Debug, Clone, Hash)]
pub enum LibraryName {
NonUmdObject(LibraryNonUmdObject),
UmdObject(LibraryCustomUmdObject),
}

#[derive(Debug, Clone, Hash)]
pub enum LibraryNonUmdObject {
Array(Vec<String>),
String(String),
}

#[derive(Debug, Clone, Hash)]
pub struct LibraryCustomUmdObject {
pub amd: Option<String>,
pub commonjs: Option<String>,
pub root: Option<Vec<String>>,
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/runtime_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ bitflags! {
const NODE_MODULE_DECORATOR = 1 << 47;

const HARMONY_MODULE_DECORATOR = 1 << 48;

/**
* the System.register context object
*/
const SYSTEM_CONTEXT = 1 << 49;
}
}

Expand Down Expand Up @@ -284,6 +289,7 @@ impl RuntimeGlobals {
R::CREATE_FAKE_NAMESPACE_OBJECT => "__webpack_require__.t",
R::HARMONY_MODULE_DECORATOR => "__webpack_require__.hmd",
R::NODE_MODULE_DECORATOR => "__webpack_require__.nmd",
R::SYSTEM_CONTEXT => "__webpack_require__.y",
r => panic!(
"Unexpected flag `{r:?}`. RuntimeGlobals should only be printed for one single flag."
),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_napi_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ thread_local! {
pub use crate::{
ext::{js_reg_exp_ext::JsRegExpExt, js_string_ext::JsStringExt},
js_values::js_reg_exp::JsRegExp,
utils::object_prototype_to_string_call,
utils::{downcast_into, object_prototype_to_string_call},
};
6 changes: 5 additions & 1 deletion crates/rspack_napi_shared/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ptr;

use napi::{bindgen_prelude::FromNapiValue, JsFunction, JsObject, JsString};
use napi::{bindgen_prelude::FromNapiValue, JsFunction, JsObject, JsString, JsUnknown};

fn get_js_global_object(env: napi::sys::napi_env) -> napi::Result<JsObject> {
let mut global = ptr::null_mut();
Expand All @@ -27,3 +27,7 @@ pub fn object_prototype_to_string_call(
};
type_description
}

pub fn downcast_into<T: FromNapiValue + 'static>(o: JsUnknown) -> napi::Result<T> {
<T as FromNapiValue>::from_unknown(o)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const WEBPACK_RESOURCE_QUERY: &str = "__resourceQuery";
pub const WEBPACK_CHUNK_LOAD: &str = "__webpack_chunk_load__";
pub const WEBPACK_BASE_URI: &str = "__webpack_base_uri__";
pub const NON_WEBPACK_REQUIRE: &str = "__non_webpack_require__";
pub const SYSTEM_CONTEXT: &str = "__system_context__";

pub struct ApiScanner<'a> {
pub unresolved_ctxt: SyntaxContext,
Expand Down Expand Up @@ -173,6 +174,14 @@ impl Visit for ApiScanner<'_> {
None,
)));
}
SYSTEM_CONTEXT => self
.presentational_dependencies
.push(Box::new(ConstDependency::new(
ident.span.real_lo(),
ident.span.real_hi(),
RuntimeGlobals::SYSTEM_CONTEXT.name().into(),
Some(RuntimeGlobals::SYSTEM_CONTEXT),
))),
_ => {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl<'a> WorkerScanner<'a> {
public_path: None,
base_uri: None,
filename: None,
library: None,
},
)));
if let Some(range) = range {
Expand Down
Loading

0 comments on commit f3b27b9

Please sign in to comment.