Skip to content

Move webpack/webpack to TypeScript #124

@avivkeller

Description

@avivkeller

I'd like us to seriously consider migrating webpack core from JavaScript-with-JSDoc to TypeScript, compiling down to JavaScript on release so that nothing changes for consumers in non-TS environments.

After a few weeks investigating how we generate our type definitions, my conclusion is that our current setup has reached the point where it costs us more than it gives us. The combination of TypeScript + CommonJS + JSDoc does not interact well, and the workarounds we've built to make it interact are a maintenance burden.

Webpack core ships a types.d.ts file that consumers depend on. That file is generated from 18,000+ JSDoc comments annotations across the source tree. Plain tsc --declaration --allowJs --emitDeclarationOnly is not sufficient to produce the types.d.ts we ship. We rely on a custom generator at webpack/tooling/generate-types that wraps the TypeScript compiler API and post-processes its output.

The custom tooling exists because the "official" path (letting tsc emit declarations from JSDoc) falls short in several practical ways for a codebase of webpack's size and shape:

  • Cross-file inference through CommonJS require boundaries is fragile.
  • Internal symbols leak into public declarations unless explicitly pruned. (For instance, the wall of @typedef-s in every file)
  • Declaration merging and module-augmentation patterns common in plugin APIs are hard to express purely in JSDoc.

The result is a pipeline where we run tsc against all of our .js files for type-checking (treating them as TypeScript via checkJs), and then run a separate, custom generator to produce the shipped declarations. We pay the cost of being a TypeScript project without getting the primary benefit of being one.

Stated plainly: JSDoc was not designed to be a TypeScript source format. TypeScript's JSDoc support was added pragmatically, so projects could opt into type-checking without rewriting source files. It is not a great primary authoring format for an 18,000-annotation codebase whose public type surface is itself a deliverable. We are paying TypeScript's costs (compiler in the loop, type-check step in CI, custom generator) while authoring in a format that was never meant to scale to this size.

So, I propose we, slowly of course, move the entire webpack core repository to TypeScript source. Compile to JavaScript at build/release time so that downstream consumers, including non-TS environments and Node-as-runtime use cases, see no functional change. The shipped types.d.ts is generated by tsc directly from .ts sources, the way TypeScript was designed to work.

Essentially,

  • lib/**/*.jslib/**/*.ts.
  • The custom generate-types pipeline (and related pipelines) is retired in favor of tsc --declaration.
  • The published npm package still contains compiled .js plus .d.ts.
  • CI runs tsc once, as the authoritative type-checker and declaration emitter, instead of running tsc and the custom generator in series.

Note

GSoC 2026 is starting soon. GSoC candidates will be relying on the types.d.ts file for the new documentation. While this should not interact with their project, it may be best to discuss implementation after GSoC concludes. My suggested target for this would be webpack 7.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions