Previously my setup was as follows
// --------
// SCSS GENERAL
{
test: /\.(scss)$/,
exclude: /\.module\.(scss)$/,
use: [
// ...
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true
}
},
// ...
]
},
// --------
// SCSS MODULES
{
test: /\.module\.(scss)$/,
use: [
// ...
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
mode: 'local',
localIdentName: (development) ? '[name]__[local]--[hash:base64:5]' : '[hash:base64:16]'
},
localsConvention: 'asIs',
onlyLocals: false
}
},
// ...
]
},
and having :export directive in non-module scss file worked
_variables.scss_
:export {
canvasTouchPointColor: $colorSelectionText;
canvasBackgroundColor: $colorHeaderBackground;
}
_somejavascript.js_
import styles from 'shared/scss/variables.scss';
console.log('styles.canvasTouchPointColor', styles.canvasTouchPointColor);
console.log('styles.canvasBackgroundColor', styles.canvasBackgroundColor);
Now, using the new configuration syntax
// --------
// SCSS GENERAL
{
test: /\.(scss)$/,
exclude: /\.module\.(scss)$/,
use: [
// ...
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {auto: false} // unnecessary, but let us be verbose
}
},
// ...
]
},
// --------
// SCSS MODULES
{
test: /\.module\.(scss)$/,
use: [
// ...
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
mode: 'local',
localIdentName: (development) ? '[name]__[local]--[hash:base64:5]' : '[hash:base64:16]',
exportLocalsConvention: 'asIs',
exportOnlyLocals: false
}
}
},
// ...
]
},
bringing SCSS variables into JavaScript does not work for SCSS files which are not considered modules.
Note, that moving :export to _module_ file does bring back variables into JS ->
_variables.module.scss_
@import "variables.scss";
:export {
canvasTouchPointColor: $colorSelectionText;
canvasBackgroundColor: $colorHeaderBackground;
}
_somejavascript.js_
import styles from 'shared/scss/variables.module.scss';
console.log('styles.canvasTouchPointColor', styles.canvasTouchPointColor);
console.log('styles.canvasBackgroundColor', styles.canvasBackgroundColor);
does work.
Is the assumption that the new implementation allows SCSS :export only to be used when modules are true?
If so can it be reverted to the previous behaviour?
Thanks!
Please set https://github.com/webpack-contrib/css-loader/blob/master/src/options.json#L39 compileType: 'icss'
// --------
// SCSS GENERAL
{
test: /\.(scss)$/,
exclude: /\.module\.(scss)$/,
use: [
// ...
{
loader: 'css-loader',
options: {
importLoaders: 3,
sourceMap: true,
modules: {
compileType: 'icss'
}
}
},
// ...
]
},
// --------
// SCSS MODULES
{
test: /\.module\.(scss)$/,
// ...
},
This setup works
Tried compileType before submitting the issue and it did not work. Was using it as shown below as I did not want modules for this loader chain
modules: {
compileType: 'icss'
auto: false
}
Apparently auto needs not to be defined. Together with the fact that previously _modules_ could be left out altogether (see OP _Expected Behavior / Situation_) the behaviour has changed quite a lot.
What about having loader behave as
modules: {
compileType: 'icss'
}
if modules key is left out, instead of behaving as
modules: undefined
as that would kind of resemble as what css-loader in practice did before v4?
There are two cases:
So both configuration is valid, but maybe we should docs this
Using compileType: 'icss' is rare case, let's add docs about it
see the pull request
Using
compileType: 'icss'is rare case, let's add docs about it
containing project variables in some variables.scss (and not variables.module.scss) and then @import'ing them in both component.module.scss'es as well as JavaScript files directly IMHO is not some edge case.
@evilebottnawi PR for doc addition passing now, please review. thanks!
Fixed https://github.com/webpack-contrib/css-loader/pull/1163, logic for icss and auto will be in the next major release (because it is potential breaking change), but it will be soon, because postcss@8 will be released in near future