Repository navigation
perf: validate options with a precompiled check - #756
Conversation
webpack validates the plugin options with `compiler.validate`, which without a check loads `ajv` and compiles `options.json`: about 200ms of every production build start, since webpack adds this plugin as its default minimizer. `src/options.check.js` is that schema precompiled with `schema-utils precompile`, passed as the fourth argument to `compiler.validate` and to the `schema-utils` fallback. Valid options are only checked; `ajv` still reports what is wrong with invalid ones, so the errors stay the same. `npm run fix:schema` regenerates the check and `lint:schema` fails when it is out of date with the schema. Babel copies it as is, it is already written for Node.js 10.13.0 and the transforms made it three times the size. Every case of the validation tests now also asserts that the check agrees with the schema. `schema-utils` goes to 4.6.0 for the runtime helpers the check loads, which also changes how two error messages print the property types.
🦋 Changeset detectedLatest commit: 5a1e8da The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #756 +/- ##
=======================================
Coverage 97.67% 97.67%
=======================================
Files 5 5
Lines 1718 1718
Branches 664 668 +4
=======================================
Hits 1678 1678
Misses 39 39
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe plugin adds a generated validator for its options schema and passes it to the webpack compiler validation path and the Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
|
The failing Fix: #760. Once it's merged, I'll merge Generated by Claude Code |
webpack 5.112's CSS minimizer emits declarations in a different order, so the two embedded-source tests that compared the stylesheet verbatim failed on the rows that install webpack@latest (Node 10/12/14). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QYPkbvwyRWt2SHgXDoYRdo
Summary
webpack adds this plugin as its default minimizer in production mode. Validating its options with
compiler.validate(schema, options, configuration)loadsajvand compilesoptions.json, about 200 ms on every production build start.src/options.check.jsis that schema precompiled withschema-utils precompile(schema-utils 4.6.0, webpack/schema-utils#231). It's passed as the check argument tocompiler.validateand to theschema-utilsvalidatefallback for older webpack versions:ajvnever loads.ajvvalidation, so the error messages stay the same.Measurements
Production
webpack(config)withnew MinimizerPlugin()as the minimizer, in a fresh Node 22 process, median of 5:webpack(config)ajvloadedChanges
src/index.jspassesrequire("./options.check.js")to both validation paths.npm run fix:schemaregenerates the check.lint:schema(run bynpm run lint) fails when the check is out of date withoptions.json. A change to a description doesn't trigger it, because annotations are stripped before compiling.transformIgnorePatterns, and ESLint, Prettier, cspell and coverage ignore the generated file.types/options.check.d.tsis generated bybuild:typeslike the other declarations.schema-utilsgoes to^4.6.0, because the check loads itsucs2lengthhelper fromschema-utils/dist/runtime. The lockfile was on 4.3.3, and later versions print property types in error messages. That changes two snapshots, e.g.object { path, export? }becomesobject { path: non-empty string, export?: non-empty string }.Tests
createCompilerintest/validate-options.test.jsnow also asserts that the precompiled check accepts or rejects the options exactly as the fullschema-utilsvalidation does, across every valid and invalid case in that file. webpack'scompiler.validatenever runs the schema when a check says the options are valid, so a check that disagrees would let invalid options through.npm run lintand the full suite (781 tests, 837 snapshots) pass locally.dist/options.check.jsis byte-identical to the source and works under Node.Generated by Claude Code
Summary by CodeRabbit