Skip to content

Commit ce6fb1d

Browse files
feat: accept file: URL instances for resolve context and request (#607)
1 parent ae46898 commit ce6fb1d

5 files changed

Lines changed: 231 additions & 106 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enhanced-resolve": minor
3+
---
4+
5+
Allow the `context` path and `request` arguments of `resolve` (and `resolveSync`/`resolvePromise`) to accept `file:` `URL` instances, converting them to filesystem paths. Plain strings stay literal paths, matching Node's `fs`.

‎lib/Resolver.js‎

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
createCachedJoin,
1616
getType,
1717
normalize,
18+
toPath,
1819
} = require("./util/path");
1920

2021
/* eslint-disable jsdoc/check-alignment */
@@ -25,8 +26,8 @@ const _withResolvers =
2526
? /**
2627
* @param {Resolver} self resolver
2728
* @param {Context} context context information object
28-
* @param {string} path context path
29-
* @param {string} request request string
29+
* @param {string | URL} path context path or a `file:` URL instance
30+
* @param {string | URL} request request string or a `file:` URL instance
3031
* @param {ResolveContext} resolveContext resolve context
3132
* @returns {Promise<string | false>} result
3233
*/
@@ -42,8 +43,8 @@ const _withResolvers =
4243
: /**
4344
* @param {Resolver} self resolver
4445
* @param {Context} context context information object
45-
* @param {string} path context path
46-
* @param {string} request request string
46+
* @param {string | URL} path context path or a `file:` URL instance
47+
* @param {string | URL} request request string or a `file:` URL instance
4748
* @param {ResolveContext} resolveContext resolve context
4849
* @returns {Promise<string | false>} result
4950
*/
@@ -692,23 +693,23 @@ class Resolver {
692693

693694
/**
694695
* @overload
695-
* @param {string} path context path
696-
* @param {string} request request string
696+
* @param {string | URL} path context path or a `file:` URL instance
697+
* @param {string | URL} request request string or a `file:` URL instance
697698
* @param {ResolveContext=} resolveContext resolve context
698699
* @returns {string | false} result
699700
*/
700701
/**
701702
* @overload
702703
* @param {Context} context context information object
703-
* @param {string} path context path
704-
* @param {string} request request string
704+
* @param {string | URL} path context path or a `file:` URL instance
705+
* @param {string | URL} request request string or a `file:` URL instance
705706
* @param {ResolveContext=} resolveContext resolve context
706707
* @returns {string | false} result
707708
*/
708709
/**
709-
* @param {Context | string} context context information object or context path when no context is provided
710-
* @param {string | ResolveContext=} path context path or resolve context when no context is provided
711-
* @param {string | ResolveContext=} request request string or resolve context when no context is provided
710+
* @param {Context | string | URL} context context information object, or the context path (string or `file:` URL instance) when no context is provided
711+
* @param {string | URL | ResolveContext=} path context path (string or `file:` URL instance) or resolve context when no context is provided
712+
* @param {string | URL | ResolveContext=} request request string (or `file:` URL instance) or resolve context when no context is provided
712713
* @param {ResolveContext=} resolveContext resolve context
713714
* @returns {string | false} result
714715
*/
@@ -744,23 +745,23 @@ class Resolver {
744745

745746
/**
746747
* @overload
747-
* @param {string} path context path
748-
* @param {string} request request string
748+
* @param {string | URL} path context path or a `file:` URL instance
749+
* @param {string | URL} request request string or a `file:` URL instance
749750
* @param {ResolveContext=} resolveContext resolve context
750751
* @returns {Promise<string | false>} result
751752
*/
752753
/**
753754
* @overload
754755
* @param {Context} context context information object
755-
* @param {string} path context path
756-
* @param {string} request request string
756+
* @param {string | URL} path context path or a `file:` URL instance
757+
* @param {string | URL} request request string or a `file:` URL instance
757758
* @param {ResolveContext=} resolveContext resolve context
758759
* @returns {Promise<string | false>} result
759760
*/
760761
/**
761-
* @param {Context | string} context context information object or context path when no context is provided
762-
* @param {string | ResolveContext=} path context path or resolve context when no context is provided
763-
* @param {string | ResolveContext=} request request string or resolve context when no context is provided
762+
* @param {Context | string | URL} context context information object, or the context path (string or `file:` URL instance) when no context is provided
763+
* @param {string | URL | ResolveContext=} path context path (string or `file:` URL instance) or resolve context when no context is provided
764+
* @param {string | URL | ResolveContext=} request request string (or `file:` URL instance) or resolve context when no context is provided
764765
* @param {ResolveContext=} resolveContext resolve context
765766
* @returns {Promise<string | false>} result
766767
*/
@@ -778,40 +779,40 @@ class Resolver {
778779

779780
/**
780781
* @overload
781-
* @param {string} path context path
782-
* @param {string} request request string
782+
* @param {string | URL} path context path or a `file:` URL instance
783+
* @param {string | URL} request request string or a `file:` URL instance
783784
* @param {ResolveCallback} callback callback function
784785
* @returns {void}
785786
*/
786787
/**
787788
* @overload
788-
* @param {string} path context path
789-
* @param {string} request request string
789+
* @param {string | URL} path context path or a `file:` URL instance
790+
* @param {string | URL} request request string or a `file:` URL instance
790791
* @param {ResolveContext} resolveContext resolve context
791792
* @param {ResolveCallback} callback callback function
792793
* @returns {void}
793794
*/
794795
/**
795796
* @overload
796797
* @param {Context} context context information object
797-
* @param {string} path context path
798-
* @param {string} request request string
798+
* @param {string | URL} path context path or a `file:` URL instance
799+
* @param {string | URL} request request string or a `file:` URL instance
799800
* @param {ResolveCallback} callback callback function
800801
* @returns {void}
801802
*/
802803
/**
803804
* @overload
804805
* @param {Context} context context information object
805-
* @param {string} path context path
806-
* @param {string} request request string
806+
* @param {string | URL} path context path or a `file:` URL instance
807+
* @param {string | URL} request request string or a `file:` URL instance
807808
* @param {ResolveContext} resolveContext resolve context
808809
* @param {ResolveCallback} callback callback function
809810
* @returns {void}
810811
*/
811812
/**
812-
* @param {Context | string} context context information object or context path when no context is provided
813-
* @param {string | ResolveContext | ResolveCallback=} path context path or (when no context) resolve context or callback
814-
* @param {string | ResolveContext | ResolveCallback=} request request string or (when no context) resolve context or callback
813+
* @param {Context | string | URL} context context information object, or the context path (string or `file:` URL instance) when no context is provided
814+
* @param {string | URL | ResolveContext | ResolveCallback=} path context path (string or `file:` URL instance) or (when no context) resolve context or callback
815+
* @param {string | URL | ResolveContext | ResolveCallback=} request request string (or `file:` URL instance) or (when no context) resolve context or callback
815816
* @param {ResolveContext | ResolveCallback=} resolveContext resolve context or callback when no resolve context is provided
816817
* @param {ResolveCallback=} callback callback function
817818
* @returns {void}
@@ -835,8 +836,9 @@ class Resolver {
835836
// proceed straight to per-arg validation below
836837
} else {
837838
// Slow path: shift positional args based on what was supplied.
838-
// Shift when context is omitted (first positional arg is the path string).
839-
if (typeof context === "string") {
839+
// Shift when context is omitted (first positional arg is the path,
840+
// either a string or a `file:` URL instance).
841+
if (typeof context === "string" || context instanceof URL) {
840842
// Keep an already-supplied callback (resolveSync / resolvePromise
841843
// always pass one in the 5th position).
842844
if (typeof callback !== "function") {
@@ -864,11 +866,17 @@ class Resolver {
864866
context = {};
865867
}
866868
}
869+
// Accept `file:` URL instances for path/request, converting to a
870+
// filesystem path (mirrors the URL support in resolve options). The
871+
// `instanceof` check sits on the error branch, so the common string
872+
// case keeps its single `typeof` check on this hot path.
867873
if (typeof path !== "string") {
868-
return callback(new Error("path argument is not a string"));
874+
if (path instanceof URL) path = toPath(path);
875+
else return callback(new Error("path argument is not a string"));
869876
}
870877
if (typeof request !== "string") {
871-
return callback(new Error("request argument is not a string"));
878+
if (request instanceof URL) request = toPath(request);
879+
else return callback(new Error("request argument is not a string"));
872880
}
873881

874882
/** @type {ResolveRequest} */

‎lib/index.js‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ const memoize = require("./util/memoize");
2121

2222
/**
2323
* @typedef {{
24-
* (context: Context, path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void,
25-
* (context: Context, path: string, request: string, callback: ResolveCallback): void,
26-
* (path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void,
27-
* (path: string, request: string, callback: ResolveCallback): void,
24+
* (context: Context, path: string | URL, request: string | URL, resolveContext: ResolveContext, callback: ResolveCallback): void,
25+
* (context: Context, path: string | URL, request: string | URL, callback: ResolveCallback): void,
26+
* (path: string | URL, request: string | URL, resolveContext: ResolveContext, callback: ResolveCallback): void,
27+
* (path: string | URL, request: string | URL, callback: ResolveCallback): void,
2828
* }} ResolveFunctionAsync
2929
*/
3030

3131
/**
3232
* @typedef {{
33-
* (context: Context, path: string, request: string, resolveContext?: ResolveContext): string | false,
34-
* (path: string, request: string, resolveContext?: ResolveContext): string | false,
33+
* (context: Context, path: string | URL, request: string | URL, resolveContext?: ResolveContext): string | false,
34+
* (path: string | URL, request: string | URL, resolveContext?: ResolveContext): string | false,
3535
* }} ResolveFunction
3636
*/
3737

3838
/**
3939
* @typedef {{
40-
* (context: Context, path: string, request: string, resolveContext?: ResolveContext): Promise<string | false>,
41-
* (path: string, request: string, resolveContext?: ResolveContext): Promise<string | false>,
40+
* (context: Context, path: string | URL, request: string | URL, resolveContext?: ResolveContext): Promise<string | false>,
41+
* (path: string | URL, request: string | URL, resolveContext?: ResolveContext): Promise<string | false>,
4242
* }} ResolveFunctionPromise
4343
*/
4444

@@ -70,14 +70,14 @@ const getAsyncResolver = memoize(() =>
7070
*/
7171
const resolve =
7272
/**
73-
* @param {object | string} context context
74-
* @param {string} path path
75-
* @param {string | ResolveContext | ResolveCallback} request request
73+
* @param {object | string | URL} context context
74+
* @param {string | URL} path path
75+
* @param {string | URL | ResolveContext | ResolveCallback} request request
7676
* @param {ResolveContext | ResolveCallback=} resolveContext resolve context
7777
* @param {ResolveCallback=} callback callback
7878
*/
7979
(context, path, request, resolveContext, callback) => {
80-
if (typeof context === "string") {
80+
if (typeof context === "string" || context instanceof URL) {
8181
callback = /** @type {ResolveCallback} */ (resolveContext);
8282
resolveContext = /** @type {ResolveContext} */ (request);
8383
request = path;
@@ -110,14 +110,14 @@ const getSyncResolver = memoize(() =>
110110
*/
111111
const resolveSync =
112112
/**
113-
* @param {object | string} context context
114-
* @param {string} path path
115-
* @param {string | ResolveContext | undefined} request request
113+
* @param {object | string | URL} context context
114+
* @param {string | URL} path path
115+
* @param {string | URL | ResolveContext | undefined} request request
116116
* @param {ResolveContext=} resolveContext resolve context
117117
* @returns {string | false} resolved path
118118
*/
119119
(context, path, request, resolveContext) => {
120-
if (typeof context === "string") {
120+
if (typeof context === "string" || context instanceof URL) {
121121
resolveContext = /** @type {ResolveContext} */ (request);
122122
request = path;
123123
path = context;
@@ -136,14 +136,14 @@ const resolveSync =
136136
*/
137137
const resolvePromise =
138138
/**
139-
* @param {object | string} context context
140-
* @param {string} path path
141-
* @param {string | ResolveContext | undefined} request request
139+
* @param {object | string | URL} context context
140+
* @param {string | URL} path path
141+
* @param {string | URL | ResolveContext | undefined} request request
142142
* @param {ResolveContext=} resolveContext resolve context
143143
* @returns {Promise<string | false>} resolved path
144144
*/
145145
(context, path, request, resolveContext) => {
146-
if (typeof context === "string") {
146+
if (typeof context === "string" || context instanceof URL) {
147147
resolveContext = /** @type {ResolveContext} */ (request);
148148
request = path;
149149
path = context;
@@ -169,14 +169,14 @@ function create(options) {
169169
...options,
170170
});
171171
/**
172-
* @param {object | string} context Custom context
173-
* @param {string} path Base path
174-
* @param {string | ResolveContext | ResolveCallback} request String to resolve
172+
* @param {object | string | URL} context Custom context
173+
* @param {string | URL} path Base path
174+
* @param {string | URL | ResolveContext | ResolveCallback} request String to resolve
175175
* @param {ResolveContext | ResolveCallback=} resolveContext Resolve context
176176
* @param {ResolveCallback=} callback Result callback
177177
*/
178178
return function create(context, path, request, resolveContext, callback) {
179-
if (typeof context === "string") {
179+
if (typeof context === "string" || context instanceof URL) {
180180
callback = /** @type {ResolveCallback} */ (resolveContext);
181181
resolveContext = /** @type {ResolveContext} */ (request);
182182
request = path;
@@ -207,14 +207,14 @@ function createSync(options) {
207207
...options,
208208
});
209209
/**
210-
* @param {object | string} context custom context
211-
* @param {string} path base path
212-
* @param {string | ResolveContext | undefined} request request to resolve
210+
* @param {object | string | URL} context custom context
211+
* @param {string | URL} path base path
212+
* @param {string | URL | ResolveContext | undefined} request request to resolve
213213
* @param {ResolveContext=} resolveContext Resolve context
214214
* @returns {string | false} Resolved path or false
215215
*/
216216
return function createSync(context, path, request, resolveContext) {
217-
if (typeof context === "string") {
217+
if (typeof context === "string" || context instanceof URL) {
218218
resolveContext = /** @type {ResolveContext} */ (request);
219219
request = path;
220220
path = context;
@@ -239,14 +239,14 @@ function createPromise(options) {
239239
...options,
240240
});
241241
/**
242-
* @param {object | string} context Custom context
243-
* @param {string} path Base path
244-
* @param {string | ResolveContext | undefined} request String to resolve
242+
* @param {object | string | URL} context Custom context
243+
* @param {string | URL} path Base path
244+
* @param {string | URL | ResolveContext | undefined} request String to resolve
245245
* @param {ResolveContext=} resolveContext Resolve context
246246
* @returns {Promise<string | false>} resolved path
247247
*/
248248
return function createPromise(context, path, request, resolveContext) {
249-
if (typeof context === "string") {
249+
if (typeof context === "string" || context instanceof URL) {
250250
resolveContext = /** @type {ResolveContext} */ (request);
251251
request = path;
252252
path = context;

0 commit comments

Comments
 (0)