Skip to content

Commit b1ae770

Browse files
docs: point the type-check guidance at is(kind) instead of instanceof (#22622)
Claude-Session: https://claude.ai/code/session_01FuxKwZepsFsatQwWC2XFQy
1 parent f899873 commit b1ae770

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Before writing a path-shape regexp, read the top of that file and its `module.ex
116116
A list of module, source or dependency types in `lib/` claims those are all there will ever be; the day one is added it is silently wrong, and the new type takes whichever branch the list forgot. Instead:
117117

118118
- **Ask the object.** `module.getSourceTypes()`, `chunkGraph.getModuleSourceTypes(module)`, `moduleGraph.getParentModule(dependency)` answer for whatever exists, plugin types included. Know `getParentModule`: concatenation re-points an incoming connection at the javascript module absorbing the referencing one, so `connection.originModule.type` reads `javascript` for a css `url()` or html `src`, while the dependency's own module still reads `css`/`html`.
119-
- **Match the class, not its name.** `dependency instanceof URLDependency` says what `dependency.type === "new URL()"` only approximates, and survives renames.
119+
- **Ask the kind, not the class.** `module.is("css")` and `isDependencyKind(dependency, "url")` say what `dependency.type === "new URL()"` only approximates, survive renames and need no `require` of the class; chunks, groups and blocks answer `isHotUpdate()`, `isEntrypoint()` and `isAsyncBlock()`. Keep `instanceof` for built-ins and for telling a class from a plain value. A new kind: [docs/architecture.md](docs/architecture.md#adding-a-dependency-type-or-runtime-requirement).
120120

121121
**A feature flag is the same list in disguise**: gating on `options.experiments.<x>` to mean "which types can exist here" reads like configuration but goes stale the same way.
122122

‎docs/architecture.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Every schema is derived from the module declaring its options by `generate-schem
107107

108108
**New dependency type:** pair the `Dependency` subclass with a `DependencyTemplate` (emits the code), register the class with `makeSerializable(...)`, and wire the template into `compilation.dependencyTemplates`. One whose `getResourceIdentifier()` returns a value resolves a module, so it also needs a factory in `compilation.dependencyFactories`, or the build throws `No module factory available for dependency type`. A plugin outside the repo reaches all three through `compiler.webpack` — `Dependency`, `template.DependencyTemplate`, `module.NullFactory` and `util.makeSerializable` are public for it, and `makeSerializable` registers globally, so build the class once rather than per `apply`.
109109

110+
**A class callers must tell apart** declares its kind after the class, `CssModule.KINDS = new Set([...NormalModule.KINDS, "css"])`, and maps the kind to the class in the `ModuleKinds` typedef in `lib/module/Module.js` (or `DependencyKinds` in `lib/graph/Dependency.js`), which types `is(kind)`. Kinds are kebab-case; a subclass inherits its parent's, so a plugin's `NormalModule` subclass still answers `is("normal")`. Test a dependency with `isDependencyKind`, never `dependency.is` directly: a plugin's dependency built on another webpack copy's `Dependency` has no `is()`.
111+
110112
**New runtime requirement:** declare it in `lib/runtime/RuntimeGlobals.js`, emit it with a `RuntimeModule` subclass, and inject it through the `compilation.hooks` pair matching its scope: `additionalModuleRuntimeRequirements`/`runtimeRequirementInModule` per module, `additionalChunkRuntimeRequirements`/`runtimeRequirementInChunk` per chunk, or `additionalTreeRuntimeRequirements`/`runtimeRequirementInTree` per runtime chunk and everything it loads.
111113

112114
Tap every hook with a unique plugin-name string.

0 commit comments

Comments
 (0)