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.
Features
- sync and async versions
- loaders and query strings
- normal resolve
- context resolve (resolve a directory)
- loaders resolve
- code completion
Request Format
relative: ./file
, .././../folder/./file
absolute: /home/file
, C:\folder\file
module: module
, module/with/sub/file
query: resourceFile?query
(with resourceFile one of above, and query be any string)
loaders: loader!resource
, loader1!loader2!resource
(with loader and resource each one of above)
Example: raw!./customLoader?evil,strict!C:\fail\loader?fail=big!../file.js?charset=utf-8
Methods
var resolve = require("enhanced-resolve");
resolve(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.sync(context: String, identifier: String, options?: Object) => String
resolve.context(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.context.sync(context: String, identifier: String, options?: Object) => String
resolve.loaders(context: String, identifier: String, options?: Object, callback: (err: Error, result: String[]))
resolve.loaders.sync(context: String, identifier: String, options?: Object) => String[]
resolve.complete(context: String, identifier: String, options?: Object, callback: (err: Error, result: Completion[]))
resolve.complete.sync(context: String, identifier: String, options?: Object) => Completion[]
resolve.parse(identifier: String) => {loaders: Part[], resource: Part}
resolve.parse.part(identifierPart: String) => Part
resolve.stringify(parsed: {loaders: Part[], resource: Part}) => String
resolve.stringify.part(part: Part) => String
resolve.parse.isModule(identifierPart: String) => Boolean
type Part { path: String, query: String, module: Boolean }
type Completion {
insert: String,
seqment: String,
part: String,
result: String
}
Options
{
paths: ["/my/absolute/dirname"],
modulesDirectories: ["xyz_modules", "node_modules"],
alias: {
"old-module": "new-module",
"another-module": "new-module/more/stuff"
},
extensions: ["", ".www.js", ".js"],
packageMains: ["abc", "main"]
loaderExtensions: [".loader.js", ".www-loader.js", "", ".js"],
loaderPostfixes: ["-loader", "-xyz", ""],
loaderPackageMains: ["myloader", "main"]
loaders: [{
test: /\.generator\.js/,
include: "\\.js",
exclude: [
/\.no\.generator\.js/,
"\\.nono\\.generator\\.js"
}
loader: "val"
}],
postprocess: {
normal: [function(filename, callback) {
if(/\.exclude\.[^\\\/]*$/.test(filename))
return callback(new Error("File is excluded"));
callback(null, filename);
}],
context: [],
}
disableLoaders: false,
disableResourceQuery: false,
disableResourcePureQuery: false,
disableLoaderQuery: false,
}
Tests
npm test
License
Copyright (c) 2012 Tobias Koppers
MIT (http://www.opensource.org/licenses/mit-license.php)