or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-resolution.mdfile-system.mdindex.mdmodern-features.mdplugin-system.mdresolver-factory.md
tile.json

tessl/npm-enhanced-resolve

Highly configurable async require.resolve function with extensive customization options for module resolution

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/enhanced-resolve@5.18.x

To install, run

npx @tessl/cli install tessl/npm-enhanced-resolve@5.18.0

index.mddocs/

Enhanced Resolve

Enhanced Resolve is a highly configurable async require.resolve function that provides sophisticated Node.js module resolution capabilities. It serves as the core resolution engine for webpack and offers extensive customization through plugins, custom file systems, and configuration options, supporting modern package.json features like exports fields and conditional exports.

Package Information

  • Package Name: enhanced-resolve
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install enhanced-resolve

Core Imports

const resolve = require("enhanced-resolve");

ESM:

import resolve from "enhanced-resolve";

TypeScript:

import resolve, { 
  ResolverFactory, 
  CachedInputFileSystem,
  SyncAsyncFileSystemDecorator,
  CloneBasenamePlugin,
  LogInfoPlugin,
  forEachBail,
  type ResolveCallback,
  type ResolveOptions,
  type BaseFileSystem,
  type FileSystem,
  type SyncFileSystem 
} from "enhanced-resolve";

Basic Usage

const resolve = require("enhanced-resolve");

// Simple async resolution
resolve(__dirname, "./some-module", (err, result) => {
  if (err) console.error(err);
  else console.log(result); // "/path/to/resolved/module.js"
});

// Sync resolution
const resolved = resolve.sync(__dirname, "./some-module");
console.log(resolved); // "/path/to/resolved/module.js" or false

// Custom resolver with options
const customResolve = resolve.create({
  extensions: [".js", ".json", ".ts"],
  alias: {
    "@": "/src",
    "utils": "/src/utils"
  },
  modules: ["node_modules", "custom_modules"]
});

customResolve(__dirname, "@/components/Button", (err, result) => {
  console.log(result); // "/src/components/Button.js"
});

Architecture

Enhanced Resolve is built around several key architectural components:

  • Resolver Pipeline: Plugin-based resolution pipeline using tapable hooks for extensibility
  • File System Layer: Configurable file system interface with caching support via CachedInputFileSystem
  • Plugin System: 29 built-in plugins handling different aspects of resolution (aliases, exports fields, symlinks, etc.)
  • Factory Pattern: ResolverFactory creates configured resolver instances
  • Modern Package Support: Full support for package.json exports/imports fields and conditional exports

Capabilities

Core Resolution Functions

Primary async and sync resolution functions with multiple calling patterns and customizable options.

// Main async resolver function with multiple overloads
function resolve(context, path, request, resolveContext, callback);
function resolve(context, path, request, callback);
function resolve(path, request, resolveContext, callback);
function resolve(path, request, callback);

// Sync resolver function
resolve.sync(context, path, request): string | false;
resolve.sync(path, request): string | false;

Core Resolution

Custom Resolver Factory

Factory functions for creating custom resolvers with specific configuration options.

// Create custom async resolver
resolve.create(options: ResolveOptions): ResolverFunction;

// Create custom sync resolver  
resolve.create.sync(options: ResolveOptions): SyncResolverFunction;

Resolver Factory

File System Integration

Configurable file system abstraction with caching capabilities for performance optimization.

class CachedInputFileSystem {
  constructor(fileSystem: BaseFileSystem, duration: number);
  purge(what?: string | string[]): void;
}

class SyncAsyncFileSystemDecorator {
  constructor(syncFileSystem: SyncFileSystem);
}

File System

Plugin System

Extensive plugin system with 27+ built-in plugins for handling aliases, modern package features, and custom resolution logic.

class CloneBasenamePlugin {
  constructor(source: string, target: string);
  apply(resolver: Resolver): void;
}

class LogInfoPlugin {
  constructor(source: string);
  apply(resolver: Resolver): void;
}

Plugin System

Modern Package Features

Support for modern package.json features including exports/imports fields and conditional exports.

interface ResolveOptions {
  exportsFields?: (string | string[])[];
  importsFields?: (string | string[])[];
  conditionNames?: string[];
}

Modern Features

Utility Functions

Utility functions for common operations during resolution.

/**
 * Iterates through array with early bailout functionality
 * @param array - Array to iterate through
 * @param iterator - Iterator function called for each item
 * @param callback - Final callback after completion or early bailout
 */
function forEachBail<T, Z>(
  array: T[],
  iterator: (item: T, callback: (err?: Error, result?: Z) => void, index: number) => void,
  callback: (err?: Error, result?: Z, index?: number) => void
): void;

Types

type ResolveCallback = (
  err: null | Error,
  res?: string | false,
  req?: ResolveRequest
) => void;

interface ResolveOptions {
  alias?: AliasOptions | AliasOption[];
  fallback?: AliasOptions | AliasOption[];
  extensions?: string[];
  modules?: string | string[];
  mainFields?: (string | string[] | { name: string | string[]; forceRelative: boolean })[];
  mainFiles?: string[];
  fileSystem: FileSystem;
  plugins?: Plugin[];
  symlinks?: boolean;
  unsafeCache?: boolean | Cache;
  conditionNames?: string[];
  exportsFields?: (string | string[])[];
  importsFields?: (string | string[])[];
}

interface ResolveContext {
  contextDependencies?: WriteOnlySet<string>;
  fileDependencies?: WriteOnlySet<string>;
  missingDependencies?: WriteOnlySet<string>;
  log?: (str: string) => void;
}

interface ResolveRequest {
  path: string | false;
  context?: object;
  request?: string;
  descriptionFilePath?: string;
  descriptionFileData?: JsonObject;
}

type AliasOptions = {
  [key: string]: string | false | string[];
};

interface AliasOption {
  name: string;
  alias: string | false | string[];
  onlyModule?: boolean;
}

interface JsonObject {
  [key: string]: any;
}

interface WriteOnlySet<T> {
  add(item: T): void;
}

interface FileSystem {
  readFile(path: string, callback: (err: Error | null, data?: Buffer) => void): void;
  readdir(path: string, callback: (err: Error | null, files?: string[]) => void): void;
  readlink(path: string, callback: (err: Error | null, link?: string) => void): void;
  stat(path: string, callback: (err: Error | null, stats?: any) => void): void;
}

interface SyncFileSystem {
  readFileSync(path: string): Buffer;
  readdirSync(path: string): string[];
  readlinkSync(path: string): string;
  statSync(path: string): any;
}

type BaseFileSystem = FileSystem & SyncFileSystem;