Skip to content

Commit 3766e86

Browse files
feat: support icss
1 parent 700d9ca commit 3766e86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2711
-1468
lines changed

README.md

+110-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ Good loaders for requiring your assets are the [file-loader](https://github.com/
2222
and the [url-loader](https://github.com/webpack/url-loader) which you should specify
2323
in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
2424

25-
> ⚠️ **CSS Module users should continue using `v1.0.0` in the meantime**
26-
2725
## Requirements
2826

2927
This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.
@@ -271,6 +269,116 @@ module.exports = {
271269
};
272270
```
273271

272+
### PostCSS
273+
274+
To begin, you'll need to install `postcss-loader`:
275+
276+
```console
277+
$ npm install postcss-loader --save-dev
278+
```
279+
280+
Visit [postcss-loader](https://github.com/postcss/postcss-loader) readme for more information.
281+
282+
**webpack.config.js**
283+
284+
```js
285+
module.exports = {
286+
module: {
287+
rules: [
288+
{
289+
test: /\.css$/,
290+
use: [
291+
{
292+
loader: 'css-loader',
293+
},
294+
{
295+
loader: 'postcss-loader',
296+
},
297+
],
298+
},
299+
],
300+
},
301+
};
302+
```
303+
304+
### CSS Modules
305+
306+
To begin, you'll need to install `postcss-loader`, `postcss-icss-values`, `postcss-icss-selectors`, `postcss-icss-composes` and `postcss-icss-keyframes`:
307+
308+
```console
309+
$ npm install postcss-loader postcss-icss-values postcss-icss-selectors postcss-icss-composes postcss-icss-keyframes --save-dev
310+
```
311+
312+
New `postcss` plugins for css modules use the [ICSS](https://github.com/css-modules/icss) specification.
313+
314+
**webpack.config.js**
315+
316+
```js
317+
module.exports = {
318+
module: {
319+
rules: [
320+
{
321+
test: /\.modules.css$/,
322+
use: [
323+
{
324+
loader: 'css-loader',
325+
options: {
326+
importLoaders: 1,
327+
},
328+
},
329+
{
330+
loader: 'postcss-loader',
331+
options: {
332+
plugins: (loader) => [
333+
require('postcss-icss-values')(),
334+
require('postcss-icss-selectors')({
335+
mode: 'global', // Can be `local`
336+
generateScopedName: require('generic-names')({
337+
hashPrefix: '',
338+
context: loader.rootContext,
339+
})
340+
}),
341+
require('postcss-icss-composes')(),
342+
require('postcss-icss-keyframes')({
343+
generateScopedName: require('generic-names')({
344+
hashPrefix: '',
345+
context: loader.rootContext,
346+
})
347+
}),
348+
],
349+
},
350+
},
351+
],
352+
},
353+
],
354+
},
355+
};
356+
```
357+
358+
### Custom Export
359+
360+
Due to the [ICSS](https://github.com/css-modules/icss) specification, you can export any values from your styles.
361+
362+
Just add `:export {}` with exported values. You can use `postcss-loader` with own `postcss` plugin to automate these actions. [Writing a PostCSS Plugin](Writing a PostCSS Plugin).
363+
364+
Example:
365+
366+
**style.css**
367+
368+
```css
369+
:export {
370+
color: black;
371+
}
372+
```
373+
374+
**file.js**
375+
376+
```js
377+
import styles from 'style.css'
378+
379+
console.log(styles.color); // Output `black`
380+
```
381+
274382
## Contributing
275383

276384
Please take a moment to read our contributing guidelines if you haven't yet done so.

0 commit comments

Comments
 (0)