Skip to content

Commit 7e8355c

Browse files
authored
Merge pull request #253 from webpack/feature/prefer-relative
add preferRelative option
2 parents e2b976d + 9858d0b commit 7e8355c

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ myResolver.resolve({}, lookupStartPath, request, resolveContext, (
9595
| plugins | [] | A list of additional resolve plugins which should be applied |
9696
| resolver | undefined | A prepared Resolver to which the plugins are attached |
9797
| resolveToContext | false | Resolve to a context instead of a file |
98+
| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
9899
| restrictions | [] | A list of resolve restrictions |
99100
| roots | [] | A list of root paths |
100101
| symlinks | true | Whether to resolve symlinks to their symlinked location |

lib/ResolverFactory.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const UseFilePlugin = require("./UseFilePlugin");
7373
* @property {boolean=} resolveToContext Resolve to a context instead of a file
7474
* @property {(string|RegExp)[]=} restrictions A list of resolve restrictions
7575
* @property {boolean=} useSyncFileSystemCalls Use only the sync constiants of the file system calls
76+
* @property {boolean=} preferRelative Prefer to resolve module requests as relative requests before falling back to modules
7677
*/
7778

7879
/**
@@ -101,6 +102,7 @@ const UseFilePlugin = require("./UseFilePlugin");
101102
* @property {boolean} fullySpecified
102103
* @property {boolean} resolveToContext
103104
* @property {Set<string|RegExp>} restrictions
105+
* @property {boolean} preferRelative
104106
*/
105107

106108
/**
@@ -218,6 +220,7 @@ function createOptions(options) {
218220
roots: new Set(options.roots || undefined),
219221
fullySpecified: options.fullySpecified || false,
220222
resolveToContext: options.resolveToContext || false,
223+
preferRelative: options.preferRelative || false,
221224
restrictions: new Set(options.restrictions)
222225
};
223226
}
@@ -249,6 +252,7 @@ exports.createResolver = function (options) {
249252
plugins: userPlugins,
250253
pnpApi,
251254
resolveToContext,
255+
preferRelative,
252256
symlinks,
253257
unsafeCache,
254258
resolver: customResolver,
@@ -338,6 +342,9 @@ exports.createResolver = function (options) {
338342
new AliasFieldPlugin("normal-resolve", item, "internal-resolve")
339343
);
340344
});
345+
if (preferRelative) {
346+
plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
347+
}
341348
plugins.push(
342349
new ConditionalPlugin(
343350
"after-normal-resolve",
@@ -359,7 +366,9 @@ exports.createResolver = function (options) {
359366
if (roots.size > 0) {
360367
plugins.push(new RootsPlugin("after-normal-resolve", roots, "relative"));
361368
}
362-
plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
369+
if (!preferRelative) {
370+
plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
371+
}
363372

364373
// internal
365374
importsFields.forEach(importsField => {

test/resolve.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const issue238Resolve = resolve.create({
2020
modules: ["src/a", "src/b", "src/common", "node_modules"]
2121
});
2222

23+
const preferRelativeResolve = resolve.create({
24+
preferRelative: true
25+
});
26+
2327
function testResolve(name, context, moduleName, result) {
2428
describe(name, function () {
2529
it("should resolve sync correctly", function () {
@@ -251,4 +255,22 @@ describe("resolve", function () {
251255
}
252256
);
253257
});
258+
259+
it("should correctly resolve with preferRelative", function (done) {
260+
preferRelativeResolve(fixtures, "main1.js", function (err, filename) {
261+
if (err) done(err);
262+
should.exist(filename);
263+
filename.should.equal(path.join(fixtures, "main1.js"));
264+
done();
265+
});
266+
});
267+
268+
it("should correctly resolve with preferRelative", function (done) {
269+
preferRelativeResolve(fixtures, "m1/a.js", function (err, filename) {
270+
if (err) done(err);
271+
should.exist(filename);
272+
filename.should.equal(path.join(fixtures, "node_modules", "m1", "a.js"));
273+
done();
274+
});
275+
});
254276
});

types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ declare interface ResolveOptions {
194194
fullySpecified: boolean;
195195
resolveToContext: boolean;
196196
restrictions: Set<string | RegExp>;
197+
preferRelative: boolean;
197198
}
198199
declare abstract class Resolver {
199200
fileSystem: FileSystem;
@@ -415,6 +416,11 @@ declare interface UserResolveOptions {
415416
* Use only the sync constiants of the file system calls
416417
*/
417418
useSyncFileSystemCalls?: boolean;
419+
420+
/**
421+
* Prefer to resolve module requests as relative requests before falling back to modules
422+
*/
423+
preferRelative?: boolean;
418424
}
419425
declare function exports(
420426
context?: any,

0 commit comments

Comments
 (0)