webpack-loader-css-styled-jsx-component
Webpack loader for transpiling css files into styled-jsx component.
Reasoning
styled-jsx as a method of delivery stylesheets to documnet
We want to useAdvantages:
- Claims to be blazingly fast
- Supports dynamic styles -> themability
- Only loads css used by the components on the page
- Does not repeat css for resusable components
- Cleans up after itself
- Plays well with isomorphic app
Disadvantages:
- no stylesheet bundles -> stylesheets are not cached (however, js bundles that include css can still be cached)
We do not want to write css in string literals in javascript files
Onle language per file. Syntax highlighting.
We want to take advantage of css pre-processing
We want to inject themes into css
BEM methodology
We useAll styles are applied by class names. Therefore, we have no need for scoping styles.
Installation
npm i -D webpack-loader-css-styled-jsx-component
Peer Dependencies
Usage
Component.jsx
import Styles from './Component.scss'
const theme = {
primaryColor: '#ff0000'
}
export default Component = () => (
<div className='component'>
<Styles theme={theme}/>
</div>
)
Component.scss
.component {
color: -var(--theme-primaryColor);
}
webpack.config.js
const path = require('path')
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'babel-loader',
options: {
babelrc: false,
extends: path.resolve(__dirname, './.babelrc')
}
},
'webpack-loader-css-styled-jsx-component',
'sass-loader'
]
}
]
}
}
.babelrc
{
"presets": [
"env",
"react"
],
"plugins": [
"styled-jsx/babel"
]
}
Notes
- We also use rollup for bundling reusable ui components library - https://github.com/360incentives/rollup-plugin-sass-styled-jsx-component