I'm setting up a Gatsby Project with gatsby-plugin-sass
.
my gatsby-config.js
file:
module.exports = {
plugins: [
'gatsby-plugin-resolve-src',
'gatsby-plugin-sass',
'gatsby-plugin-react-helmet',
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/assets/images`,
},
},
],
}
I have the following styles file structure:
|
|src
|-styles
|--base
|--- _variables.scss
|--components
|--- _Buttons.scss
|--- ...
|--main.scss
Im my _Buttons.scss
file I'm importing like this:
@import '../base/variables';
@use 'sass:color as *;
When I'm trying to use the sass color functions like this (as specified on https://sass-lang.com/documentation/modules)
%buttons-focus {
background-color: color.adjust($primary-color, $alpha: -0.5);
}
I get the following Error:
Invalid CSS after "...nd-color: color": expected expression (e.g. 1px, bold), was ".adjust($primary-co"
In my main.scss I'm importing styles like this:
@import './components/Buttons';
Am I overseeing something?
I've tried changing up @use
with @import
, with no luck. For me it seems like the gatsby-sass-plugin is not aware of sass modules.
In Gatsby, Sass code can be translated to well-formatted, standard CSS using a plugin. Sass will compile .sass and .scss files to .css files for you, so you can write your stylesheets with more advanced features.
CSS files loaded as modules don’t allow any special Sass features and so can’t expose any Sass variables, functions, or mixins. In order to make sure authors don’t accidentally write Sass in their CSS, all Sass features that aren’t also valid CSS will produce errors. Otherwise, the CSS will be rendered as-is. It can even be extended!
Note: Gatsby is using css-loader@^5.0.0. By default, the Dart implementation of Sass ( sass) is used. To use the implementation written in Node ( node-sass ), you can install node-sass instead of sass and pass it into the options as the implementation:
The @use rule loads mixins, functions, and variables from other Sass stylesheets, and combines CSS from multiple stylesheets together. Stylesheets loaded by @use are called "modules". Sass also provides built-in modules full of useful functions.
gatsby-plugin-sass
uses node-sass
under the hood. But in order to support built-in modules with @use, you need to configure gatsby-plugin-sass
to use dart-sass
instead. See below.
Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead.
By default the node implementation of Sass (node-sass) is used. To use the implementation written in Dart (dart-sass), you can install sass instead of node-sass and pass it into the options as the implementation:
npm install --save-dev sass
gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-sass`,
options: {
implementation: require("sass"),
},
},
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With