-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
docs(guides): add native CSS guide for experiments.css #8193
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
alexander-akait
merged 15 commits into
webpack:main
from
phoekerson:docs/guide-native-css-experiments
Apr 26, 2026
+271
−0
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
601fdf1
docs: add Modern Web Platform guide (Web Components, Import Maps, PWA)
phoekerson b5cbcc0
Update src/content/guides/modern-web-platform.mdx
phoekerson b9a7679
Merge branch 'webpack:main' into main
phoekerson 75da3df
docs(modern-web-platform): link DOMException to MDN and add CDN impor…
phoekerson 954181c
Merge branch 'feat/update-footer-openjs-ai-policy' of https://github.…
phoekerson 40a434d
fix: add AI Coding Assistants Policy link to footer
phoekerson cfb7f05
fix: add OpenJS Foundation AI Coding Assistants Policy to footer
phoekerson 26d0bcb
fix(footer): format OpenJS AI policy link to satisfy Prettier
phoekerson 232c509
Merge branch 'docs/guide-native-css-experiments' of https://github.co…
phoekerson 0ebba45
docs(guides): add native css guide for experiments.css
phoekerson c8122cb
docs(guides): fix contributors field in native css guide frontmatter
phoekerson 2213920
docs(guides): expand native css migration with loader option mapping
phoekerson e46c3cf
docs(guides): recommend css/auto with module naming for native css mi…
phoekerson 22879b6
docs(guides): clarify native CSS migration from css-loader/mini-css-e…
phoekerson 9e84d89
docs(guides): remove css-loader from native CSS migration diff
phoekerson 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,153 @@ | ||
| --- | ||
| title: Native CSS | ||
| sort: 27 | ||
| contributors: | ||
| - alexander-akait | ||
| --- | ||
|
|
||
| This guide shows how to use webpack native CSS handling with `experiments.css`. | ||
|
|
||
| T> `experiments.css` is still experimental. It is expected to become the default in webpack v6, but behavior can still change while development continues. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| Enable native CSS support in your webpack configuration: | ||
|
|
||
| **webpack.config.js** | ||
|
|
||
| ```js | ||
| export default { | ||
| experiments: { | ||
| css: true, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| With this option enabled, webpack can process CSS without adding `css-loader` and `mini-css-extract-plugin` for the basic flow. | ||
|
|
||
| ## Importing CSS | ||
|
|
||
| After enabling the experiment, import `.css` files directly from JavaScript: | ||
|
|
||
| **src/index.js** | ||
|
|
||
| ```js | ||
| import "./styles.css"; | ||
|
|
||
| const element = document.createElement("h1"); | ||
| element.textContent = "Hello native CSS"; | ||
| document.body.appendChild(element); | ||
| ``` | ||
|
|
||
| **src/styles.css** | ||
|
|
||
| ```css | ||
| h1 { | ||
| color: #1f6feb; | ||
| } | ||
| ``` | ||
|
|
||
| Webpack will process the CSS and include it in the build output. | ||
|
|
||
| ## CSS Modules | ||
|
|
||
| Native CSS support also includes CSS Modules. Use the `.module.css` extension: | ||
|
|
||
| **src/button.module.css** | ||
|
|
||
| ```css | ||
| .button { | ||
| background: #0d6efd; | ||
| color: white; | ||
| border: 0; | ||
| border-radius: 4px; | ||
| padding: 8px 12px; | ||
| } | ||
| ``` | ||
|
|
||
| **src/index.js** | ||
|
|
||
| ```js | ||
| import * as styles from "./button.module.css"; | ||
|
|
||
| const button = document.createElement("button"); | ||
| button.className = styles.button; | ||
| button.textContent = "Click me"; | ||
| document.body.appendChild(button); | ||
| ``` | ||
|
|
||
| T> CSS Modules class names are exported. By default, named exports are enabled for CSS modules. | ||
|
|
||
| ## Production Build | ||
|
|
||
| With `experiments.css: true`, webpack provides native CSS extraction and content hashing for CSS assets in production builds. | ||
|
|
||
| Compared to the classic setup: | ||
|
|
||
| - Traditional approach: `css-loader` + `mini-css-extract-plugin` | ||
| - Native approach: `experiments.css` with built-in extraction behavior | ||
|
|
||
| This reduces configuration and keeps the CSS pipeline closer to webpack core features. | ||
|
|
||
| ## Experimental Status & Known Limitations | ||
|
|
||
| `experiments.css` is explicitly experimental, so treat it as opt-in and test carefully before broad rollout. | ||
|
|
||
| Known points to keep in mind: | ||
|
|
||
| - APIs and behavior may still evolve before webpack v6 defaults. | ||
| - Some loader-specific options are not part of native CSS behavior (for example, loader-specific filters). | ||
| - If your project relies on advanced loader chains, validate each part before migrating fully. | ||
|
|
||
| ## Migration Guide | ||
|
|
||
| If you currently use `css-loader` and `mini-css-extract-plugin`, migrate in small steps. | ||
|
|
||
| ### 1) Start from a classic setup | ||
|
|
||
| **webpack.config.js** | ||
|
|
||
| ```js | ||
| import MiniCssExtractPlugin from "mini-css-extract-plugin"; | ||
|
|
||
| export default { | ||
| module: { | ||
| rules: [ | ||
| { | ||
| test: /\.css$/i, | ||
| use: [MiniCssExtractPlugin.loader, "css-loader"], | ||
| }, | ||
| ], | ||
| }, | ||
| plugins: [new MiniCssExtractPlugin()], | ||
| }; | ||
| ``` | ||
|
|
||
| ### 2) Switch to native CSS | ||
|
|
||
| **webpack.config.js** | ||
|
|
||
| ```js | ||
| export default { | ||
| experiments: { | ||
| css: true, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ### 3) Keep imports unchanged | ||
|
|
||
| Your JS imports can stay the same: | ||
|
|
||
| ```js | ||
| import "./styles.css"; | ||
| import * as styles from "./button.module.css"; | ||
| ``` | ||
|
|
||
| ### 4) Validate output in development and production | ||
|
|
||
| Check that: | ||
|
|
||
| - styles are applied correctly in development, | ||
| - generated CSS files are emitted for production, | ||
| - CSS Modules exports match your existing usage. | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add options (from
css-loaderandstyle-loaderandmini-css-extract-plugin) for CSS modules here and how to migrate, most of options are already implemented, some of them should be solved in other way, feel free to start fromcss-loader, thenmini-css-extract-pluginand thenstyle-loader, you can find all supported options here:style-loadernow isexportType: "style"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I updated it