-
Notifications
You must be signed in to change notification settings - Fork 3.1k
CSS modules #17958
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
Merged
Merged
CSS modules #17958
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
64df21c
OH MY GOD
zackradisic 4b575ab
HOLY GODDAMN
zackradisic 73429a7
holy got damn
zackradisic e4f194b
noice
zackradisic da77711
fix tests and also fix no error in onLoad plugin
zackradisic ce14293
fix stale dev mode server thingy and add comment
zackradisic ba10823
Better errors nad tests
zackradisic 1c3a03a
update stuff
zackradisic 5a755e8
docs
zackradisic b0a85bc
Shoutout
zackradisic 46f3b60
update
zackradisic ff865c6
fix compoiler error
zackradisic 4da9435
Merge branch 'main' into zack/css-modules
zackradisic c5800d2
fix compile error
zackradisic 0d9f309
buncha changes
zackradisic 7bbb9dc
use std.AutoHashMap
zackradisic 2fe4139
Merge branch 'main' into zack/css-modules
zackradisic 8411696
typo
zackradisic 31e8534
resolve comments
zackradisic fb8ac4e
remo9ve banned wrods
zackradisic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# CSS Modules | ||
|
||
Bun's bundler also supports bundling [CSS modules](https://css-tricks.com/css-modules-part-1-need/) in addition to [regular CSS](/docs/bundler/css) with support for the following features: | ||
|
||
- Automatically detecting CSS module files (`.module.css`) with zero configuration | ||
- Composition (`composes` property) | ||
- Importing CSS modules into JSX/TSX | ||
- Warnings/errors for invalid usages of CSS modules | ||
|
||
A CSS module is a CSS file (with the `.module.css` extension) where are all class names and animations are scoped to the file. This helps you avoid class name collisions as CSS declarations are globally scoped by default. | ||
|
||
Under the hood, Bun's bundler transforms locally scoped class names into unique identifiers. | ||
|
||
## Getting started | ||
|
||
Create a CSS file with the `.module.css` extension: | ||
|
||
```css | ||
/* styles.module.css */ | ||
.button { | ||
color: red; | ||
} | ||
|
||
/* other-styles.module.css */ | ||
.button { | ||
color: blue; | ||
} | ||
``` | ||
|
||
You can then import this file, for example into a TSX file: | ||
|
||
```tsx | ||
import styles from "./styles.module.css"; | ||
import otherStyles from "./other-styles.module.css"; | ||
|
||
export default function App() { | ||
return ( | ||
<> | ||
<button className={styles.button}>Red button!</button> | ||
<button className={otherStyles.button}>Blue button!</button> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
The `styles` object from importing the CSS module file will be an object with all class names as keys and | ||
their unique identifiers as values: | ||
|
||
```tsx | ||
import styles from "./styles.module.css"; | ||
import otherStyles from "./other-styles.module.css"; | ||
|
||
console.log(styles); | ||
console.log(otherStyles); | ||
``` | ||
|
||
This will output: | ||
|
||
```ts | ||
{ | ||
button: "button_123"; | ||
} | ||
|
||
{ | ||
button: "button_456"; | ||
} | ||
``` | ||
|
||
As you can see, the class names are unique to each file, avoiding any collisions! | ||
|
||
### Composition | ||
|
||
CSS modules allow you to _compose_ class selectors together. This lets you reuse style rules across multiple classes. | ||
|
||
For example: | ||
|
||
```css | ||
/* styles.module.css */ | ||
.button { | ||
composes: background; | ||
color: red; | ||
} | ||
|
||
.background { | ||
background-color: blue; | ||
} | ||
``` | ||
|
||
Would be the same as writing: | ||
|
||
```css | ||
.button { | ||
background-color: blue; | ||
color: red; | ||
} | ||
|
||
.background { | ||
background-color: blue; | ||
} | ||
``` | ||
|
||
{% callout %} | ||
There are a couple rules to keep in mind when using `composes`: | ||
|
||
- A `composes` property must come before any regular CSS properties or declarations | ||
- You can only use `composes` on a **simple selector with a single class name**: | ||
|
||
```css | ||
#button { | ||
/* Invalid! `#button` is not a class selector */ | ||
composes: background; | ||
} | ||
|
||
.button, | ||
.button-secondary { | ||
/* Invalid! `.button, .button-secondary` is not a simple selector */ | ||
composes: background; | ||
} | ||
``` | ||
|
||
{% /callout %} | ||
|
||
### Composing from a separate CSS module file | ||
|
||
You can also compose from a separate CSS module file: | ||
|
||
```css | ||
/* background.module.css */ | ||
.background { | ||
background-color: blue; | ||
} | ||
|
||
/* styles.module.css */ | ||
.button { | ||
composes: background from "./background.module.css"; | ||
color: red; | ||
} | ||
``` | ||
|
||
{% callout %} | ||
When composing classes from separate files, be sure that they do not contain the same properties. | ||
|
||
The CSS module spec says that composing classes from separate files with conflicting properties is | ||
undefined behavior, meaning that the output may differ and be unreliable. | ||
{% /callout %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,6 +218,9 @@ export default { | |
page("bundler/html", "Bundle frontend & static sites", { | ||
description: `Zero-config HTML bundler for single-page apps and multi-page apps. Automatic bundling, TailwindCSS plugins, TypeScript, JSX, React support, and incredibly fast builds`, | ||
}), | ||
page("bundler/css", "Bundle, transpile, and minify CSS", { | ||
description: `Production ready CSS bundler with support for modern CSS features, CSS modules, and more.`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are you sure you want to word it that strongly right now? |
||
}), | ||
page("bundler/fullstack", "Fullstack Dev Server", { | ||
description: "Serve your frontend and backend from the same app with Bun's dev server.", | ||
}), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.