6

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.

Henry
  • 63
  • 4

1 Answers1

10

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.

Built-In Modules - sass-lang

Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead.

Alternative Sass Implementations - gatsby-plugin-sass

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"),
    },
  },
]
ksav
  • 20,015
  • 6
  • 46
  • 66
  • 2
    Likewise - I spent some time before I found this, so I've filled a request to add some docs/errors which help identify the problem: https://github.com/sass/node-sass/issues/2991 – wu-lee Oct 03 '20 at 14:14