What is enhanced-resolve?
The enhanced-resolve package is a highly configurable module resolution library for Node.js, which is used by webpack under the hood. It resolves a path to a file or directory in a file system. It can handle complex resolution patterns like module aliases, extensions, and directories with package.json files.
What are enhanced-resolve's main functionalities?
File Resolution
Resolves the path to a file, taking into account file extensions and processing according to the configuration provided.
const { ResolverFactory } = require('enhanced-resolve');
const resolver = ResolverFactory.createResolver({
fileSystem: require('fs'),
extensions: ['.js', '.json']
});
resolver.resolve({}, __dirname, './path/to/file', (err, result) => {
if (err) console.error(err);
else console.log(result);
});
Directory Resolution
Resolves the path to a directory, looking for the 'main' field in the package.json or index.js within that directory.
const { ResolverFactory } = require('enhanced-resolve');
const resolver = ResolverFactory.createResolver({
fileSystem: require('fs'),
mainFields: ['main']
});
resolver.resolve({}, __dirname, './path/to/directory', (err, result) => {
if (err) console.error(err);
else console.log(result);
});
Plugin System
Allows the use of plugins to extend or modify the resolution behavior, providing a high degree of customization.
const { ResolverFactory } = require('enhanced-resolve');
const MyPlugin = require('./MyPlugin');
const resolver = ResolverFactory.createResolver({
fileSystem: require('fs'),
plugins: [new MyPlugin()] // Custom plugin to modify resolution behavior
});
// Use the resolver as before
Other packages similar to enhanced-resolve
resolve
A simple module resolution package that can be used to resolve file paths similarly to Node's 'require.resolve'. It is less configurable than enhanced-resolve but is easier to use for simple resolution tasks.
browser-resolve
A browser-focused module resolver that aims to replicate Node's 'require.resolve' behavior for browser environments. It is similar to enhanced-resolve but with a focus on resolving modules for bundling in the browser.
require-resolve
This package provides a function to resolve a module path relative to a given path. It is a simpler alternative to enhanced-resolve, focusing on resolving require paths without the extensive configuration options.
enhanced-resolve
Offers a async require.resolve function. It's highly configurable.
documentation
Features
- plugin system
- provide a custom filesystem
- sync and async node.js filesystems included
Tests
npm test
Contributing
Take a look at lib/ResolveFactory.js
to understand how everything fits together.
License
Copyright (c) 2012-2013 Tobias Koppers
MIT (http://www.opensource.org/licenses/mit-license.php)