Visitar URL original
fix(config): normalize file URLs for options taking absolute paths by Pcmhacker-piro · Pull Request #22059 · webpack/webpack · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/007-file-url-normalization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack": patch
---

Normalize file URLs in `module.noParse`, `resolve.restrictions`, `resolveLoader.restrictions`, `stats.context`, `dotenv.dir`, and `experiments.buildHttp`.
61 changes: 54 additions & 7 deletions lib/config/normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ const fileUrlsToPaths = (pathsOrUrls) =>
typeof pathOrUrl === "string" ? fileUrlToPath(pathOrUrl) : pathOrUrl
);

/**
* Normalizes `module.noParse` converting any file URLs to paths.
* @param {import("../../declarations/WebpackOptions").NoParse=} noParse noParse option
* @returns {import("../../declarations/WebpackOptions").NoParse | undefined} normalized noParse
*/
const normalizeNoParse = (noParse) => {
if (typeof noParse === "string") {
return fileUrlToPath(noParse);
}
if (Array.isArray(noParse)) {
return fileUrlsToPaths(noParse);
}
return noParse;
};

/**
* Normalize `output.resourceHints` into its object form: the scalar / array /
* function shorthand becomes `{ initial: <value> }` (with `"preload"` aliased to
Expand Down Expand Up @@ -280,7 +295,17 @@ const getNormalizedWebpackOptions = (config) => ({
return { ...devServer };
}),
devtool: config.devtool,
dotenv: config.dotenv,
dotenv: optionalNestedConfig(config.dotenv, (dotenv) => {
if (typeof dotenv === "object" && dotenv !== null) {
return {
...dotenv,
...(dotenv.dir !== undefined && {
dir: optionalFileUrlToPath(dotenv.dir)
})
};
}
return dotenv;
}),
entry:
config.entry === undefined
? { main: {} }
Expand All @@ -292,9 +317,20 @@ const getNormalizedWebpackOptions = (config) => ({
: getNormalizedEntryStatic(config.entry),
experiments: nestedConfig(config.experiments, (experiments) => ({
...experiments,
buildHttp: optionalNestedConfig(experiments.buildHttp, (options) =>
Array.isArray(options) ? { allowedUris: options } : options
),
buildHttp: optionalNestedConfig(experiments.buildHttp, (options) => {
const values = Array.isArray(options)
? { allowedUris: options }
: options;
return {
...values,
...(values.cacheLocation !== undefined && {
cacheLocation: optionalFileUrlToPath(values.cacheLocation)
}),
...(values.lockfileLocation !== undefined && {
lockfileLocation: optionalFileUrlToPath(values.lockfileLocation)
})
};
}),
lazyCompilation: optionalNestedConfig(
experiments.lazyCompilation,
(options) => (options === true ? {} : options)
Expand Down Expand Up @@ -340,7 +376,7 @@ const getNormalizedWebpackOptions = (config) => ({
/** @type {ModuleOptionsNormalized} */
(
nestedConfig(config.module, (module) => ({
noParse: module.noParse,
noParse: normalizeNoParse(module.noParse),
unsafeCache: module.unsafeCache,
parser: keyedNestedConfig(module.parser, cloneObject, {
javascript: (parserOptions) => ({
Expand Down Expand Up @@ -555,9 +591,17 @@ const getNormalizedWebpackOptions = (config) => ({
),
resolve: nestedConfig(config.resolve, (resolve) => ({
...resolve,
...(resolve.restrictions !== undefined && {
restrictions: fileUrlsToPaths(resolve.restrictions)
}),
byDependency: keyedNestedConfig(resolve.byDependency, cloneObject)
})),
resolveLoader: cloneObject(config.resolveLoader),
resolveLoader: nestedConfig(config.resolveLoader, (resolveLoader) => ({
...resolveLoader,
...(resolveLoader.restrictions !== undefined && {
restrictions: fileUrlsToPaths(resolveLoader.restrictions)
})
})),
snapshot: nestedConfig(config.snapshot, (snapshot) => ({
resolveBuildDependencies: optionalNestedConfig(
snapshot.resolveBuildDependencies,
Expand Down Expand Up @@ -615,7 +659,10 @@ const getNormalizedWebpackOptions = (config) => ({
};
}
return {
...stats
...stats,
...(stats.context !== undefined && {
context: optionalFileUrlToPath(stats.context)
})
};
}),
target: config.target,
Expand Down
63 changes: 62 additions & 1 deletion test/Normalization.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,31 @@ describe("getNormalizedWebpackOptions", () => {
cacheDirectory: url,
cacheLocation: url
},
dotenv: {
dir: url
},
experiments: {
buildHttp: {
cacheLocation: url,
lockfileLocation: url
}
},
module: {
noParse: url
},
resolve: {
restrictions: [url]
},
resolveLoader: {
restrictions: [url]
},
snapshot: {
immutablePaths: [url],
managedPaths: [url],
unmanagedPaths: [url]
},
stats: {
context: url
}
});

Expand All @@ -35,22 +56,62 @@ describe("getNormalizedWebpackOptions", () => {
cacheDirectory: directory,
cacheLocation: directory
});
expect(options.dotenv).toMatchObject({
dir: directory
});
expect(options.experiments.buildHttp).toMatchObject({
cacheLocation: directory,
lockfileLocation: directory
});
expect(options.module.noParse).toBe(directory);
expect(options.resolve.restrictions).toEqual([directory]);
expect(options.resolveLoader.restrictions).toEqual([directory]);
expect(options.snapshot.immutablePaths).toEqual([directory]);
expect(options.snapshot.managedPaths).toEqual([directory]);
expect(options.snapshot.unmanagedPaths).toEqual([directory]);
expect(options.stats.context).toBe(directory);
});

it("leaves a plain path and a regular expression alone", () => {
const expression = /node_modules/;
const options = getNormalizedWebpackOptions({
context: directory,
recordsPath: false,
snapshot: { managedPaths: [directory, expression] }
dotenv: { dir: directory },
experiments: {
buildHttp: {
cacheLocation: directory,
lockfileLocation: directory
}
},
module: {
noParse: [directory, expression]
},
resolve: {
restrictions: [directory, expression]
},
resolveLoader: {
restrictions: [directory, expression]
},
snapshot: { managedPaths: [directory, expression] },
stats: { context: directory }
});

expect(options.context).toBe(directory);
expect(options.recordsInputPath).toBe(false);
expect(options.dotenv).toMatchObject({ dir: directory });
expect(options.experiments.buildHttp).toMatchObject({
cacheLocation: directory,
lockfileLocation: directory
});
expect(options.module.noParse).toEqual([directory, expression]);
expect(options.resolve.restrictions).toEqual([directory, expression]);
expect(options.resolveLoader.restrictions).toEqual([
directory,
expression
]);
expect(options.snapshot.managedPaths).toEqual([directory, expression]);
expect(options.stats.context).toBe(directory);
});
});
});
1 change: 1 addition & 0 deletions test/configCases/no-parse/file-url/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "parsed dep";
8 changes: 8 additions & 0 deletions test/configCases/no-parse/file-url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it("should not parse modules matching file URLs in noParse", () => {
expect(require("./not-parsed-a")).toBe("a");
expect(require("./not-parsed-b")).toBe("b");
});

it("should parse other modules", () => {
expect(require("./parsed")).toBe("parsed dep");
});
2 changes: 2 additions & 0 deletions test/configCases/no-parse/file-url/not-parsed-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (module.exports === 42) require("./does-not-exist-a");
module.exports = "a";
2 changes: 2 additions & 0 deletions test/configCases/no-parse/file-url/not-parsed-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (module.exports === 42) require("./does-not-exist-b");
module.exports = "b";
1 change: 1 addition & 0 deletions test/configCases/no-parse/file-url/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./dep");
5 changes: 5 additions & 0 deletions test/configCases/no-parse/file-url/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

const supportsFileUrlPaths = require("../../../helpers/supportsFileUrlPaths");

module.exports = () => supportsFileUrlPaths();
14 changes: 14 additions & 0 deletions test/configCases/no-parse/file-url/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const path = require("path");
const { pathToFileURL } = require("url");

/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
noParse: [
pathToFileURL(path.resolve(__dirname, "not-parsed-a")).href,
pathToFileURL(path.resolve(__dirname, "not-parsed-b")).href
]
}
};