Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CSS Modules support #4852

Merged
merged 95 commits into from Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
8b9d1b8
add initial CSS Modules integration
markdalgleish Nov 24, 2022
4cc9f8e
only write CSS bundle and source map to disk
markdalgleish Nov 24, 2022
5a55f96
Only emit CSS from the CSS bundle build
markdalgleish Nov 24, 2022
72de4a3
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Nov 24, 2022
fc57bae
bump css-bundle version
markdalgleish Nov 24, 2022
5d96e1c
clean up CSS build output logic
markdalgleish Nov 28, 2022
48aaaf3
dispose CSS compiler in browser compiler
markdalgleish Nov 28, 2022
9f40c89
remove emitCss option, clean up server output
markdalgleish Nov 28, 2022
83eb997
make CSS Modules object type readonly
markdalgleish Nov 28, 2022
0dcd2dc
add initial CSS Modules integration test
markdalgleish Nov 29, 2022
33b09aa
refactor local fork of CSS Modules plugin
markdalgleish Nov 29, 2022
1352cc5
bundle CSS to support composes property
markdalgleish Dec 5, 2022
ec7e32a
put CSS Modules behind v2_cssBundle future flag
markdalgleish Dec 6, 2022
6381250
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 6, 2022
b4e2168
update css-bundle versions
markdalgleish Dec 6, 2022
1599d3a
rename v2_cssBundle flag to unstable_cssBundle
markdalgleish Dec 6, 2022
88a0761
remove CSS Module entry point cache
markdalgleish Dec 6, 2022
f31cd7a
inline setup and buildCssModulesJs functions
markdalgleish Dec 6, 2022
600897a
remove build context, clean up path resolution
markdalgleish Dec 6, 2022
657ef3e
use esbuild's resolver in lightningcss
markdalgleish Dec 7, 2022
55653e7
refactor CSS Modules plugin
markdalgleish Dec 7, 2022
08a6f66
upgrade lightningcss
markdalgleish Dec 7, 2022
d6d4d70
use relative path for filename
markdalgleish Dec 7, 2022
ccf946f
add workaround for CSS Modules hash clashes
markdalgleish Dec 7, 2022
e63338d
simplify esbuild plugin, add PluginData type
markdalgleish Dec 8, 2022
ea3e3bd
namespace compiled CSS suffix
markdalgleish Dec 8, 2022
be03658
use complete path for virtual CSS file name
markdalgleish Dec 8, 2022
e2c2d1a
split CSS Modules test cases, add more, clean up
markdalgleish Dec 8, 2022
abdd5d3
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 8, 2022
1dc7da5
bump css-bundle versions
markdalgleish Dec 8, 2022
5254c2e
add unstable_cssBundle default to test package
markdalgleish Dec 8, 2022
3117fe4
ensure scoped grid line names don't break build
markdalgleish Dec 8, 2022
3e95c07
add CSS Modules to deterministic build test
markdalgleish Dec 8, 2022
78f74bd
ensure stable hashes when using import composition
markdalgleish Dec 8, 2022
57f688c
add comment explaining pattern order
markdalgleish Dec 8, 2022
1e0c3a1
add comment about cwd limitation
markdalgleish Dec 8, 2022
41f65cf
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 8, 2022
d17e6cd
add test for root relative image URLs
markdalgleish Dec 9, 2022
0641e8c
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 11, 2022
c8b8c33
fix esbuild resolve error after upgrade
markdalgleish Dec 11, 2022
21c9e0b
add CSS bundle deduplication test
markdalgleish Dec 11, 2022
568adf2
clean up tests
markdalgleish Dec 12, 2022
de3e1c3
rename test
markdalgleish Dec 12, 2022
039233b
support import side effects in client entry
markdalgleish Dec 12, 2022
6ae02b2
add trailing newline to tsconfig
markdalgleish Dec 12, 2022
4a38a94
update FutureConfig type, sort keys
markdalgleish Dec 12, 2022
2c5eb5a
add changeset
markdalgleish Dec 12, 2022
be5b9cc
add initial docs
markdalgleish Dec 12, 2022
61ac3c5
migrate to postcss-modules, add tests and clean up
markdalgleish Dec 13, 2022
9709d24
hoist postcss imports
markdalgleish Dec 13, 2022
385aed5
format tsconfig
markdalgleish Dec 13, 2022
a774503
reorder imports
markdalgleish Dec 13, 2022
66b0ad8
reorder options
markdalgleish Dec 13, 2022
77080dd
add root relative class composition to test
markdalgleish Dec 13, 2022
beec55a
clean up css-bundle dist, add typings field
markdalgleish Dec 13, 2022
778e4d2
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 13, 2022
a698f16
update note about CSS Modules support
markdalgleish Dec 13, 2022
ac9e013
fix docs formatting
markdalgleish Dec 13, 2022
1bf956f
update readme and description
markdalgleish Dec 13, 2022
e06857c
update comment
markdalgleish Dec 13, 2022
718909f
don't provide default object for entrypoints
markdalgleish Dec 13, 2022
696e2c0
remove redundant type annotations
markdalgleish Dec 13, 2022
d05e29f
remove redundant relative path
markdalgleish Dec 13, 2022
df3aa42
simplify css compiler build step
markdalgleish Dec 13, 2022
fffb86d
add comment explaining exports in css entry
markdalgleish Dec 13, 2022
5baeff3
add comment explaining resolved module contents
markdalgleish Dec 13, 2022
0243f83
simplify plugin data
markdalgleish Dec 13, 2022
3438678
use arrow functions for fixtures to reduce nesting
markdalgleish Dec 13, 2022
8fc72b6
nit formatting
markdalgleish Dec 13, 2022
a5a1e27
switch from patch to minor release
markdalgleish Dec 13, 2022
806f033
convert unstable_cssBundle value to nested object
markdalgleish Dec 13, 2022
61088c2
reinstate relative path to improve CSS comments
markdalgleish Dec 13, 2022
a3e95d1
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 13, 2022
6347b79
flatten CSS Modules feature flag
markdalgleish Dec 13, 2022
84131d3
make virtual css paths relative to root dir
markdalgleish Dec 13, 2022
8257efc
rename CSS entry module ID and plugin
markdalgleish Dec 14, 2022
b8f6d42
convert cssBundleHref to named export
markdalgleish Dec 14, 2022
7ed88e0
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 18, 2022
2a5098e
bump css-bundle versions
markdalgleish Dec 18, 2022
fa2d219
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 19, 2022
19dc2b7
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 20, 2022
141ff02
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 20, 2022
aece1ee
don't output CSS Modules in server/app build
markdalgleish Dec 20, 2022
fd6d3ed
Update docs/guides/migrating-react-router-app.md
markdalgleish Dec 21, 2022
6a30fb5
Update docs/guides/styling.md
markdalgleish Dec 21, 2022
cc96521
Update docs/guides/styling.md
markdalgleish Dec 21, 2022
5cb0efb
Update docs/guides/styling.md
markdalgleish Dec 21, 2022
cc5dc69
Update packages/remix-dev/compiler/compileBrowser.ts
markdalgleish Dec 21, 2022
3ebae45
Update packages/remix-dev/compiler/compileBrowser.ts
markdalgleish Dec 21, 2022
f0c7b82
Update docs/guides/styling.md
markdalgleish Dec 21, 2022
0da587d
dedupe CSS in dev
markdalgleish Dec 21, 2022
c7357fa
remove explicit single CSS file warning from docs
markdalgleish Dec 21, 2022
146ae5d
remove redundant optional chaining
markdalgleish Dec 21, 2022
ea9f2dc
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Dec 22, 2022
e42dd07
Merge branch 'dev' into markdalgleish/css-modules
markdalgleish Jan 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/little-avocados-fetch.md
@@ -0,0 +1,9 @@
---
"@remix-run/css-bundle": patch
"@remix-run/dev": patch
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved
"@remix-run/react": patch
"@remix-run/server-runtime": patch
"@remix-run/testing": patch
---

Add unstable support for CSS Modules via `future.unstable_cssBundle` feature flag
5 changes: 3 additions & 2 deletions docs/guides/migrating-react-router-app.md
Expand Up @@ -492,7 +492,7 @@ The answer to all of these questions is up to your bundler, _not you_. We think

**Note:** Remix does not currently support CSS processing directly. If you use preprocessors like Sass, Less, or PostCSS, you can run those as a separate process in development.

We also do not yet support CSS Modules, as that requires compiler integration and current approaches are not aligned with our design philosophy. We are actively working on a solution and plan to have an API for CSS Modules very soon.
We also do not yet support [CSS Modules][css-modules] by default, but it is currently [available behind a feature flag.][css-modules]
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved

</docs-info>

Expand Down Expand Up @@ -642,4 +642,5 @@ Now then, go off and _remix your app_. We think you'll like what you build along
[routing-in-remix]: ./routing
[styling-in-remix]: ./styling
[frequently-asked-questions]: ../pages/faq
[common-gotchas]: ../pages/gotchas
[common-gotchas]: ../pages/currently
[css-modules]: ./styling#css-modules
71 changes: 71 additions & 0 deletions docs/guides/styling.md
Expand Up @@ -766,6 +766,76 @@ Other CSS-in-JS libraries will have a similar setup. If you've got a CSS framewo

NOTE: You may run into hydration warnings when using Styled Components. Hopefully [this issue][styled-components-issue] will be fixed soon.

## CSS Modules

<docs-warning>This feature is unstable and currently only available behind a feature flag. We're confident in the use cases it solves but the API and implementation may change in the future.</docs-warning>

<docs-warning>Since all CSS Modules styles end up in a single CSS file at the end of the build, we recommend that you only use this styling approach for smaller applications, or sparingly within larger applications. If you're concerned about bundle size, you should probably look at [Tailwind](#tailwind-css) or [Regular Stylesheets](#regular-stylesheets) instead.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure that we need this warning. I think a single module bundle is probably fine for decently large apps since it's easy to cache, and CSS files are rarely a bottleneck before some pretty serious scale. I think we can talk about the tradeoffs without making it look scary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does sound a little scary if someone were evaluating remix and saw this
Not discounting that the warning isn't true. Maybe there are strategies for reducing the bundle size like minifying the CSS.

just noticed out production CSS output isn't compressed in our app 😅
Remix doesn't minify CSS by default?
Not a big deal for our internal tool or web UI, but can be for public facing UIs were download speed matters

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remix doesn't minify CSS by default?

No, Remix doesn't process CSS by default at all (aside than hashing the filename for effective versioning/caching). This is explained in the same doc.

That said, CSS Modules is opening the door a bit on this bigger question. We should absolutely be able to minify the production build of your CSS Modules bundle IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a single module bundle is probably fine for decently large apps since it's easy to cache

But change any style will break the cache.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But change any style will break the cache.

As it should! I'm not saying there are no potential perf tradeoffs here, just that we shouldn't overstate them because:

CSS files are rarely a bottleneck before some pretty serious scale

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should absolutely be able to minify the production build of your CSS Modules bundle IMO.

The generated CSS is already being minified since it's being bundled by esbuild honouring the same minification settings as the JS output.

</docs-warning>

To enable [CSS Modules], set the `unstable_cssBundle` feature flag in `remix.config.js`.

```js filename=remix.config.js
/** @type {import('@remix-run/dev').AppConfig} */
module.exports = {
future: {
unstable_cssBundle: true,
},
// ...
};
```

With this feature flag enabled, the Remix compiler will now generate a single CSS file containing all CSS Modules styles in your application. Note that any [regular stylesheet imports](http://localhost:3000/docs/en/v1/guides/styling#regular-stylesheets) will remain as separate files.
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved

Unlike many other tools in the React ecosystem, insertion of this CSS bundle into the page is not handled for you automatically. Instead, we ensure that you always have control over the link tags on your page. This lets you decide where the CSS bundle is inserted and in what order relative to other style sheets.
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved

To get access to the CSS bundle, first install the `@remix-run/css-bundle` package.

```sh
npm install @remix-run/css-bundle
```

Then, import the CSS bundle's `href` and add it to a link descriptor, most likely in `root.tsx` so that it applies to your entire application.

```tsx filename=root.tsx lines=[2,6]
import type { LinksFunction } from "@remix-run/node"; // or cloudflare/deno
import cssBundleHref from "@remix-run/css-bundle";
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved

export const links: LinksFunction = () => {
return [
{ rel: "stylesheet", href: cssBundleHref },
// ...
];
};
```

You're all set! You can now opt into CSS Modules via the `.module.css` file name convention, for example:
markdalgleish marked this conversation as resolved.
Show resolved Hide resolved

```css filename=app/components/button/styles.module.css
.root {
border: solid 1px;
background: white;
color: #454545;
}
```

```tsx filename=app/components/button/index.js lines=[1,9]
import styles from "./styles.module.css";

export const Button = React.forwardRef(
({ children, ...props }, ref) => {
return (
<button
{...props}
ref={ref}
className={styles.root}
/>
);
}
);
Button.displayName = "Button";
```

[custom-properties]: https://developer.mozilla.org/en-US/docs/Web/CSS/--*
[link]: ../components/link
[route-module-links]: ../route/links
Expand All @@ -774,3 +844,4 @@ NOTE: You may run into hydration warnings when using Styled Components. Hopefull
[styled-components-issue]: https://github.com/styled-components/styled-components/issues/3660
[tailwind]: https://tailwindcss.com
[tailwind-intelli-sense-extension]: https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss
[css modules]: https://github.com/css-modules/css-modules