feat(css): export PostCSS config type for type-safe configs#22792
Open
linyiru wants to merge 1 commit into
Open
feat(css): export PostCSS config type for type-safe configs#22792linyiru wants to merge 1 commit into
linyiru wants to merge 1 commit into
Conversation
1cdd72d to
f3fc791
Compare
Re-exports the PostCSS user config shape as `PostcssUserConfig` from
`vite`, so PostCSS configs can be typed against the same definition Vite
loads them with:
```ts
import type { PostcssUserConfig } from 'vite'
const config: PostcssUserConfig = { plugins: [] }
export default config
```
The type mirrors `postcss-load-config`'s `Config`. `postcss-load-config`
is bundled into Vite and its declarations use CommonJS `export =` syntax,
which `rolldown-plugin-dts` cannot re-export through the bundled `.d.ts`,
so the shape is mirrored from `postcss` types instead. A type-level test
asserts the mirror stays structurally identical to the upstream `Config`,
so it cannot silently drift.
Closes vitejs#19109
f3fc791 to
ec6bbc4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Re-exports the PostCSS user config shape as
PostcssUserConfigfromvite, so PostCSS configs can be typed against the same definition Vite loads them with:Closes #19109 (approved by @bluwy in the issue). This continues the work of #22525, which was closed by its author; the only outstanding review feedback there (from @sapphi-red) was "We should use the types from
postcss-load-config" instead of hand-writing the shape — this PR addresses exactly that.Why it mirrors the type instead of re-exporting it
A literal re-export turned out not to be possible with the current type-bundling setup, for two compounding reasons:
postcss-load-configis a bundleddevDependency, not a runtime dependency — so it can't be referenced as an externalimport('postcss-load-config')in the published.d.ts(consumers don't have it installed).export =syntax, whichrolldown-plugin-dtscannot re-export through the bundled.d.ts. A named re-export fails codegen (Export 'PostcssUserConfig' is not defined), and resolving the type inline pulls in non-externalizedpostcss/lib/*subpaths and fails on theirexport =declarations.So
PostcssUserConfigis defined by mirroringpostcss-load-config'sConfigusing the already-publicpostcsstypes. To make sure the mirror can never silently drift, a type-level test asserts it stays structurally identical to the upstreamConfig:This also caught a subtle bug in the previous attempt: it used
PostCSS.TransformCallbackwhereConfigactually usesTransformer(which additionally requirespostcssPlugin/postcssVersion).Tests
packages/vite/src/node/__tests_dts__/postcss.ts— a type-only test that (1) locksPostcssUserConfigtopostcss-load-config'sConfigviaEqual, and (2) checks both the array and object plugin formats are accepted and an invalid value is rejected. It runs as part ofpnpm typecheck(tsc -p src/node/__tests_dts__).pnpm typecheck,pnpm build-types(roll + check),eslint, andoxfmt --checkall pass.Notes for reviewers
Equallock is the approach I landed on given theexport =/ bundled-dep constraints above. If you'd prefer a literal reference (e.g. by also externalizingpostcsssubpaths inrolldown.dts.config.ts, which leaksimport ... from "postcss/lib/processor"into the public.d.ts), happy to switch — let me know.