Skip to content

Commit

Permalink
feat: support restrictions options
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jul 9, 2024
1 parent 6634f97 commit 2325dd7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,7 @@ export interface RawResolveOptionsWithDependencyType {
importsFields?: Array<string>
extensionAlias?: Record<string, Array<string>>
aliasFields?: Array<string>
restrictions?: Array<string>
roots?: Array<string>
dependencyCategory?: string
resolveToContext?: boolean
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_values/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl JsCompilation {
self
.0
.update_asset(&filename, |original_source, original_info| {
let new_source: Result<BoxSource> = try {
let new_source: napi::Result<BoxSource> = try {
let new_source = match new_source_or_function {
Either::A(new_source) => Into::<CompatSource>::into(new_source).boxed(),
Either::B(new_source_fn) => {
Expand Down
5 changes: 2 additions & 3 deletions crates/rspack_binding_values/src/options/raw_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ pub struct RawResolveOptionsWithDependencyType {
#[napi(ts_type = "Record<string, Array<string>>")]
pub extension_alias: Option<HashMap<String, Vec<String>>>,
pub alias_fields: Option<Vec<String>>,
// TODO
// pub restrictions: Option<Vec<String>>,
pub restrictions: Option<Vec<String>>,
pub roots: Option<Vec<String>>,

pub dependency_category: Option<String>,
Expand Down Expand Up @@ -255,7 +254,7 @@ pub fn normalize_raw_resolve_options_with_dependency_type(
extension_alias,
alias_fields,
roots: raw.roots,
restrictions: None,
restrictions: raw.restrictions,
imports_fields,
by_dependency,
description_files: raw.description_files,
Expand Down
3 changes: 0 additions & 3 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
getRawOptions
} from "./config";
import { rspack } from "./index";
// import { ResolverFactory } from "./ResolverFactory";
import { ThreadsafeWritableNodeFS } from "./FileSystem";
import ConcurrentCompilationError from "./error/ConcurrentCompilationError";
import Cache = require("./lib/Cache");
Expand Down Expand Up @@ -127,7 +126,6 @@ class Compiler {

running: boolean;
idle: boolean;
// resolverFactory: ResolverFactory;
infrastructureLogger: any;
watching?: Watching;

Expand Down Expand Up @@ -534,7 +532,6 @@ class Compiler {
childCompiler.outputPath = this.outputPath;
childCompiler.inputFileSystem = this.inputFileSystem;
childCompiler.outputFileSystem = null;
// childCompiler.resolverFactory = this.resolverFactory;
childCompiler.modifiedFiles = this.modifiedFiles;
childCompiler.removedFiles = this.removedFiles;
childCompiler.fileTimestamps = this.fileTimestamps;
Expand Down
24 changes: 20 additions & 4 deletions packages/rspack/src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ interface ResolveContext {}

type ErrorWithDetail = Error & { details?: string };

type ResolveOptionsWithDependencyType =
binding.RawResolveOptionsWithDependencyType;
type ResolveOptionsWithDependencyType = Omit<
binding.RawResolveOptionsWithDependencyType,
"restrictions"
> & {
restrictions?: (string | RegExp)[];
};

function isString(value: string | RegExp): value is string {
return typeof value === "string";
}

export class Resolver {
binding: binding.JsResolver;
Expand Down Expand Up @@ -37,8 +45,16 @@ export class Resolver {
}
}

withOptions(options: ResolveOptionsWithDependencyType): Resolver {
const binding = this.binding.withOptions(options);
withOptions({
restrictions,
...rest
}: ResolveOptionsWithDependencyType): Resolver {
const bindingOptions: binding.RawResolveOptionsWithDependencyType = rest;
// TODO: rspack_resolver is unimplemented regex
if (Array.isArray(restrictions)) {
bindingOptions.restrictions = restrictions.filter<string>(isString);
}
const binding = this.binding.withOptions(bindingOptions);
return new Resolver(binding);
}
}

0 comments on commit 2325dd7

Please sign in to comment.