Refactor CSS node types with reusable type builders#267
Open
bartveneman wants to merge 2 commits into
Open
Conversation
Factor the ~44 near-identical node-type declarations (CSSNode & {
type, type_name, clone(): ToPlain<Self> }) into two small internal
generics, Leaf<> and WithClone<>, instead of hand-rolling each one.
Purely a type-level change (zero runtime/JS output difference,
zero public API change) that shrinks the largest file in the
published tarball (node-types.d.ts: 25.17kB -> 22.18kB), cutting
overall unpacked install size by ~1.5%.
Contributor
|
| 📦 Package | 📋 Versions |
|---|---|
| tinybench | 2 versions
|
| @babel/helper-validator-identifier | 2 versions
|
| js-tokens | 2 versions
|
| @babel/parser | 2 versions
|
| @babel/types | 2 versions
|
| @babel/helper-string-parser | 2 versions
|
| @emnapi/core | 3 versions
|
| @emnapi/wasi-threads | 2 versions
|
| @emnapi/runtime | 3 versions
|
| @rolldown/binding-wasm32-wasi | 2 versions
|
| obug | 2 versions
|
| picomatch | 2 versions
|
| postcss-value-parser | 2 versions
|
| glob-parent | 2 versions
|
| yaml | 2 versions
|
| is-arrayish | 2 versions
|
| get-tsconfig | 2 versions
|
| semver | 2 versions
|
| @oxc-project/types | 2 versions
|
| rolldown | 2 versions
|
| @rolldown/binding-android-arm64 | 2 versions
|
| @rolldown/binding-darwin-arm64 | 2 versions
|
| @rolldown/binding-darwin-x64 | 2 versions
|
| @rolldown/binding-freebsd-x64 | 2 versions
|
| @rolldown/binding-linux-arm-gnueabihf | 2 versions
|
| @rolldown/binding-linux-arm64-gnu | 2 versions
|
| @rolldown/binding-linux-arm64-musl | 2 versions
|
| @rolldown/binding-linux-ppc64-gnu | 2 versions
|
| @rolldown/binding-linux-s390x-gnu | 2 versions
|
| @rolldown/binding-linux-x64-gnu | 2 versions
|
| @rolldown/binding-linux-x64-musl | 2 versions
|
| @rolldown/binding-openharmony-arm64 | 2 versions
|
| @rolldown/binding-win32-arm64-msvc | 2 versions
|
| @rolldown/binding-win32-x64-msvc | 2 versions
|
| tinyexec | 2 versions
|
💡 To find out what depends on a specific package, run: pnpm -r why example-package
🎉 Package Size Decrease
| 📦 Package | 📏 Base Size | 📏 Source Size | 📈 Size Change |
|---|---|---|---|
| @projectwallace/css-parser | 41 kB | 41 kB | -69 B |
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FuvCERnifYS6ggHAXMwVkD
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.
Summary
This refactor introduces reusable type builders (
Toggle,WithClone, andLeaf) to reduce duplication across ~40 CSS node type definitions. These are pure type-level utilities that compile away entirely, improving maintainability without runtime cost.Key Changes
Added
Toggle<HasKey, ValueKey, T>type: A discriminated union pattern for optional fields gated by boolean flags (e.g.,has_prelude/prelude). Replaces repeated inline union patterns throughout the file.Added
WithClone<T>type: Wraps a node type to add aclone()method with proper return type inference (ToPlain<T>). Eliminates repetitive clone method declarations.Added
Leaf<Type, Name, Extra>type: CombinesCSSNode, type tag, and optional extra fields for leaf nodes (nodes without children). Significantly reduces boilerplate for simple node types.Refactored all node type definitions to use these builders:
StyleSheet,Rule,Atrule,Declaration,Selector,SelectorList,Block,Comment,RawIdentifier,Number,Dimension,String,Hash,Function,Operator,Parenthesis,Url,UnicodeRange,ValueTypeSelector,ClassSelector,IdSelector,AttributeSelector,PseudoClassSelector,PseudoElementSelector,Combinator,UniversalSelector,NestingSelector,NthSelector,NthOfSelector,LangSelectorAtrulePrelude,MediaQuery,MediaFeature,MediaType,ContainerQuery,SupportsQuery,SupportsDeclaration,LayerName,PreludeSelectorList,PreludeOperator,FeatureRangeImplementation Details
Toggletype replaces verbose union patterns like({ readonly has_X: true } & { readonly X: T }) | ({ readonly has_X: false } & { readonly X: null })with a single, reusable construct.BlockChildnow usesTogglefor itshas_next/next_siblingpattern.https://claude.ai/code/session_01FuvCERnifYS6ggHAXMwVkD