Skip to content

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

@qeleb

Description

@qeleb

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions