Commit 7c65f3f
Neal Granger
Add support for js style modules.
Css styles can be written as plain js modules.
```js
// style.css.js
module.exports = {
body: {
fontFamily: 'sans-serif',
},
'.app': {
width: 400,
},
'@media screen and (min-width: 800px)': {
'.app': {
width: 800,
},
},
}
```
```css
/* generated css */
body {
font-family: sans-serif;
}
.app {
width: 400px;
}
@media screen and (min-width: 800px) {
.app {
width: 800px;
}
}
```
Multiple values for blocks and properties can be defined as arrays.
```js
// style.css.js
module.exports = {
body: [{
height: '100%',
}, {
fontFamily: 'sans-serif',
}],
'.app': {
display: ['block', 'flex'],
},
};
```
```css
/* generated css */
body {
height: 100%;
}
body {
font-family: sans-serif;
}
.app {
display: block;
display: flex;
}
```
Style modules can be written in es6 and transformed with `babel-loader`.
```js
// webpack.config.js
import babel from 'webpack-config-babel';
import postcss from 'webpack-config-postcss';
export default compose(
...,
postcss(),
babel(),
)();
```
(Include the `babel` config _after_ `postcss` config since webpack executes loaders from bottom to top.)
```js
// style.css.js
export default {
'.app': {
width: 400,
},
};
```
```css
/* generated css */
.app {
width: 400px;
}
```
In es6 style modules, named exports are interpreted as css classes.
```js
// style.css.js
export const app = {
width: 400,
};
```
```css
/* generated css */
.app {
width: 400px;
}
```
This preserves import/export continuity when using css modules.
```js
// style.css.js
export const base = {
fontSize: 16,
};
export const normal = {
composes: 'base',
color: 'black',
};
export const active = {
composes: 'base',
color: 'green',
};
```
```js
// component.js
import {createElement} from 'react';
import {normal, active} from './style.css.js';
export default ({state, children}) =>
<div className={state === 'active' ? active : normal}>
{children}
</div>
```1 parent e99c6ac commit 7c65f3f
2 files changed
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
83 | 96 | | |
84 | 97 | | |
85 | 98 | | |
| |||
0 commit comments