Skip to content

Commit

Permalink
add resolve.preferAbsolute option
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 11, 2021
1 parent a064b0f commit 32cea58
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 12 deletions.
6 changes: 5 additions & 1 deletion declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,10 @@ export interface ResolveOptions {
* Plugins for the resolver.
*/
plugins?: ("..." | ResolvePluginInstance)[];
/**
* Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.
*/
preferAbsolute?: boolean;
/**
* Prefer to resolve module requests as relative request and fallback to resolving as module.
*/
Expand All @@ -1374,7 +1378,7 @@ export interface ResolveOptions {
*/
restrictions?: (RegExp | string)[];
/**
* A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.
* A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.
*/
roots?: string[];
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"acorn": "^8.0.4",
"browserslist": "^4.14.5",
"chrome-trace-event": "^1.0.2",
"enhanced-resolve": "^5.3.1",
"enhanced-resolve": "^5.5.0",
"eslint-scope": "^5.1.1",
"events": "^3.2.0",
"glob-to-regexp": "^0.4.1",
Expand Down
6 changes: 5 additions & 1 deletion schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,10 @@
]
}
},
"preferAbsolute": {
"description": "Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.",
"type": "boolean"
},
"preferRelative": {
"description": "Prefer to resolve module requests as relative request and fallback to resolving as module.",
"type": "boolean"
Expand Down Expand Up @@ -2888,7 +2892,7 @@
}
},
"roots": {
"description": "A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.",
"description": "A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.",
"type": "array",
"items": {
"description": "Directory in which requests that are server-relative URLs (starting with '/') are resolved.",
Expand Down
34 changes: 30 additions & 4 deletions test/__snapshots__/Cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5122,6 +5122,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"resolve-loader-prefer-absolute": Object {
"configs": Array [
Object {
"description": "Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.",
"multiple": false,
"path": "resolveLoader.preferAbsolute",
"type": "boolean",
},
],
"description": "Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.",
"multiple": false,
"simpleType": "boolean",
},
"resolve-loader-prefer-relative": Object {
"configs": Array [
Object {
Expand Down Expand Up @@ -5183,13 +5196,13 @@ Object {
"resolve-loader-roots-reset": Object {
"configs": Array [
Object {
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.",
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.",
"multiple": false,
"path": "resolveLoader.roots",
"type": "reset",
},
],
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.",
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.",
"multiple": false,
"simpleType": "boolean",
},
Expand Down Expand Up @@ -5310,6 +5323,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"resolve-prefer-absolute": Object {
"configs": Array [
Object {
"description": "Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.",
"multiple": false,
"path": "resolve.preferAbsolute",
"type": "boolean",
},
],
"description": "Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.",
"multiple": false,
"simpleType": "boolean",
},
"resolve-prefer-relative": Object {
"configs": Array [
Object {
Expand Down Expand Up @@ -5371,13 +5397,13 @@ Object {
"resolve-roots-reset": Object {
"configs": Array [
Object {
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.",
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.",
"multiple": false,
"path": "resolve.roots",
"type": "reset",
},
],
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.",
"description": "Clear all items provided in configuration. A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.",
"multiple": false,
"simpleType": "boolean",
},
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/resolving/prefer-absolute/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should be possible to import an absolute path", () => {
require(__filename);
});
15 changes: 15 additions & 0 deletions test/configCases/resolving/prefer-absolute/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
resolve: {
preferAbsolute: true,
plugins: [
{
apply(resolver) {
resolver.hooks.file.tap("Test", (request, resolverContext) => {
if (/test.configCases.*test.configCases/.test(request.path))
throw new Error("Trying to resolve as root path");
});
}
}
]
}
};
3 changes: 3 additions & 0 deletions test/configCases/resolving/prefer-root/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should be possible to import an root path", () => {
require("/index.js");
});
14 changes: 14 additions & 0 deletions test/configCases/resolving/prefer-root/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
resolve: {
plugins: [
{
apply(resolver) {
resolver.hooks.file.tap("Test", (request, resolverContext) => {
if (request.path === "/index.js")
throw new Error("Trying to resolve as absolute path");
});
}
}
]
}
};
13 changes: 12 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7666,6 +7666,7 @@ declare interface ResolveOptionsTypes {
resolveToContext: boolean;
restrictions: Set<string | RegExp>;
preferRelative: boolean;
preferAbsolute: boolean;
}

/**
Expand Down Expand Up @@ -7797,6 +7798,11 @@ declare interface ResolveOptionsWebpackOptions {
*/
plugins?: ("..." | ResolvePluginInstance)[];

/**
* Prefer to resolve server-relative URLs (starting with '/') as absolute paths before falling back to resolve in 'resolve.roots'.
*/
preferAbsolute?: boolean;

/**
* Prefer to resolve module requests as relative request and fallback to resolving as module.
*/
Expand All @@ -7813,7 +7819,7 @@ declare interface ResolveOptionsWebpackOptions {
restrictions?: (string | RegExp)[];

/**
* A list of directories in which requests that are server-relative URLs (starting with '/') are resolved. On non-windows system these requests are tried to resolve as absolute path first.
* A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.
*/
roots?: string[];

Expand Down Expand Up @@ -10050,6 +10056,11 @@ declare interface UserResolveOptions {
* Prefer to resolve module requests as relative requests before falling back to modules
*/
preferRelative?: boolean;

/**
* Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots
*/
preferAbsolute?: boolean;
}
declare abstract class VariableInfo {
declaredScope: ScopeInfo;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2304,10 +2304,10 @@ enhanced-resolve@^4.0.0:
memory-fs "^0.5.0"
tapable "^1.0.0"

enhanced-resolve@^5.3.1:
version "5.4.1"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.4.1.tgz#c89b0c34f17f931902ef2913a125d4b825b49b6f"
integrity sha512-4GbyIMzYktTFoRSmkbgZ1LU+RXwf4AQ8Z+rSuuh1dC8plp0PPeaWvx6+G4hh4KnUJ48VoxKbNyA1QQQIUpXjYA==
enhanced-resolve@^5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.5.0.tgz#5f2ca7d511076541e2a30dc364a40c4f6c027bcd"
integrity sha512-b4a6BasBCoLzri4MdaeOlDMpls2oioI28CF17csMiav9dq46yvQaKPFNUrCHB6VqQokBDG2VIEEL81jMiQ6Wtw==
dependencies:
graceful-fs "^4.2.4"
tapable "^2.2.0"
Expand Down

0 comments on commit 32cea58

Please sign in to comment.