Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
enhanced-resolve
Advanced tools
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.
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
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.
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.
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.
Offers a async require.resolve function. It's highly configurable.
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
var resolve = require("enhanced-resolve");
// Resolve a normal request
resolve(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.sync(context: String, identifier: String, options?: Object) => String
// Resolve a context request, which means the result should be a folder
resolve.context(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.context.sync(context: String, identifier: String, options?: Object) => String
// Only resolve loaders, a array of resolved loaders is the result
resolve.loaders(context: String, identifier: String, options?: Object, callback: (err: Error, result: String[]))
resolve.loaders.sync(context: String, identifier: String, options?: Object) => String[]
// Autocomplete a incomplete require expression.
// identifier must contain exactly one "*", which indicates the insert position
resolve.complete(context: String, identifier: String, options?: Object, callback: (err: Error, result: Completion[]))
resolve.complete.sync(context: String, identifier: String, options?: Object) => Completion[]
// parse a request
resolve.parse(identifier: String) => {loaders: Part[], resource: Part}
// parse only a part
resolve.parse.part(identifierPart: String) => Part
// stringify a parsed request
resolve.stringify(parsed: {loaders: Part[], resource: Part}) => String
// stringify only a part
resolve.stringify.part(part: Part) => String
// checks if a request part is a module
resolve.parse.isModule(identifierPart: String) => Boolean
// the type used for parse and stringify
type Part { path: String, query: String, module: Boolean }
type Completion { // examples for "loader!module/dir/fi*?query"
insert: String, // i. e. "le.js"
seqment: String, // i. e. "file.js"
part: String, // i. e. "module/dir/file.js?query"
result: String // i. e. "loader!module/dir/file.js?query"
}
{
paths: ["/my/absolute/dirname"],
// default: []
// search paths for modules
modulesDirectories: ["xyz_modules", "node_modules"],
// default: (defaults are NOT included if you define your own)
// ["node_modules"];
// directories to be searched for modules
alias: {
"old-module": "new-module",
"another-module": "new-module/more/stuff"
},
// replace a module
extensions: ["", ".www.js", ".js"],
// defaults: (defaults are NOT included if you define your own)
// ["", ".js"]
// postfixes for files to try
packageMains: ["abc", "main"]
// defaults: ["main"]
// lookup fields in package.json
loaderExtensions: [".loader.js", ".www-loader.js", "", ".js"],
// defaults: (defaults are NOT included if you define your own)
// [".node-loader.js", ".loader.js", "", ".js"]
// postfixes for loaders to try
loaderPostfixes: ["-loader", "-xyz", ""],
// defaults: (defaults are NOT included if you define your own)
// ["-node-loader", "-loader", ""]
// postfixes for loader modules to try
loaderPackageMains: ["myloader", "main"]
// defaults: ["loader", "main"]
// lookup fields for loaders in package.json
loaders: [{
// test, include and exclude can be undefined, RegExp, string or array of these
test: /\.generator\.js/,
include: "\\.js",
exclude: [
/\.no\.generator\.js/,
"\\.nono\\.generator\\.js"
}
loader: "val"
}],
// default: []
// automatically use loaders if resolved filename match RegExp
// and no loader is specified.
postprocess: {
normal: [function(filename, callback) {
// webpack will not find files including ".exclude."
if(/\.exclude\.[^\\\/]*$/.test(filename))
return callback(new Error("File is excluded"));
callback(null, filename);
}],
// defaults: []
// postprocess resolved filenames by all specified async functions
// a postprocessor must call the callback
// You can pass a filename instead of a function
// The filename is required and the exports are expected to be a function.
context: [],
// same as postprocess.normal but for contextes
}
}
npm test
Copyright (c) 2012 Tobias Koppers
FAQs
Offers a async require.resolve function. It's highly configurable.
The npm package enhanced-resolve receives a total of 28,355,290 weekly downloads. As such, enhanced-resolve popularity was classified as popular.
We found that enhanced-resolve demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.