3131Allows you to share variables between CSS and JS with Webpack and HMR.
3232</big ></p >
3333
34+
35+ This loader transforms special CSS files to JS modules.
36+
37+ * Shared variables between CSS and JS
38+ * HMR friendly, CSS changes are applied on the fly.
39+
40+ To be more JS friendly loader will:
41+
42+ * strip ` px ` part from css px numbers
43+ * convert dashes-case to camelCase
44+ * check for runtime config mutations and access of missing keys (only in dev or es6 mode)
45+
46+ ## Usage
47+
48+ ``` css
49+ /* variables.config.css */
50+
51+ @custom-media --small-device (max-width: 480px))
52+ :root {
53+ --primary-color: blue;
54+ --gutter: 30px;
55+ }
56+ ```
57+
58+ ```css
59+ /* component.css */
60+
61+ @import ' colors.config.css'
62+
63+ .component {
64+ color: var(--primary-color);
65+ margin: 0 var(--gutter);
66+ }
67+
68+ @media (--small-device) {
69+ /* styles for small viewport */
70+ }
71+
72+ ```
73+
74+ ``` js
75+ // component.js
76+ import variables from ' colors.config.css' ;
77+
78+ console .log (variables);
79+ /*
80+ variables = {
81+ primaryColor: 'blue';
82+ gutter: 30;
83+ smallDevice: '(max-width: 480px)'
84+ }
85+ */
86+
87+ component .style .color = variables .primaryColor ;
88+
89+ function add5ToGutter () {
90+ return 5 + variables .gutter ;
91+ }
92+ ```
93+
94+
3495## Install
3596
3697``` sh
@@ -41,8 +102,12 @@ yarn add --dev postcss-variables-loader
41102npm install --save-dev postcss-variables-loader
42103```
43104
105+ ** NB** : You need to process CSS somehow (eg [ postcss] ( https://github.com/postcss/postcss ) )
106+ and imports inside css (eg via [ postcss-import] ( https://github.com/postcss/postcss-import ) )
44107
45- Webpack with babel-loader
108+
109+ ** Recommended webpack configuration** :
110+ ` webpack.config.js ` with babel-loader
46111```
47112loaders: [
48113 {
@@ -54,52 +119,18 @@ loaders: [
54119 {
55120 test: /\.css$/,
56121 exclude: /\.config.css$/,
57- loader: 'css-loader'
122+ loader: 'css-loader!postcss-loader '
58123 }
59124]
60125```
61126
62- ES5 webpack config
63- ```
64- loaders: [
65- {
66- test: /\.config.css$/,
67- loader: 'postcss-variables-loader?es5=1'
68- },
69-
70- // dont forget to exclude *.config.css from other css loaders
71- {
72- test: /\.css$/,
73- exclude: /\.config.css$/,
74- loader: 'css-loader'
75- }
76- ]
77- ```
78- ## Usage
127+ ## Options
79128
80- ```
81-
82- # config/colors.config.css
83- :root {
84- --primaryColor: blue;
85- }
86-
87-
88-
89- # component.css (works with pre-processors too)
90- @import 'config/colors.config.css' // via postcss-import for example
129+ if ` production.env.NODE_ENV === 'development' ` it will try to wrap config inside ` Proxy ` in runtime.
130+ It is used to guard from accidental mutations or accessing missing keys.
131+ If you dont want this behaviour: pass ` es5=1 ` :
91132
92- .component {
93- color: var(--primaryColor);
94- }
95-
96-
97-
98- # component.js
99- import colors from 'config/colors.config.css';
100-
101- component.style.color = colors.primaryColor;
102- ```
133+ ` loader: 'postcss-variables-loader?es5=1' `
103134
104135## License
105136
0 commit comments