CSS Modules classes are compiled as dynamic/reactive variables rather than static strings (Optimization)

Open
#223 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
typescript, vite

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.

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.

Dominant language
TypeScript
Stars
520
Forks
70
Avg merge
23h 35m
Merged PRs (30d)
39

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from solidjs/solid-vite-plugin

All issues in solidjs/solid-vite-plugin

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.