Skip to content

Commit 166bd24

Browse files
fix: precise types for EXPECTED_ANY and three bugs they exposed (#22681)
* refactor(types): replace EXPECTED_ANY with real types outside the JS syntax files * refactor(types): narrow the remaining EXPECTED_ANY in stats, templates and tooling plugins * fix(debug): profile the taps of hook maps in ProfilingPlugin * fix: report import.meta errors raised inside async require callbacks * fix: match urlHints rules written as logical conditions * fix: match a rule's glob against relative values such as URL requests * refactor(types): type TupleSet keys as a tuple and reuse MagicCommentOptions * refactor(types): type the constant operator helpers by their compile-time operands * refactor(types): build deserialize placeholders from one typed options object * refactor(types): type fs implementations and passed-on schemas without casts * fix(profiling): Profile cached hook map taps and consolidate changesets * fix(devtool): include glob rules in cache key value type * fix(types): support Node 20 filesystem implementation declarations
1 parent ffa7ce7 commit 166bd24

79 files changed

Lines changed: 799 additions & 441 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Match `urlHints` rules written with `and`, `or`, `not` and `glob` conditions; profile hook map taps, such as resolver and parser hooks, in `ProfilingPlugin`; report `import.meta` option errors inside `require.ensure` callbacks, not crash; and replace `any`, `object` and `Function` with precise types in the type definitions.

‎.changeset/107-precise-object-function-types.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎docs/architecture.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Every schema is derived from the module declaring its options by `generate-schem
7676
- `declarations/WebpackOptions.ts` — **the source** for the configuration itself, and the only file left in `declarations/`. A definition of it that is also published as a schema of its own says `@publishes <path>`, and that schema is the reference.
7777
- `schemas/WebpackOptions.json` — top-level options.
7878
- `schemas/plugins/*.json` — per-plugin options (`BannerPlugin`, `IgnorePlugin`, `ProgressPlugin`, `SourceMapDevToolPlugin`, …).
79+
- `schemas/**/*.d.json.ts` — hand-written, beside a schema `lib/` passes on whole: TypeScript reads it in place of the JSON, typing the schema as `Schema`, whose literal JSON types it never matches.
7980

8081
**Tests** — structure, naming and running one case: [TESTING_DOCS.md](../TESTING_DOCS.md).
8182

‎eslint.config.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export default defineConfig([
8585

8686
// Ignore not supported files
8787
"**/*.d.ts",
88+
"**/*.d.json.ts",
8889

8990
// Ignore precompiled schemas
9091
"schemas/**/*.check.js",

‎lib/Compilation.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ const { toError } = ErrorHelpers;
170170
/** @import ModuleGraphConnection from "./graph/ModuleGraphConnection" */
171171
/** @import RequestShortener from "./util/RequestShortener" */
172172
/** @import RuntimeModule from "./runtime/RuntimeModule" */
173-
/** @import { RenderManifestEntry, RenderManifestOptions } from "./template/Template" */
173+
/**
174+
* @import {
175+
* RenderManifestEntry,
176+
* RenderManifestEntryTemplated,
177+
* RenderManifestOptions
178+
* } from "./template/Template"
179+
*/
174180

175181
/**
176182
* One language's source embedded in another's output.
@@ -4106,7 +4112,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
41064112
const warning = new (getModuleDependencyWarning())(
41074113
module,
41084114
w,
4109-
/** @type {EXPECTED_ANY} */ (w).loc || d.loc
4115+
w.loc || d.loc
41104116
);
41114117
this.warnings.push(warning);
41124118
hasProblems = true;
@@ -4118,7 +4124,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
41184124
const error = new (getModuleDependencyError())(
41194125
module,
41204126
e,
4121-
/** @type {EXPECTED_ANY} */ (e).loc || d.loc
4127+
e.loc || d.loc
41224128
);
41234129
this.errors.push(error);
41244130
hasProblems = true;
@@ -4729,9 +4735,10 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
47294735
* is over. `runtimeRequirementInTree` is a sync hook, so a tap cannot await
47304736
* the load itself; anything the tap reads off the requirement set must be
47314737
* captured before queueing, since `create` runs later.
4738+
* @template T
47324739
* @param {Chunk} chunk target chunk
4733-
* @param {() => Promise<EXPECTED_ANY>} load resolves the runtime module implementation
4734-
* @param {(loaded: EXPECTED_ANY) => RuntimeModule} create builds the runtime module from it
4740+
* @param {() => Promise<T>} load resolves the runtime module implementation
4741+
* @param {(loaded: T) => RuntimeModule} create builds the runtime module from it
47354742
* @param {ChunkGraph} chunkGraph the chunk graph
47364743
* @returns {void}
47374744
*/
@@ -6035,7 +6042,7 @@ This prevents using hashes of each other and should be avoided.`);
60356042
);
60366043

60376044
assetCacheItem.get((err, sourceFromCache) => {
6038-
/** @type {string | import("./template/TemplatedPathPlugin").TemplatePathFn<EXPECTED_ANY>} */
6045+
/** @type {RenderManifestEntryTemplated["filenameTemplate"]} */
60396046
let filenameTemplate;
60406047
/** @type {string} */
60416048
let file;
@@ -6741,7 +6748,7 @@ Object.defineProperty(compilationPrototype, "cache", {
67416748
set: util.deprecate(
67426749
/**
67436750
* Handles the value callback for this hook.
6744-
* @param {EXPECTED_ANY} _v value
6751+
* @param {Cache} _v value
67456752
*/
67466753
(_v) => {},
67476754
"Compilation.cache was removed in favor of Compilation.getCache()",

‎lib/asset/AssetModulesPlugin.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ const { PUBLIC_PATH_AUTO } = require("../util/publicPathPlaceholder");
5353
* @returns {Schema} definition
5454
*/
5555
const getSchema = (name) => {
56-
const { definitions } =
57-
/** @type {EXPECTED_ANY} */
58-
(require("../../schemas/WebpackOptions.json"));
56+
const { definitions } = require("../../schemas/WebpackOptions.json");
5957

6058
return {
6159
definitions,

‎lib/config/cli.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"use strict";
77

88
const path = require("path");
9-
const webpackSchema =
10-
/** @type {EXPECTED_ANY} */
11-
(require("../../schemas/WebpackOptions.json"));
9+
const webpackSchema = require("../../schemas/WebpackOptions.json");
1210

1311
/** @import { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema" */
1412
/** @typedef {JSONSchema4 | JSONSchema6 | JSONSchema7} JSONSchema */

‎lib/config/normalization.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const getCreatePathGlobRegExp = memoize(
2929
* OptimizationRuntimeChunkNormalized,
3030
* OutputNormalized,
3131
* PluginsNormalized,
32+
* RawDevTool,
3233
* WebpackOptions,
3334
* WebpackOptionsNormalized
3435
* } from "../../declarations/WebpackOptions"
@@ -766,7 +767,7 @@ const applyWebpackOptionsInterception = (options) => {
766767
// Prefer `all`, then `javascript`, then `css`
767768
const match = ["all", "javascript", "css"]
768769
.map((type) =>
769-
/** @type {Extract<WebpackOptionsNormalized["devtool"], EXPECTED_ANY[]>} */ (
770+
/** @type {Exclude<WebpackOptionsNormalized["devtool"], RawDevTool | undefined>} */ (
770771
_devtool
771772
).find((item) => item.type === type)
772773
)

‎lib/css/CssModule.js‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,30 +192,14 @@ class CssModule extends NormalModule {
192192
* @returns {CssModule} the deserialized object
193193
*/
194194
static deserialize(context) {
195-
const obj = new CssModule({
196-
// will be deserialized by Module
197-
layer: /** @type {EXPECTED_ANY} */ (null),
198-
type: "",
199-
// will be filled by updateCacheModule
200-
resource: "",
201-
context: "",
202-
request: /** @type {EXPECTED_ANY} */ (null),
203-
userRequest: /** @type {EXPECTED_ANY} */ (null),
204-
rawRequest: /** @type {EXPECTED_ANY} */ (null),
205-
loaders: /** @type {EXPECTED_ANY} */ (null),
206-
matchResource: /** @type {EXPECTED_ANY} */ (null),
207-
parser: /** @type {EXPECTED_ANY} */ (null),
208-
parserOptions: /** @type {EXPECTED_ANY} */ (null),
209-
generator: /** @type {EXPECTED_ANY} */ (null),
210-
generatorOptions: /** @type {EXPECTED_ANY} */ (null),
211-
resolveOptions: undefined,
212-
cssLayer: /** @type {EXPECTED_ANY} */ (null),
213-
supports: /** @type {EXPECTED_ANY} */ (null),
214-
media: /** @type {EXPECTED_ANY} */ (null),
215-
inheritance: /** @type {EXPECTED_ANY} */ (null),
216-
extractSourceMap: /** @type {EXPECTED_ANY} */ (null),
217-
exportType: /** @type {EXPECTED_ANY} */ (null)
218-
});
195+
const obj = new CssModule(
196+
// the rest is restored by `deserialize` and `updateCacheModule`
197+
/** @type {CssModuleCreateData} */ ({
198+
type: "",
199+
resource: "",
200+
context: ""
201+
})
202+
);
219203
obj.deserialize(context);
220204
return obj;
221205
}

‎lib/css/CssParser.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const getCommentCompilationWarning = memoize(() =>
9191
* CssAutoOrModuleParserOptions
9292
* } from "../../declarations/WebpackOptions"
9393
*/
94+
/** @import { MagicCommentOptions } from "../util/magicComment" */
9495

9596
/** @typedef {[number, number]} Range */
9697
/** @typedef {{ line: number, column: number }} Position */
@@ -1019,7 +1020,7 @@ class CssParser extends Parser {
10191020
* `CssUrlDependency`. Magic comments win over the project-wide default.
10201021
* @param {CssUrlDependency} dep dep
10211022
* @param {string} request asset request
1022-
* @param {Record<string, EXPECTED_ANY> | null | undefined} options parsed comment options
1023+
* @param {MagicCommentOptions | null | undefined} options parsed comment options
10231024
* @param {import("../graph/Dependency").DependencyLocation} loc location for warnings
10241025
* @returns {void}
10251026
*/
@@ -1252,10 +1253,10 @@ class CssParser extends Parser {
12521253
* @param {Range} range byte range to scan for magic comments
12531254
* @param {number} warnStart start offset of the loc for an invalid-`webpackIgnore` warning (computed lazily)
12541255
* @param {number} warnEnd end offset of that loc
1255-
* @returns {{ ignored: boolean, options: Record<string, EXPECTED_ANY> | null }} parsed options and `webpackIgnore` flag
1256+
* @returns {{ ignored: boolean, options: MagicCommentOptions | null }} parsed options and `webpackIgnore` flag
12561257
*/
12571258
const magicCommentsIn = (range, warnStart, warnEnd) => {
1258-
/** @type {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} */
1259+
/** @type {{ options: MagicCommentOptions | null, errors: (Error & { comment: Comment })[] | null }} */
12591260
const { options, errors } = parseCommentOptionsInRange(
12601261
/** @type {(Comment & { range: [number, number], value: string })[]} */ (
12611262
comments

0 commit comments

Comments
 (0)