Runtime CSS Modules #4542
Replies: 8 comments 8 replies
-
Are imports going to be left untouched? So the main CSS file is going to have the |
Beta Was this translation helpful? Give feedback.
-
This would make What if, like Remix Utils, there's a LinksFunction and a DynamicLinksFunction? The first is for static links, the second for links that requires data, the first are prefetched, the dynamic links are not prefetched. This would also let devs use the loader data to dynamically add link tags while still having prefetching for other links. |
Beta Was this translation helpful? Give feedback.
-
The setup step seems like something that could be a config instead. module.exports = {
cssModules: true
} If the idea is to let devs choose the path used for CSS modules it could be an option too module.exports = {
cssModules: "/_css/*"
} Then for the import { css } from "@remix-run/node"
import { css } from "@remix-run/cloudflare"
import { css } from "@remix-run/deno" The |
Beta Was this translation helpful? Give feedback.
-
This approach makes sense and I'm keen to see CSS Modules support in Remix. It would be great to have a way to distribute React components and associated CSS modules, e.g. on npm, in a way that is compatible with Remix and other frameworks. At the moment, distribution of scoped styles as part of a package seems to be an area that runtime CSSinJS still does well at, and it would be good to see some static options that are compatible across frameworks. I created https://github.com/govuk-react/govuk-react which uses runtime CSSinJS, and made some progress on moving towards CSS modules a few years ago in an experimental replacement. I was hoping CSS modules support would eventually converge 😄 My understanding of the approach described here is that would not be possible? Or could this could somehow be extended to also support CSS module imports in node_modules? |
Beta Was this translation helpful? Give feedback.
-
I guess it would make some thing more difficult or impossible if we do it at runtime. like purging the unused css. |
Beta Was this translation helpful? Give feedback.
-
Another potential issue here is the DX for those using any sort of post-processor. If I'm using Sass/PostCSS for CSS Modules and Sass/PostCSS for my plain CSS, presumably I'm already running a separate dev/pre-build process for the latter. Doing this at runtime for CSS Modules means I'd need to wire that up twice and move dev dependencies to my server build. Also: said post-processor might depend on Node, and the user is shipping to another runtime without interop. |
Beta Was this translation helpful? Give feedback.
-
There are another couple of concerns I have with this approach.
|
Beta Was this translation helpful? Give feedback.
-
Looks like this will be superseded by #4631 - linking for anyone else subscribed here :) |
Beta Was this translation helpful? Give feedback.
-
CSS Modules in Remix
The idea here is a way to support CSS Modules without screwing around with bundlers. Everything happens at runtime.
Setup
First, add a resource route to handle css modules from the browser:
Next create a CSS module function to use in your loaders with the same path:
Now you're ready to use them:
Using CSS Modules
Define a CSS module.
Use it in a route with your newly created
css
function:It's definitely more steps than you're used to with css modules but there are a couple things we like about it:
useLoaderData
.How it works
createCssModules
The point of this is to generate class names for the server render, not produce the actual CSS asset.
The function this creates (we called it
css
in the code above), does the following things:@imports
with fingerprints (no concatting){links}
.{styles}
Resource Route
The point of the resource route is to actually serve the asset.
/_css/root.module.css
Questions
Caching Perf
/_css/root.module-[hash].css
, split it at-
and read the file on disk in the resource route, then sentimmutable
cache headers.Runtime Perf
createCssModules(routePath, cache)
Hurdles
data
to links againRelated to #2214
Beta Was this translation helpful? Give feedback.
All reactions