|
| 1 | +--- |
| 2 | +title: Native CSS |
| 3 | +sort: 27 |
| 4 | +contributors: |
| 5 | + - alexander-akait |
| 6 | +--- |
| 7 | + |
| 8 | +This guide shows how to use webpack native CSS handling with `experiments.css`. |
| 9 | + |
| 10 | +T> `experiments.css` is still experimental. It is expected to become the default in webpack v6, but behavior can still change while development continues. |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +Enable native CSS support in your webpack configuration: |
| 15 | + |
| 16 | +**webpack.config.js** |
| 17 | + |
| 18 | +```js |
| 19 | +export default { |
| 20 | + experiments: { |
| 21 | + css: true, |
| 22 | + }, |
| 23 | +}; |
| 24 | +``` |
| 25 | + |
| 26 | +With this option enabled, webpack can process CSS without adding `css-loader` and `mini-css-extract-plugin` for the basic flow. |
| 27 | + |
| 28 | +## Importing CSS |
| 29 | + |
| 30 | +After enabling the experiment, import `.css` files directly from JavaScript: |
| 31 | + |
| 32 | +**src/index.js** |
| 33 | + |
| 34 | +```js |
| 35 | +import "./styles.css"; |
| 36 | + |
| 37 | +const element = document.createElement("h1"); |
| 38 | +element.textContent = "Hello native CSS"; |
| 39 | +document.body.appendChild(element); |
| 40 | +``` |
| 41 | + |
| 42 | +**src/styles.css** |
| 43 | + |
| 44 | +```css |
| 45 | +h1 { |
| 46 | + color: #1f6feb; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Webpack will process the CSS and include it in the build output. |
| 51 | + |
| 52 | +## CSS Modules |
| 53 | + |
| 54 | +Native CSS support also includes CSS Modules. Use the `.module.css` extension: |
| 55 | + |
| 56 | +**src/button.module.css** |
| 57 | + |
| 58 | +```css |
| 59 | +.button { |
| 60 | + background: #0d6efd; |
| 61 | + color: white; |
| 62 | + border: 0; |
| 63 | + border-radius: 4px; |
| 64 | + padding: 8px 12px; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +**src/index.js** |
| 69 | + |
| 70 | +```js |
| 71 | +import * as styles from "./button.module.css"; |
| 72 | + |
| 73 | +const button = document.createElement("button"); |
| 74 | +button.className = styles.button; |
| 75 | +button.textContent = "Click me"; |
| 76 | +document.body.appendChild(button); |
| 77 | +``` |
| 78 | + |
| 79 | +T> CSS Modules class names are exported. By default, named exports are enabled for CSS modules. |
| 80 | + |
| 81 | +## Production Build |
| 82 | + |
| 83 | +With `experiments.css: true`, webpack provides native CSS extraction and content hashing for CSS assets in production builds. |
| 84 | + |
| 85 | +Compared to the classic setup: |
| 86 | + |
| 87 | +- Traditional approach: `css-loader` + `mini-css-extract-plugin` |
| 88 | +- Native approach: `experiments.css` with built-in extraction behavior |
| 89 | + |
| 90 | +This reduces configuration and keeps the CSS pipeline closer to webpack core features. |
| 91 | + |
| 92 | +## Experimental Status & Known Limitations |
| 93 | + |
| 94 | +`experiments.css` is explicitly experimental, so treat it as opt-in and test carefully before broad rollout. |
| 95 | + |
| 96 | +Known points to keep in mind: |
| 97 | + |
| 98 | +- APIs and behavior may still evolve before webpack v6 defaults. |
| 99 | +- Some loader-specific options are not part of native CSS behavior (for example, loader-specific filters). |
| 100 | +- If your project relies on advanced loader chains, validate each part before migrating fully. |
| 101 | + |
| 102 | +## Migration Guide |
| 103 | + |
| 104 | +If you currently use `css-loader` and `mini-css-extract-plugin`, migrate in small steps. |
| 105 | + |
| 106 | +### 1) Start from a classic setup |
| 107 | + |
| 108 | +**webpack.config.js** |
| 109 | + |
| 110 | +```js |
| 111 | +import MiniCssExtractPlugin from "mini-css-extract-plugin"; |
| 112 | + |
| 113 | +export default { |
| 114 | + module: { |
| 115 | + rules: [ |
| 116 | + { |
| 117 | + test: /\.css$/i, |
| 118 | + use: [MiniCssExtractPlugin.loader, "css-loader"], |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + plugins: [new MiniCssExtractPlugin()], |
| 123 | +}; |
| 124 | +``` |
| 125 | + |
| 126 | +### 2) Switch to native CSS |
| 127 | + |
| 128 | +**webpack.config.js** |
| 129 | + |
| 130 | +```js |
| 131 | +export default { |
| 132 | + experiments: { |
| 133 | + css: true, |
| 134 | + }, |
| 135 | +}; |
| 136 | +``` |
| 137 | + |
| 138 | +### 3) Keep imports unchanged |
| 139 | + |
| 140 | +Your JS imports can stay the same: |
| 141 | + |
| 142 | +```js |
| 143 | +import "./styles.css"; |
| 144 | +import * as styles from "./button.module.css"; |
| 145 | +``` |
| 146 | + |
| 147 | +### 4) Validate output in development and production |
| 148 | + |
| 149 | +Check that: |
| 150 | + |
| 151 | +- styles are applied correctly in development, |
| 152 | +- generated CSS files are emitted for production, |
| 153 | +- CSS Modules exports match your existing usage. |
0 commit comments