Skip to content

Commit

Permalink
refactor: remove as much getRawOptions as possible (#8773)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Dec 20, 2024
1 parent 0352d73 commit 28abee8
Show file tree
Hide file tree
Showing 21 changed files with 894 additions and 1,117 deletions.
63 changes: 21 additions & 42 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export declare class EntryOptionsDto {
get runtime(): false | string | undefined
set runtime(chunkLoading: boolean | string | undefined)
get chunkLoading(): string | undefined
set chunkLoading(chunkLoading: string | undefined)
set chunkLoading(chunkLoading: string | false | undefined)
get asyncChunks(): boolean | undefined
set asyncChunks(asyncChunks: boolean | undefined)
get baseUri(): string | undefined
Expand Down Expand Up @@ -612,7 +612,7 @@ export interface JsEntryData {
export interface JsEntryOptions {
name?: string
runtime?: false | string
chunkLoading?: string
chunkLoading?: false | string
asyncChunks?: boolean
publicPath?: "auto" | JsFilename
baseUri?: string
Expand Down Expand Up @@ -686,7 +686,7 @@ export interface JsLibraryAuxiliaryComment {
export interface JsLibraryCustomUmdObject {
amd?: string
commonjs?: string
root?: Array<string>
root?: Array<string> | string
}

export interface JsLibraryName {
Expand All @@ -697,11 +697,11 @@ export interface JsLibraryName {
}

export interface JsLibraryOptions {
name?: JsLibraryName
export?: Array<string>
libraryType: string
name?: string | Array<string> | JsLibraryCustomUmdObject
export?: Array<string> | string
type: string
umdNamedDefine?: boolean
auxiliaryComment?: JsLibraryAuxiliaryComment
auxiliaryComment?: string | JsLibraryAuxiliaryComment
amdContainer?: string
}

Expand Down Expand Up @@ -1198,14 +1198,6 @@ export interface RawCacheGroupOptions {

export interface RawCacheOptions {
type: string
maxGenerations: number
maxAge: number
profile: boolean
buildDependencies: Array<string>
cacheDirectory: string
cacheLocation: string
name: string
version: string
}

export interface RawConsumeOptions {
Expand Down Expand Up @@ -1274,12 +1266,6 @@ export interface RawCopyRspackPluginOptions {
patterns: Array<RawCopyPattern>
}

export interface RawCrossOriginLoading {
type: "bool" | "string"
stringPayload?: string
boolPayload?: boolean
}

export interface RawCssAutoGeneratorOptions {
exportsConvention?: "as-is" | "camel-case" | "camel-case-only" | "dashes" | "dashes-only"
exportsOnly?: boolean
Expand Down Expand Up @@ -1381,8 +1367,8 @@ export interface RawEvalDevToolModulePluginOptions {
sourceUrlComment?: string
}

export interface RawExperimentCacheOptionsCommon {
type: "disable"|"memory"
export interface RawExperimentCacheOptionsMemory {
type: "memory" | "disable"
}

export interface RawExperimentCacheOptionsPersistent {
Expand All @@ -1396,9 +1382,9 @@ export interface RawExperimentCacheOptionsPersistent {
export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
incremental?: RawIncremental
rspackFuture: RawRspackFuture
cache: RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsCommon
incremental?: false | { [key: string]: boolean }
rspackFuture?: RawRspackFuture
cache: RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsMemory | boolean
}

export interface RawExperimentSnapshotOptions {
Expand Down Expand Up @@ -1745,26 +1731,23 @@ export interface RawOccurrenceChunkIdsPluginOptions {

export interface RawOptimizationOptions {
removeAvailableModules: boolean
sideEffects: string
usedExports: string
sideEffects: boolean | string
usedExports: boolean | string
providedExports: boolean
innerGraph: boolean
mangleExports: string
mangleExports: boolean | string
concatenateModules: boolean
}

export interface RawOptions {
mode?: undefined | 'production' | 'development' | 'none'
target: Array<string>
context: string
output: RawOutputOptions
resolve: RawResolveOptions
resolveLoader: RawResolveOptions
module: RawModuleOptions
devtool: string
optimization: RawOptimizationOptions
stats: RawStatsOptions
snapshot: RawSnapshotOptions
cache: RawCacheOptions
experiments: RawExperiments
node?: RawNodeOption
Expand All @@ -1780,12 +1763,12 @@ export interface RawOutputOptions {
clean: boolean | JsCleanOptions
publicPath: "auto" | JsFilename
assetModuleFilename: JsFilename
wasmLoading: string
wasmLoading: string | false
enabledWasmLoadingTypes: Array<string>
webassemblyModuleFilename: string
filename: JsFilename
chunkFilename: JsFilename
crossOriginLoading: RawCrossOriginLoading
crossOriginLoading: string | false
cssFilename: JsFilename
cssChunkFilename: JsFilename
hotUpdateMainFilename: string
Expand All @@ -1801,7 +1784,7 @@ export interface RawOutputOptions {
importMetaName: string
iife: boolean
module: boolean
chunkLoading: string
chunkLoading: string | false
chunkLoadTimeout: number
charset: boolean
enabledChunkLoadingTypes?: Array<string>
Expand All @@ -1812,10 +1795,10 @@ export interface RawOutputOptions {
hashDigestLength: number
hashSalt?: string
asyncChunks: boolean
workerChunkLoading: string
workerWasmLoading: string
workerChunkLoading: string | false
workerWasmLoading: string | false
workerPublicPath: string
scriptType: "module" | "text/javascript" | "false"
scriptType: "module" | "text/javascript" | false
environment: RawEnvironment
compareBeforeEmit: boolean
}
Expand Down Expand Up @@ -1963,10 +1946,6 @@ export interface RawSizeLimitsPluginOptions {
maxEntrypointSize?: number
}

export interface RawSnapshotOptions {

}

export interface RawSourceMapDevToolPluginOptions {
append?: (false | null) | string | Function
columns?: boolean
Expand Down
16 changes: 10 additions & 6 deletions crates/rspack_binding_values/src/compilation/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rspack_napi::napi::bindgen_prelude::*;

use crate::{
dependency::JsDependency, entry::JsEntryOptions, library::JsLibraryOptions, JsDependencyWrapper,
RawChunkLoading, WithFalse,
};

#[napi]
Expand Down Expand Up @@ -58,12 +59,15 @@ impl EntryOptionsDTO {
}
}

#[napi(setter)]
pub fn set_chunk_loading(&mut self, chunk_loading: Either<String, ()>) {
self.0.chunk_loading = match chunk_loading {
Either::A(s) => Some(Into::into(s.as_str())),
Either::B(_) => None,
};
#[napi(setter, ts_type = "(chunkLoading: string | false | undefined)")]
pub fn set_chunk_loading(&mut self, chunk_loading: Either<RawChunkLoading, ()>) {
match chunk_loading {
Either::A(WithFalse::False) => self.0.chunk_loading = Some(ChunkLoading::Disable),
Either::A(WithFalse::True(s)) => {
self.0.chunk_loading = Some(ChunkLoading::Enable(s.as_str().into()))
}
Either::B(_) => self.0.chunk_loading = None,
}
}

#[napi(getter)]
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_binding_values/src/options/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use napi_derive::napi;
use rspack_core::{EntryOptions, EntryRuntime};

use super::library::JsLibraryOptions;
use crate::JsFilename;
use crate::{JsFilename, RawChunkLoading};

#[derive(Debug)]
#[napi(object, object_to_js = false)]
Expand Down Expand Up @@ -34,7 +34,8 @@ pub struct JsEntryOptions {
pub name: Option<String>,
#[napi(ts_type = "false | string")]
pub runtime: Option<JsEntryRuntime>,
pub chunk_loading: Option<String>,
#[napi(ts_type = "false | string")]
pub chunk_loading: Option<RawChunkLoading>,
pub async_chunks: Option<bool>,
#[napi(ts_type = "\"auto\" | JsFilename")]
pub public_path: Option<JsFilename>,
Expand All @@ -50,7 +51,7 @@ impl From<JsEntryOptions> for EntryOptions {
Self {
name: value.name,
runtime: value.runtime.map(|r| JsEntryRuntimeWrapper(r).into()),
chunk_loading: value.chunk_loading.as_deref().map(Into::into),
chunk_loading: value.chunk_loading.map(Into::into),
async_chunks: value.async_chunks,
public_path: value.public_path.map(Into::into),
base_uri: value.base_uri,
Expand Down
64 changes: 49 additions & 15 deletions crates/rspack_binding_values/src/options/library.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
use std::vec;

use napi::bindgen_prelude::Either3;
use napi_derive::napi;
use rspack_core::LibraryAuxiliaryComment;
use rspack_core::{LibraryCustomUmdObject, LibraryName, LibraryNonUmdObject, LibraryOptions};
use rspack_napi::napi::bindgen_prelude::Either;

#[derive(Debug)]
#[napi(object)]
pub struct JsLibraryCustomUmdObject {
pub amd: Option<String>,
pub commonjs: Option<String>,
pub root: Option<Vec<String>>,
pub root: Option<Either<Vec<String>, String>>,
}

impl From<JsLibraryCustomUmdObject> for LibraryCustomUmdObject {
fn from(value: JsLibraryCustomUmdObject) -> Self {
Self {
amd: value.amd,
commonjs: value.commonjs,
root: value.root,
root: value.root.map(|v| match v {
Either::A(v) => v,
Either::B(v) => vec![v],
}),
}
}
}
Expand All @@ -25,7 +32,7 @@ impl From<LibraryCustomUmdObject> for JsLibraryCustomUmdObject {
Self {
amd: value.amd,
commonjs: value.commonjs,
root: value.root,
root: value.root.map(Either::A),
}
}
}
Expand Down Expand Up @@ -100,6 +107,17 @@ pub struct JsLibraryAuxiliaryComment {
pub amd: Option<String>,
}

impl From<String> for JsLibraryAuxiliaryComment {
fn from(value: String) -> Self {
Self {
root: Some(value.clone()),
commonjs: Some(value.clone()),
commonjs2: Some(value.clone()),
amd: Some(value),
}
}
}

impl From<JsLibraryAuxiliaryComment> for LibraryAuxiliaryComment {
fn from(value: JsLibraryAuxiliaryComment) -> Self {
Self {
Expand All @@ -125,23 +143,33 @@ impl From<LibraryAuxiliaryComment> for JsLibraryAuxiliaryComment {
#[derive(Debug)]
#[napi(object)]
pub struct JsLibraryOptions {
pub name: Option<JsLibraryName>,
pub export: Option<Vec<String>>,
pub name: Option<Either3<String, Vec<String>, JsLibraryCustomUmdObject>>,
pub export: Option<Either<Vec<String>, String>>,
// webpack type
pub library_type: String,
pub r#type: String,
pub umd_named_define: Option<bool>,
pub auxiliary_comment: Option<JsLibraryAuxiliaryComment>,
pub auxiliary_comment: Option<Either<String, JsLibraryAuxiliaryComment>>,
pub amd_container: Option<String>,
}

impl From<JsLibraryOptions> for LibraryOptions {
fn from(value: JsLibraryOptions) -> Self {
Self {
name: value.name.map(Into::into),
export: value.export,
library_type: value.library_type,
name: value.name.map(|name| match name {
Either3::A(name) => LibraryName::NonUmdObject(LibraryNonUmdObject::String(name)),
Either3::B(names) => LibraryName::NonUmdObject(LibraryNonUmdObject::Array(names)),
Either3::C(umd_object) => LibraryName::UmdObject(umd_object.into()),
}),
export: value.export.map(|v| match v {
Either::A(v) => v,
Either::B(v) => vec![v],
}),
library_type: value.r#type,
umd_named_define: value.umd_named_define,
auxiliary_comment: value.auxiliary_comment.map(Into::into),
auxiliary_comment: value.auxiliary_comment.map(|v| match v {
Either::A(v) => Into::<JsLibraryAuxiliaryComment>::into(v).into(),
Either::B(v) => v.into(),
}),
amd_container: value.amd_container,
}
}
Expand All @@ -150,11 +178,17 @@ impl From<JsLibraryOptions> for LibraryOptions {
impl From<LibraryOptions> for JsLibraryOptions {
fn from(value: LibraryOptions) -> Self {
JsLibraryOptions {
name: value.name.map(|name| name.into()),
export: value.export,
library_type: value.library_type,
name: value.name.map(|name| match name {
LibraryName::NonUmdObject(LibraryNonUmdObject::String(name)) => Either3::A(name),
LibraryName::NonUmdObject(LibraryNonUmdObject::Array(names)) => Either3::B(names),
LibraryName::UmdObject(umd_object) => Either3::C(umd_object.into()),
}),
export: value.export.map(Either::A),
r#type: value.library_type,
umd_named_define: value.umd_named_define,
auxiliary_comment: value.auxiliary_comment.map(|comment| comment.into()),
auxiliary_comment: value
.auxiliary_comment
.map(|comment| Either::B(comment.into())),
amd_container: value.amd_container,
}
}
Expand Down
Loading

0 comments on commit 28abee8

Please sign in to comment.