From 98b1339acc7fe78ca2f8e237b1218b7e1bc802df Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 9 Jul 2024 11:41:22 +0800 Subject: [PATCH] fix: remove unwrap --- crates/node_binding/binding.d.ts | 2 +- crates/rspack_binding_values/src/resolver.rs | 36 +++++++----- .../src/resolver_factory.rs | 19 +++--- packages/rspack/etc/api.md | 58 ++++++++++++++++--- packages/rspack/src/Compilation.ts | 18 +++--- packages/rspack/src/Compiler.ts | 4 +- 6 files changed, 93 insertions(+), 44 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index fea97846b9b..2e3a35a4e82 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -63,7 +63,7 @@ export class JsCompilation { export class JsResolver { resolveSync(path: string, request: string): string | false - withOptions(raw?: RawResolveOptionsWithDependencyType | undefined | null): JsResolver + withOptions(raw?: RawResolveOptionsWithDependencyType | undefined | null): this } export class JsResolverFactory { diff --git a/crates/rspack_binding_values/src/resolver.rs b/crates/rspack_binding_values/src/resolver.rs index 8de88238dd5..45463020814 100644 --- a/crates/rspack_binding_values/src/resolver.rs +++ b/crates/rspack_binding_values/src/resolver.rs @@ -44,21 +44,27 @@ impl JsResolver { } #[napi] - pub fn with_options(&self, raw: Option) -> Self { - let mut options = - normalize_raw_resolve_options_with_dependency_type(raw, self.options.resolve_to_context) - .unwrap(); + pub fn with_options( + &self, + raw: Option, + ) -> napi::Result { + let options = + normalize_raw_resolve_options_with_dependency_type(raw, self.options.resolve_to_context); + match options { + Ok(mut options) => { + options.resolve_options = match options.resolve_options.take() { + Some(resolve_options) => match &self.options.resolve_options { + Some(origin_resolve_options) => Some(Box::new( + resolve_options.merge(*origin_resolve_options.clone()), + )), + None => Some(resolve_options), + }, + None => self.options.resolve_options.clone(), + }; - options.resolve_options = match options.resolve_options.take() { - Some(resolve_options) => match &self.options.resolve_options { - Some(origin_resolve_options) => Some(Box::new( - resolve_options.merge(*origin_resolve_options.clone()), - )), - None => Some(resolve_options), - }, - None => self.options.resolve_options.clone(), - }; - - Self::new(self.resolver_factory.clone(), options) + Ok(Self::new(self.resolver_factory.clone(), options)) + } + Err(e) => Err(napi::Error::from_reason(format!("{e}"))), + } } } diff --git a/crates/rspack_binding_values/src/resolver_factory.rs b/crates/rspack_binding_values/src/resolver_factory.rs index 1cc34d766f4..fe8e9da386f 100644 --- a/crates/rspack_binding_values/src/resolver_factory.rs +++ b/crates/rspack_binding_values/src/resolver_factory.rs @@ -33,23 +33,24 @@ impl JsResolverFactory { &self, r#type: String, raw: Option, - ) -> JsResolver { - match r#type.as_str() { + ) -> napi::Result { + let (options, resolver_factory) = match r#type.as_str() { "normal" => { - let options = normalize_raw_resolve_options_with_dependency_type(raw, false).unwrap(); - JsResolver::new(self.resolver_factory.clone(), options) + (normalize_raw_resolve_options_with_dependency_type(raw, false), self.resolver_factory.clone()) } "loader" => { - let options = normalize_raw_resolve_options_with_dependency_type(raw, false).unwrap(); - JsResolver::new(self.loader_resolver_factory.clone(), options) + (normalize_raw_resolve_options_with_dependency_type(raw, false), self.loader_resolver_factory.clone()) } "context" => { - let options = normalize_raw_resolve_options_with_dependency_type(raw, true).unwrap(); - JsResolver::new(self.resolver_factory.clone(), options) + (normalize_raw_resolve_options_with_dependency_type(raw, true), self.resolver_factory.clone()) } _ => { - panic!("Invalid resolver type '{}' specified. Rspack only supports 'normal', 'context', and 'loader' types.", r#type) + return Err(napi::Error::from_reason(format!("Invalid resolver type '{}' specified. Rspack only supports 'normal', 'context', and 'loader' types.", r#type))) } + }; + match options { + Ok(options) => Ok(JsResolver::new(resolver_factory, options)), + Err(e) => Err(napi::Error::from_reason(format!("{e}"))), } } } diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 2e6750f3d2d..267ad42d412 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -59,7 +59,6 @@ import { RawRuntimeChunkOptions } from '@rspack/binding'; import { RawSourceMapDevToolPluginOptions } from '@rspack/binding'; import { RawSwcJsMinimizerRspackPluginOptions } from '@rspack/binding'; import { registerGlobalTrace } from '@rspack/binding'; -import ResolverFactory = require('./ResolverFactory'); import { RspackOptionsNormalized as RspackOptionsNormalized_2 } from '.'; import sources = require('../compiled/webpack-sources'); import { SyncBailHook } from '@rspack/lite-tapable'; @@ -909,6 +908,8 @@ export class Compilation { // @internal __internal__setAssetSource(filename: string, source: Source): void; // @internal + __internal_get_resolver_factory(): binding.JsResolverFactory; + // @internal __internal_getInner(): binding.JsCompilation; get assets(): Record; // (undocumented) @@ -1085,7 +1086,7 @@ export class Compilation { // (undocumented) renameAsset(filename: string, newFilename: string): void; // (undocumented) - resolverFactory: ResolverFactory; + get resolverFactory(): ResolverFactory; // (undocumented) seal(): void; // (undocumented) @@ -1216,8 +1217,6 @@ export class Compiler { // (undocumented) removedFiles?: ReadonlySet; // (undocumented) - resolverFactory: ResolverFactory; - // (undocumented) root: Compiler; // (undocumented) run(callback: liteTapable.Callback): void; @@ -2836,6 +2835,11 @@ export class EnvironmentPlugin { keys: any[]; } +// @public (undocumented) +type ErrorWithDetail = Error & { + details?: string; +}; + // @public (undocumented) interface Es6Config extends BaseModuleConfig { // (undocumented) @@ -3758,6 +3762,9 @@ export function getRawLibrary(library: LibraryOptions): RawLibraryOptions; // @public (undocumented) export const getRawOptions: (options: RspackOptionsNormalized, compiler: Compiler) => RawOptions; +// @public (undocumented) +export function getRawResolve(resolve: Resolve): RawOptions["resolve"]; + // @public (undocumented) export type GlobalObject = z.infer; @@ -8155,10 +8162,7 @@ export type RemotesObject = { }; // @public (undocumented) -export type Resolve = z.infer; - -// @public (undocumented) -const resolve: z.ZodType; +export type Resolve = z.infer; // @public (undocumented) export type ResolveAlias = z.infer; @@ -8166,6 +8170,10 @@ export type ResolveAlias = z.infer; // @public (undocumented) const resolveAlias: z.ZodRecord, z.ZodString]>, z.ZodArray]>, "many">]>>; +// @public (undocumented) +interface ResolveContext { +} + // @public (undocumented) type ResolveData = { contextInfo: ContextInfo; @@ -8182,9 +8190,42 @@ export type ResolveOptions = z.infer & { byDependency?: Record; }; +// @public (undocumented) +const resolveOptions: z.ZodType; + +// @public (undocumented) +type ResolveOptionsWithDependencyType = binding.RawResolveOptionsWithDependencyType; + +// @public (undocumented) +type ResolveOptionsWithDependencyType_2 = Omit & { + restrictions?: (string | RegExp)[]; +}; + +// @public (undocumented) +class Resolver { + constructor(binding: binding.JsResolver); + // (undocumented) + binding: binding.JsResolver; + // (undocumented) + resolve(context: object, path: string, request: string, resolveContext: ResolveContext, callback: (err: null | ErrorWithDetail, res?: string | false) => void): void; + // (undocumented) + resolveSync(context: object, path: string, request: string): string | false; + // (undocumented) + withOptions({ restrictions, ...rest }: ResolveOptionsWithDependencyType_2): Resolver; +} + // @public (undocumented) type ResolveRequest = BaseResolveRequest & Partial; +// @public (undocumented) +class ResolverFactory { + constructor(binding: binding.JsResolverFactory); + // (undocumented) + binding: binding.JsResolverFactory; + // (undocumented) + get(type: string, resolveOptions?: ResolveOptionsWithDependencyType): Resolver; +} + // @public (undocumented) export type ResolveTsConfig = z.infer; @@ -8378,6 +8419,7 @@ declare namespace rspackExports { SwcLoaderTransformConfig, SwcLoaderTsParserConfig, experiments, + getRawResolve, getRawLibrary, getRawChunkLoading, LoaderContext, diff --git a/packages/rspack/src/Compilation.ts b/packages/rspack/src/Compilation.ts index dbe483b9fd8..be93f650350 100644 --- a/packages/rspack/src/Compilation.ts +++ b/packages/rspack/src/Compilation.ts @@ -19,7 +19,16 @@ import { } from "@rspack/binding"; import * as liteTapable from "@rspack/lite-tapable"; import { Source } from "webpack-sources"; +import { Chunk } from "./Chunk"; +import { ChunkGraph } from "./ChunkGraph"; +import { Compiler } from "./Compiler"; import { ContextModuleFactory } from "./ContextModuleFactory"; +import { Entrypoint } from "./Entrypoint"; +import ErrorHelpers from "./ErrorHelpers"; +import { CodeGenerationResult, Module } from "./Module"; +import { NormalModuleFactory } from "./NormalModuleFactory"; +import { ResolverFactory } from "./ResolverFactory"; +import { Stats, StatsAsset, StatsError, StatsModule } from "./Stats"; import { Filename, OutputNormalized, @@ -28,15 +37,6 @@ import { StatsOptions, StatsValue } from "./config"; -import { ResolverFactory } from "./ResolverFactory"; -import { Chunk } from "./Chunk"; -import { ChunkGraph } from "./ChunkGraph"; -import { Compiler } from "./Compiler"; -import { Entrypoint } from "./Entrypoint"; -import ErrorHelpers from "./ErrorHelpers"; -import { CodeGenerationResult, Module } from "./Module"; -import { NormalModuleFactory } from "./NormalModuleFactory"; -import { Stats, StatsAsset, StatsError, StatsModule } from "./Stats"; import { LogType, Logger } from "./logging/Logger"; import { StatsFactory } from "./stats/StatsFactory"; import { StatsPrinter } from "./stats/StatsPrinter"; diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index b1b7eec523c..db404976dd5 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -12,6 +12,7 @@ import * as liteTapable from "@rspack/lite-tapable"; import type Watchpack from "watchpack"; import { Compilation, CompilationParams } from "./Compilation"; import { ContextModuleFactory } from "./ContextModuleFactory"; +import { ThreadsafeWritableNodeFS } from "./FileSystem"; import { RuleSetCompiler } from "./RuleSetCompiler"; import { Stats } from "./Stats"; import { @@ -21,9 +22,8 @@ import { RspackPluginInstance, getRawOptions } from "./config"; -import { rspack } from "./index"; -import { ThreadsafeWritableNodeFS } from "./FileSystem"; import ConcurrentCompilationError from "./error/ConcurrentCompilationError"; +import { rspack } from "./index"; import Cache = require("./lib/Cache"); import CacheFacade = require("./lib/CacheFacade"); import { Source } from "webpack-sources";