Skip to content

docs: example of icss only and mixed #1163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,217 @@ module.exports = {
};
```

### Using `Interoperable CSS` features only

The following setup is an example of allowing `Interoperable CSS` features such as `:import` and `:export` without using modules by setting `compileType` option.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
// ...
// --------
// SCSS
{
test: /\.scss$/,
exclude: /\.module\.scss$/,
use: [
{
loader: 'style-loader',
options: {}
},
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
compileType: 'icss'
}
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
{
loader: 'resolve-url-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
],
},
// --------
// ...
},
};
```

Using SCSS variables in JavaScript

**variables.scss**

```scss
:export {
colorBackgroundCanvas: red;
}
```

**app.js**

```js
import svars from 'variables.scss';
ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
```

### Using SCSS variables for both CSS modules and JavaScript if variables are defined in non-module files

The following setup is an example of allowing `Interoperable CSS` features such as `:import` and `:export` without using modules (by setting `compileType` option) together with synchronizing variable values between CSS and Javascript.

Assume case where one wants some canvas drawing to be the same color (set by color name) as HTML background (set by class name).

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
// ...
// --------
// SCSS GENERAL
{
test: /\.scss$/,
exclude: /\.module\.scss$/,
use: [
{
loader: 'style-loader',
options: {}
},
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
compileType: 'icss'
}
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
{
loader: 'resolve-url-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
],
},
// --------
// SCSS MODULES
{
test: /\.module\.scss$/,
use: [
{
loader: 'style-loader',
options: {}
},
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
compileType: 'module',
mode: 'local',
exportGlobals: false,
namedExport: false,
exportLocalsConvention: 'asIs',
exportOnlyLocals: false
}
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
{
loader: 'resolve-url-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
],
},
// --------
// ...
},
};
```

**variables.scss**

```scss
$colorBackground: red;
:export {
colorBackgroundCanvas: $colorBackground;
}
```

**Component.module.scss**

```scss
@import 'variables.scss';
.componentClass {
background-color: $colorBackground;
}
```

**Component.jsx**

```jsx
import svars from 'variables.scss';
import styles from 'Component.module.scss';

// render DOM with CSS modules class name
// <div className={styles.componentClass}>
// <canvas ref={mountsCanvas}/>
// </div>

// somewhere in JavaScript canvas drawing code use the variable directly
// const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
```

## Contributing

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