Skip to content

Commit 8353353

Browse files
authored
docs: example of icss only and mixed (#1163)
1 parent 33e7879 commit 8353353

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

README.md

+105
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,111 @@ module.exports = {
12431243
};
12441244
```
12451245

1246+
### Separating `Interoperable CSS`-only and `CSS Module` features
1247+
1248+
The following setup is an example of allowing `Interoperable CSS` features only (such as `:import` and `:export`) without using further `CSS Module` functionality by setting `compileType` option for all files that do not match `*.module.scss` naming convention. This is for reference as having `ICSS` features applied to all files was default `css-loader` behavior before v4.
1249+
Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
1250+
1251+
An example case is assumed where a project requires canvas drawing variables to be synchronized with CSS - canvas drawing uses the same color (set by color name in JavaScript) as HTML background (set by class name in CSS).
1252+
1253+
**webpack.config.js**
1254+
1255+
```js
1256+
module.exports = {
1257+
module: {
1258+
rules: [
1259+
// ...
1260+
// --------
1261+
// SCSS ALL EXCEPT MODULES
1262+
{
1263+
test: /\.scss$/,
1264+
exclude: /\.module\.scss$/,
1265+
use: [
1266+
{
1267+
loader: 'style-loader'
1268+
},
1269+
{
1270+
loader: 'css-loader',
1271+
options: {
1272+
importLoaders: 1,
1273+
modules: {
1274+
compileType: 'icss'
1275+
}
1276+
}
1277+
},
1278+
{
1279+
loader: 'sass-loader'
1280+
},
1281+
],
1282+
},
1283+
// --------
1284+
// SCSS MODULES
1285+
{
1286+
test: /\.module\.scss$/,
1287+
use: [
1288+
{
1289+
loader: 'style-loader'
1290+
},
1291+
{
1292+
loader: 'css-loader',
1293+
options: {
1294+
importLoaders: 1,
1295+
modules: {
1296+
compileType: 'module'
1297+
}
1298+
}
1299+
},
1300+
{
1301+
loader: 'sass-loader'
1302+
},
1303+
],
1304+
},
1305+
// --------
1306+
// ...
1307+
},
1308+
};
1309+
```
1310+
1311+
**variables.scss**
1312+
1313+
File treated as `ICSS`-only.
1314+
1315+
```scss
1316+
$colorBackground: red;
1317+
:export {
1318+
colorBackgroundCanvas: $colorBackground;
1319+
}
1320+
```
1321+
1322+
**Component.module.scss**
1323+
1324+
File treated as `CSS Module`.
1325+
1326+
```scss
1327+
@import 'variables.scss';
1328+
.componentClass {
1329+
background-color: $colorBackground;
1330+
}
1331+
```
1332+
1333+
**Component.jsx**
1334+
1335+
Using both `CSS Module` functionality as well as SCSS variables directly in JavaScript.
1336+
1337+
```jsx
1338+
import svars from 'variables.scss';
1339+
import styles from 'Component.module.scss';
1340+
1341+
// Render DOM with CSS modules class name
1342+
// <div className={styles.componentClass}>
1343+
// <canvas ref={mountsCanvas}/>
1344+
// </div>
1345+
1346+
// Somewhere in JavaScript canvas drawing code use the variable directly
1347+
// const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
1348+
ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
1349+
```
1350+
12461351
## Contributing
12471352

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

0 commit comments

Comments
 (0)