Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
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.
npm test
Take a look at lib/ResolveFactory.js
to understand how everything fits together. It provides a createResolver(options)
function and these are the possible options:
Field | Default | Description |
---|---|---|
modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
descriptionFiles | ["package.json"] | A list of description files to read from |
plugins | [] | A list of additional resolve plugins which should be applied |
mainFields | ["main"] | A list of main fields in description files |
aliasFields | [] | A list of alias fields in description files |
mainFiles | ["index"] | A list of main files in directories |
extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
enforceExtension | false | Enforce that a extension from extensions must be used |
moduleExtensions | [] | A list of module extensions which should be tried for modules |
enforceModuleExtension | false | Enforce that a extension from moduleExtensions must be used |
alias | [] | A list of module alias configurations or an object which maps key to value |
resolveToContext | false | Resolve to a context instead of a file |
unsafeCache | false | Use this cache object to unsafely cache the successful requests |
cacheWithContext | true | If unsafe cache is enabled, includes request.context in the cache key |
cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with path and request properties. |
fileSystem | The file system which should be used | |
resolver | undefined | A prepared Resolver to which the plugins are attached |
The options are passed from the resolve
key of your webpack configuration e.g.:
resolve: {
extensions: ['', '.js', '.jsx'],
modules: ['src', 'node_modules'],
plugins: [new DirectoryNamedWebpackPlugin()]
...
},
Copyright (c) 2012-2016 Tobias Koppers
FAQs
Offers a async require.resolve function. It's highly configurable.
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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.