Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatsby fails after using Sass files with '@use 'sass:color'

Tags:

sass

gatsby

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.

like image 269
Henry Avatar asked Mar 29 '20 21:03

Henry


People also ask

How do I use Sass in Gatsby?

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.

Why can’t I use Sass in my CSS file?

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!

What CSS-loader does Gatsby use?

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:

What is @use in Sass?

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.


1 Answers

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"),
    },
  },
]
like image 134
ksav Avatar answered Nov 15 '22 13:11

ksav