Skip to content

Commit 348743f

Browse files
authored
Merge pull request #277 from webpack/feature/prefer-absolute-4
add preferAbsolute option to prefer absolute paths over roots
2 parents d2bd61a + 958f0cf commit 348743f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ For more examples creating different types resolvers (sync/async, context, etc)
6161
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
6262
| roots | [] | A list of directories to resolve request starting with `/` from |
6363
| ignoreRootsErrors | false | Ignore fatal errors happening during handling of `roots` (allows to add `roots` without a breaking change) |
64+
| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
6465
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
6566
| plugins | [] | A list of additional resolve plugins which should be applied |
6667
| symlinks | true | Whether to resolve symlinks to their symlinked location |

lib/ResolverFactory.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ exports.createResolver = function(options) {
8585
// Ignore errors happening when resolving roots
8686
const ignoreRootsErrors = options.ignoreRootsErrors || false;
8787

88+
// Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots
89+
const preferAbsolute = options.preferAbsolute || false;
90+
8891
const restrictions = options.restrictions || [];
8992

9093
// Use this cache object to unsafely cache the successful requests
@@ -224,6 +227,9 @@ exports.createResolver = function(options) {
224227
plugins.push(new AliasFieldPlugin("described-resolve", item, "resolve"));
225228
});
226229
plugins.push(new ModuleKindPlugin("after-described-resolve", "raw-module"));
230+
if (preferAbsolute) {
231+
plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));
232+
}
227233
roots.forEach(root => {
228234
plugins.push(
229235
new RootPlugin(
@@ -234,7 +240,9 @@ exports.createResolver = function(options) {
234240
)
235241
);
236242
});
237-
plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));
243+
if (!preferAbsolute) {
244+
plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));
245+
}
238246

239247
// raw-module
240248
moduleExtensions.forEach(item => {

test/roots.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ describe("roots", () => {
2727
]
2828
});
2929

30+
const resolverPreferAbsolute = ResolverFactory.createResolver({
31+
extensions: [".js"],
32+
alias: {
33+
foo: "/fixtures"
34+
},
35+
roots: [__dirname, fixtures],
36+
fileSystem,
37+
preferAbsolute: true,
38+
plugins: [
39+
{
40+
apply(resolver) {
41+
resolver.hooks.file.tap("Test", request => {
42+
if (/test.fixtures.*test.fixtures/.test(request.path))
43+
throw new Error("Simulate a fatal error in root path");
44+
});
45+
}
46+
}
47+
]
48+
});
49+
3050
const contextResolver = ResolverFactory.createResolver({
3151
roots: [__dirname],
3252
fileSystem,
@@ -107,7 +127,7 @@ describe("roots", () => {
107127
});
108128
});
109129

110-
it("should resolve an absolute path", done => {
130+
it("should resolve an absolute path (ignore roots errors)", done => {
111131
resolver.resolve(
112132
{},
113133
fixtures,
@@ -121,4 +141,19 @@ describe("roots", () => {
121141
}
122142
);
123143
});
144+
145+
it("should resolve an absolute path (prefer absolute)", done => {
146+
resolverPreferAbsolute.resolve(
147+
{},
148+
fixtures,
149+
path.join(fixtures, "b.js"),
150+
{},
151+
(err, result) => {
152+
if (err) return done(err);
153+
if (!result) throw new Error("No result");
154+
result.should.equal(path.resolve(fixtures, "b.js"));
155+
done();
156+
}
157+
);
158+
});
124159
});

0 commit comments

Comments
 (0)