Gathering detailed insights and metrics for enhanced-resolve
Gathering detailed insights and metrics for enhanced-resolve
Gathering detailed insights and metrics for enhanced-resolve
Gathering detailed insights and metrics for enhanced-resolve
postcss-import-resolver
Customize postcss-import resolve with enhanced-resolve
enhanced-resolve-jest
Resolver for Jest which uses the enhanced-resolve module
@chialab/node-resolve
A promise based node resolution library based on enhanced-resolve.
jest-enhanced-resolve
Use Webpack's enhanced-resolve inside Jest
Offers an async require.resolve function. It's highly configurable.
npm install enhanced-resolve
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
933 Stars
860 Commits
189 Forks
19 Watching
8 Branches
132 Contributors
Updated on 24 Nov 2024
Minified
Minified + Gzipped
JavaScript (99.98%)
Shell (0.02%)
Cumulative downloads
Total Downloads
Last day
-2.7%
7,261,976
Compared to previous day
Last week
4%
38,622,403
Compared to previous week
Last month
15%
156,641,294
Compared to previous month
Last year
4.4%
1,622,303,816
Compared to previous year
Offers an async require.resolve function. It's highly configurable.
1# npm 2npm install enhanced-resolve 3# or Yarn 4yarn add enhanced-resolve
There is a Node.js API which allows to resolve requests according to the Node.js resolving rules.
Sync and async APIs are offered. A create
method allows to create a custom resolve function.
1const resolve = require("enhanced-resolve"); 2 3resolve("/some/path/to/folder", "module/dir", (err, result) => { 4 result; // === "/some/path/node_modules/module/dir/index.js" 5}); 6 7resolve.sync("/some/path/to/folder", "../../dir"); 8// === "/some/path/dir/index.js" 9 10const myResolve = resolve.create({ 11 // or resolve.create.sync 12 extensions: [".ts", ".js"] 13 // see more options below 14}); 15 16myResolve("/some/path/to/folder", "ts-module", (err, result) => { 17 result; // === "/some/node_modules/ts-module/index.ts" 18});
The easiest way to create a resolver is to use the createResolver
function on ResolveFactory
, along with one of the supplied File System implementations.
1const fs = require("fs"); 2const { CachedInputFileSystem, ResolverFactory } = require("enhanced-resolve"); 3 4// create a resolver 5const myResolver = ResolverFactory.createResolver({ 6 // Typical usage will consume the `fs` + `CachedInputFileSystem`, which wraps Node.js `fs` to add caching. 7 fileSystem: new CachedInputFileSystem(fs, 4000), 8 extensions: [".js", ".json"] 9 /* any other resolver options here. Options/defaults can be seen below */ 10}); 11 12// resolve a file with the new resolver 13const context = {}; 14const lookupStartPath = "/Users/webpack/some/root/dir"; 15const request = "./path/to-look-up.js"; 16const resolveContext = {}; 17myResolver.resolve(context, lookupStartPath, request, resolveContext, ( 18 err /*Error*/, 19 filepath /*string*/ 20) => { 21 // Do something with the path 22});
Field | Default | Description |
---|---|---|
alias | [] | A list of module alias configurations or an object which maps key to value |
aliasFields | [] | A list of alias fields in description files |
extensionAlias | {} | An object which maps extension to extension aliases |
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. |
cacheWithContext | true | If unsafe cache is enabled, includes request.context in the cache key |
conditionNames | [] | A list of exports field condition names |
descriptionFiles | ["package.json"] | A list of description files to read from |
enforceExtension | false | Enforce that a extension from extensions must be used |
exportsFields | ["exports"] | A list of exports fields in description files |
extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
fallback | [] | Same as alias , but only used if default resolving fails |
fileSystem | The file system which should be used | |
fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
mainFields | ["main"] | A list of main fields in description files |
mainFiles | ["index"] | A list of main files in directories |
modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
plugins | [] | A list of additional resolve plugins which should be applied |
resolver | undefined | A prepared Resolver to which the plugins are attached |
resolveToContext | false | Resolve to a context instead of a file |
preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
restrictions | [] | A list of resolve restrictions |
roots | [] | A list of root paths |
symlinks | true | Whether to resolve symlinks to their symlinked location |
unsafeCache | false | Use this cache object to unsafely cache the successful requests |
Similar to webpack
, the core of enhanced-resolve
functionality is implemented as individual plugins that are executed using tapable
.
These plugins can extend the functionality of the library, adding other ways for files/contexts to be resolved.
A plugin should be a class
(or its ES5 equivalent) with an apply
method. The apply
method will receive a resolver
instance, that can be used to hook in to the event system.
1class MyResolverPlugin {
2 constructor(source, target) {
3 this.source = source;
4 this.target = target;
5 }
6
7 apply(resolver) {
8 const target = resolver.ensureHook(this.target);
9 resolver
10 .getHook(this.source)
11 .tapAsync("MyResolverPlugin", (request, resolveContext, callback) => {
12 // Any logic you need to create a new `request` can go here
13 resolver.doResolve(target, request, null, resolveContext, callback);
14 });
15 }
16}
Plugins are executed in a pipeline, and register which event they should be executed before/after. In the example above, source
is the name of the event that starts the pipeline, and target
is what event this plugin should fire, which is what continues the execution of the pipeline. For an example of how these different plugin events create a chain, see lib/ResolverFactory.js
, in the //// pipeline ////
section.
It's allowed to escape #
as \0#
to avoid parsing it as fragment.
enhanced-resolve will try to resolve requests containing #
as path and as fragment, so it will automatically figure out if ./some#thing
means .../some.js#thing
or .../some#thing.js
. When a #
is resolved as path it will be escaped in the result. Here: .../some\0#thing.js
.
1yarn test
If you are using webpack
, and you want to pass custom options to enhanced-resolve
, the options are passed from the resolve
key of your webpack configuration e.g.:
resolve: {
extensions: ['.js', '.jsx'],
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
plugins: [new DirectoryNamedWebpackPlugin()]
...
},
Copyright (c) 2012-2019 JS Foundation and other contributors
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
6 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 3/11 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More