Skip to content

Commit

Permalink
fix: remove unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jul 9, 2024
1 parent 2325dd7 commit 98b1339
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 21 additions & 15 deletions crates/rspack_binding_values/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@ impl JsResolver {
}

#[napi]
pub fn with_options(&self, raw: Option<RawResolveOptionsWithDependencyType>) -> 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<RawResolveOptionsWithDependencyType>,
) -> napi::Result<Self> {
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}"))),
}
}
}
19 changes: 10 additions & 9 deletions crates/rspack_binding_values/src/resolver_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ impl JsResolverFactory {
&self,
r#type: String,
raw: Option<RawResolveOptionsWithDependencyType>,
) -> JsResolver {
match r#type.as_str() {
) -> napi::Result<JsResolver> {
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}"))),
}
}
}
58 changes: 50 additions & 8 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, Source>;
// (undocumented)
Expand Down Expand Up @@ -1085,7 +1086,7 @@ export class Compilation {
// (undocumented)
renameAsset(filename: string, newFilename: string): void;
// (undocumented)
resolverFactory: ResolverFactory;
get resolverFactory(): ResolverFactory;
// (undocumented)
seal(): void;
// (undocumented)
Expand Down Expand Up @@ -1216,8 +1217,6 @@ export class Compiler {
// (undocumented)
removedFiles?: ReadonlySet<string>;
// (undocumented)
resolverFactory: ResolverFactory;
// (undocumented)
root: Compiler;
// (undocumented)
run(callback: liteTapable.Callback<Error, Stats>): void;
Expand Down Expand Up @@ -2836,6 +2835,11 @@ export class EnvironmentPlugin {
keys: any[];
}

// @public (undocumented)
type ErrorWithDetail = Error & {
details?: string;
};

// @public (undocumented)
interface Es6Config extends BaseModuleConfig {
// (undocumented)
Expand Down Expand Up @@ -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<typeof globalObject>;

Expand Down Expand Up @@ -8155,17 +8162,18 @@ export type RemotesObject = {
};

// @public (undocumented)
export type Resolve = z.infer<typeof resolve>;

// @public (undocumented)
const resolve: z.ZodType<ResolveOptions, z.ZodTypeDef, ResolveOptions>;
export type Resolve = z.infer<typeof resolveOptions>;

// @public (undocumented)
export type ResolveAlias = z.infer<typeof resolveAlias>;

// @public (undocumented)
const resolveAlias: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<false>, z.ZodString]>, z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodLiteral<false>]>, "many">]>>;

// @public (undocumented)
interface ResolveContext {
}

// @public (undocumented)
type ResolveData = {
contextInfo: ContextInfo;
Expand All @@ -8182,9 +8190,42 @@ export type ResolveOptions = z.infer<typeof baseResolveOptions> & {
byDependency?: Record<string, ResolveOptions>;
};

// @public (undocumented)
const resolveOptions: z.ZodType<ResolveOptions>;

// @public (undocumented)
type ResolveOptionsWithDependencyType = binding.RawResolveOptionsWithDependencyType;

// @public (undocumented)
type ResolveOptionsWithDependencyType_2 = Omit<binding.RawResolveOptionsWithDependencyType, "restrictions"> & {
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<ParsedIdentifier>;

// @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<typeof resolveTsConfig>;

Expand Down Expand Up @@ -8378,6 +8419,7 @@ declare namespace rspackExports {
SwcLoaderTransformConfig,
SwcLoaderTsParserConfig,
experiments,
getRawResolve,
getRawLibrary,
getRawChunkLoading,
LoaderContext,
Expand Down
18 changes: 9 additions & 9 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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";
Expand Down

0 comments on commit 98b1339

Please sign in to comment.