Skip to content

Commit

Permalink
fix: resolved some issues
Browse files Browse the repository at this point in the history
- dll-reference-plugin should given a manifest struct, rather than
json-string
- we should not add a null module_graph_module_connection like webpack
  • Loading branch information
GiveMe-A-Name committed Nov 8, 2024
1 parent 0c05841 commit 71811aa
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 127 deletions.
36 changes: 34 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ export interface JsBeforeResolveArgs {
issuer: string
}

export interface JsBuildMeta {
strictEsmModule: boolean
hasTopLevelAwait: boolean
esm: boolean
exportsType: 'unset' | 'default' | 'namespace' | 'flagged' | 'dynamic'
defaultObject: 'false' | 'redirect' | JsBuildMetaDefaultObjectRedirectWarn
moduleArgument: 'module' | 'webpackModule'
exportsArgument: 'exports' | 'webpackExports'
sideEffectFree?: boolean
exportsFinalName?: Array<[string, string]> | undefined
}

export interface JsBuildMetaDefaultObjectRedirectWarn {
redirectWarn: JsDefaultObjectRedirectWarnObject
}

export interface JsBuildTimeExecutionOption {
publicPath?: string
baseUri?: string
Expand Down Expand Up @@ -523,6 +539,10 @@ export interface JsCreateData {
resource: string
}

export interface JsDefaultObjectRedirectWarnObject {
ignore: boolean
}

export interface JsDiagnostic {
message: string
help?: string
Expand Down Expand Up @@ -1279,15 +1299,27 @@ export interface RawDllEntryPluginOptions {
name: string
}

export interface RawDllManifest {
content: RawDllManifestContent
name?: string
type?: string
}

export interface RawDllManifestContentItem {
buildMeta?: JsBuildMeta
exports?: Array<string>
id?: string
}

export interface RawDllReferenceAgencyPluginOptions {
context?: string
name?: string
extensions: Array<string>
scope?: string
sourceType?: string
type: string
content?: string
manifest?: string
content?: RawDllManifestContent
manifest?: RawDllManifest
}

export interface RawDraft {
Expand Down
67 changes: 59 additions & 8 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use napi_derive::napi;
use rspack_binding_values::JsFilename;
use rspack_binding_values::{JsBuildMeta, JsFilename};
use rspack_plugin_dll::{
DllEntryPluginOptions, DllReferenceAgencyPluginOptions, LibManifestPluginOptions,
DllEntryPluginOptions, DllManifest, DllManifestContent, DllManifestContentItem,
DllReferenceAgencyPluginOptions, LibManifestPluginOptions,
};
use rustc_hash::FxHashMap as HashMap;
use swc_core::atoms::Atom;

#[derive(Debug)]
#[napi(object)]
Expand Down Expand Up @@ -61,17 +64,61 @@ impl From<RawLibManifestPluginOptions> for LibManifestPluginOptions {
}
}

#[derive(Debug)]
#[napi(object)]
#[napi(object, object_to_js = false)]
pub struct RawDllReferenceAgencyPluginOptions {
pub context: Option<String>,
pub name: Option<String>,
pub extensions: Vec<String>,
pub scope: Option<String>,
pub source_type: Option<String>,
pub r#type: String,
pub content: Option<String>,
pub manifest: Option<String>,
pub content: Option<RawDllManifestContent>,
pub manifest: Option<RawDllManifest>,
}

type RawDllManifestContent = HashMap<String, RawDllManifestContentItem>;

#[napi(object, object_to_js = false)]
pub struct RawDllManifestContentItem {
pub build_meta: Option<JsBuildMeta>,
pub exports: Option<Vec<String>>,
pub id: Option<String>,
}

impl From<RawDllManifestContentItem> for DllManifestContentItem {
fn from(value: RawDllManifestContentItem) -> Self {
Self {
build_meta: value.build_meta.map(|meta| meta.into()),
exports: value.exports.map(|exports| {
exports
.into_iter()
.map(|export| Atom::from(export))
.collect::<Vec<_>>()
}),
id: value.id.into(),
}
}
}

#[napi(object, object_to_js = false)]
pub struct RawDllManifest {
pub content: RawDllManifestContent,
pub name: Option<String>,
pub r#type: Option<String>,
}

impl From<RawDllManifest> for DllManifest {
fn from(value: RawDllManifest) -> Self {
Self {
content: value
.content
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect::<DllManifestContent>(),
name: value.name.map(|n| n.into()),
r#type: value.r#type.into(),
}
}
}

impl From<RawDllReferenceAgencyPluginOptions> for DllReferenceAgencyPluginOptions {
Expand All @@ -94,8 +141,12 @@ impl From<RawDllReferenceAgencyPluginOptions> for DllReferenceAgencyPluginOption
scope,
source_type,
r#type,
content,
manifest,
content: content.map(|c| {
c.into_iter()
.map(|(k, v)| (k, v.into()))
.collect::<DllManifestContent>()
}),
manifest: manifest.map(|m| m.into()),
}
}
}
Expand Down
121 changes: 119 additions & 2 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{cell::RefCell, ptr::NonNull, sync::Arc};

use napi::bindgen_prelude::ToNapiValue;
use napi_derive::napi;
use rspack_collections::IdentifierMap;
use rspack_core::{
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, Compilation, CompilationId,
DependenciesBlock, Module, ModuleGraph, ModuleIdentifier, RuntimeModuleStage, SourceType,
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, BuildMeta, BuildMetaDefaultObject,
BuildMetaExportsType, Compilation, CompilationId, DependenciesBlock, ExportsArgument, Module,
ModuleArgument, ModuleGraph, ModuleIdentifier, RuntimeModuleStage, SourceType,
};
use rspack_napi::{napi::bindgen_prelude::*, threadsafe_function::ThreadsafeFunction, OneShotRef};
use rspack_plugin_runtime::RuntimeModuleFromJs;
Expand Down Expand Up @@ -534,3 +536,118 @@ impl From<JsAddingRuntimeModule> for RuntimeModuleFromJs {
}
}
}

#[napi(object, object_to_js = false)]
pub struct JsBuildMeta {
pub strict_esm_module: bool,
pub has_top_level_await: bool,
pub esm: bool,
#[napi(ts_type = "'unset' | 'default' | 'namespace' | 'flagged' | 'dynamic'")]
pub exports_type: String,
#[napi(ts_type = "'false' | 'redirect' | JsBuildMetaDefaultObjectRedirectWarn")]
pub default_object: JsBuildMetaDefaultObject,
#[napi(ts_type = "'module' | 'webpackModule'")]
pub module_argument: String,
#[napi(ts_type = "'exports' | 'webpackExports'")]
pub exports_argument: String,
pub side_effect_free: Option<bool>,
#[napi(ts_type = "Array<[string, string]> | undefined")]
pub exports_final_name: Option<Vec<Vec<String>>>,
}

impl From<JsBuildMeta> for BuildMeta {
fn from(value: JsBuildMeta) -> Self {
let JsBuildMeta {
strict_esm_module,
has_top_level_await,
esm,
exports_argument: raw_exports_argument,
default_object: raw_default_object,
module_argument: raw_module_argument,
exports_final_name: raw_exports_final_name,
side_effect_free,
exports_type: raw_exports_type,
} = value;

let default_object = match raw_default_object {
Either::A(s) => match s.as_str() {
"false" => BuildMetaDefaultObject::False,
"redirect" => BuildMetaDefaultObject::Redirect,
_ => unreachable!(),
},
Either::B(default_object) => BuildMetaDefaultObject::RedirectWarn {

Check failure on line 578 in crates/rspack_binding_values/src/module.rs

View workflow job for this annotation

GitHub Actions / Rust check

accessing first element with `export_name.get(0)`
ignore: default_object.redirect_warn.ignore,
},
};

let exports_type = match raw_exports_type.as_str() {
"unset" => BuildMetaExportsType::Unset,
"default" => BuildMetaExportsType::Default,
"namespace" => BuildMetaExportsType::Namespace,
"flagged" => BuildMetaExportsType::Flagged,
"dynamic" => BuildMetaExportsType::Dynamic,
_ => unreachable!(),
};

let module_argument = match raw_module_argument.as_str() {
"module" => ModuleArgument::Module,
"webpackModule" => ModuleArgument::WebpackModule,
_ => unreachable!(),
};

let exports_argument = match raw_exports_argument.as_str() {
"exports" => ExportsArgument::Exports,
"webpackExports" => ExportsArgument::WebpackExports,
_ => unreachable!(),
};

let exports_final_name = raw_exports_final_name.map(|exports_name| {
exports_name
.into_iter()
.map(|export_name| {
let first = export_name
.get(0)
.expect("The buildMeta exportsFinalName item should have first value")
.clone();
let second = export_name
.get(1)
.expect("The buildMeta exportsFinalName item should have second value")
.clone();
(first, second)
})
.collect::<Vec<_>>()
});

Self {
strict_esm_module,
has_top_level_await,
esm,
exports_type,
default_object,
module_argument,
exports_argument,
side_effect_free,
exports_final_name,
}
}
}

#[napi(object)]
pub struct JsBuildMetaDefaultObjectRedirectWarn {
pub redirect_warn: JsDefaultObjectRedirectWarnObject,
}

impl From<JsBuildMetaDefaultObjectRedirectWarn> for BuildMetaDefaultObject {
fn from(value: JsBuildMetaDefaultObjectRedirectWarn) -> Self {
Self::RedirectWarn {
ignore: value.redirect_warn.ignore,
}
}
}

#[napi(object)]
pub struct JsDefaultObjectRedirectWarnObject {
pub ignore: bool,
}

pub type JsBuildMetaDefaultObject = Either<String, JsBuildMetaDefaultObjectRedirectWarn>;
18 changes: 12 additions & 6 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rspack_util::atom::Atom;
use rspack_util::ext::{AsAny, DynHash};
use rspack_util::source_map::ModuleSourceMapConfig;
use rustc_hash::FxHashSet as HashSet;
use serde::{Deserialize, Serialize};
use serde::Serialize;

use crate::concatenated_module::ConcatenatedModule;
use crate::dependencies_block::dependencies_block_update_hash;
Expand Down Expand Up @@ -79,7 +79,8 @@ impl Default for BuildInfo {
}
}

#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum BuildMetaExportsType {
#[default]
Unset,
Expand All @@ -97,7 +98,8 @@ pub enum ExportsType {
Dynamic,
}

#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Copy, Hash, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum BuildMetaDefaultObject {
#[default]
False,
Expand All @@ -111,7 +113,8 @@ pub enum BuildMetaDefaultObject {
},
}

#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Copy, Hash, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ModuleArgument {
#[default]
Module,
Expand All @@ -127,7 +130,8 @@ impl Display for ModuleArgument {
}
}

#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ExportsArgument {
#[default]
Exports,
Expand All @@ -143,7 +147,7 @@ impl Display for ExportsArgument {
}
}

#[derive(Debug, Default, Clone, Hash, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Hash, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildMeta {
pub strict_esm_module: bool,
Expand All @@ -153,7 +157,9 @@ pub struct BuildMeta {
pub default_object: BuildMetaDefaultObject,
pub module_argument: ModuleArgument,
pub exports_argument: ExportsArgument,
#[serde(skip_serializing_if = "Option::is_none")]
pub side_effect_free: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exports_final_name: Option<Vec<(String, String)>>,
}

Expand Down
18 changes: 0 additions & 18 deletions crates/rspack_core/src/module_graph/connection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::hash::Hash;

use itertools::Itertools;
use rustc_hash::FxHashSet as HashSet;

use crate::{DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec};

#[derive(Debug, Clone, Eq)]
Expand All @@ -17,8 +14,6 @@ pub struct ModuleGraphConnection {

pub active: bool,
pub conditional: bool,

explanations: HashSet<String>,
}

impl Hash for ModuleGraphConnection {
Expand Down Expand Up @@ -48,19 +43,6 @@ impl ModuleGraphConnection {
active,
conditional,
resolved_original_module_identifier: original_module_identifier,
explanations: Default::default(),
}
}

pub fn add_explanation(&mut self, explanation: String) {
self.explanations.insert(explanation);
}

pub fn explanation(&self) -> Option<String> {
if self.explanations.is_empty() {
None
} else {
Some(self.explanations.iter().join(" "))
}
}

Expand Down
Loading

0 comments on commit 71811aa

Please sign in to comment.