solidjs / solidjs/solid-vite-plugin
CSS Modules classes are compiled as dynamic/reactive variables rather than static strings (Optimization)
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 520
- Forks
- 70
- Avg merge
- 23h 35m
- Merged PRs (30d)
- 39
Description
@ryansolid Just following up on the issue we discussed during your Friday stream.
CSS Module classes are substituted for static strings by Vite AFTER the Solid babel plugin runs, so the CSS class strings are not compiled into static text directly in the DOM strings. This has a considerable performance effect because it breaks up long strings that would otherwise be inserted into the DOM at once statically.
This can be alleviated slightly with a custom Vite plugin like this one, which adds @once to at least compile these without reactivity:
{
name: 'vite-plugin-optimize-solid-css-modules',
enforce: 'pre',
transform: {
handler: code => ({
code: code.replace(/class=\{([a-zA-Z '"`[\].-]+|(?:`(?:\$\{[a-zA-Z '"`[\].-]+\}\s*)+)`)\}/g, 'class={/*@once*/$1}'),
map: null,
}),
filter: { id: /\.[mc]?[jt]sx$/ },
},
}
But without a modification to the order that CSS modules are processed, getting CSS modules to match the performance of plain CSS won’t be possible. I’ve taken a stab at fixing it myself, but the fix may need to be contributed to Vite. Needs some further investigation.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the Solid Babel plugin and Vite CSS Modules processing order; the issue provides a custom Vite plugin as a comparison point. Determine whether the processing-order change belongs in this repository or Vite, and define completion as CSS Module class strings being compiled as static, non-reactive DOM text without the workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript, vite
- Domain
- build-system, frontend, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100