question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SASS :export does not work with built-in SASS support throwing 'Selector ":export" is not pure'

See original GitHub issue

Bug report

Describe the bug

I use :export syntax to export variables from scss files and import them in JS. This worked great with next-sass, but does not even compile with the built-in SASS support.

To Reproduce

in Component.jsx:

import colors from './component.module.scss'

component.module.scss:

:export {
  white: #ffffff;
  black: #000000;
}

Expected behavior

colors in Component.jsx should be an object that looks something like this:

{
  white: "#ffffff",
  black: "#000000"
}

Actual behavior

It does not compile the scss file and throws:

ModuleBuildError: Module build failed (from ./node_modules/next/node_modules/css-loader/dist/cjs.js):
CssSyntaxError

(1:0) Selector ":export" is not pure (pure selectors must contain at least one local class or id)

System information

  • OS: Windows 10 (WSL)
  • Version of Next.js: 9.3.4
  • Version of SASS: 1.26.3

Additional information

next.config.js does not contain any code related to CSS or SASS

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:20
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
BigglesZXcommented, Jan 28, 2021

@lipoolock I had to rename my variables.scss to variables.module.scss for this feature to work (from a hint here). That was all!

5reactions
nkalinovcommented, May 4, 2020

The issue is that the built-in css-loader options are using by default “pure” CSS modules. I managed to work around this issue by updating this option for the specific css modules files that we use in our project (.module.scss):

/**
 * Stolen from https://stackoverflow.com/questions/10776600/testing-for-equality-of-regular-expressions
 */
const regexEqual = (x, y) => {
  return (
    x instanceof RegExp &&
    y instanceof RegExp &&
    x.source === y.source &&
    x.global === y.global &&
    x.ignoreCase === y.ignoreCase &&
    x.multiline === y.multiline
  );
};

module.exports = {
  webpack: config => {
    const oneOf = config.module.rules.find(
      rule => typeof rule.oneOf === 'object'
    );

    if (oneOf) {
      const moduleSassRule = oneOf.oneOf.find(rule =>
        regexEqual(rule.test, /\.module\.(scss|sass)$/)
      );

      if (moduleSassRule) {
        const cssLoader = moduleSassRule.use.find(({ loader }) =>
          loader.includes('css-loader')
        );
        if (cssLoader) {
          // Use the default CSS modules mode. Next.js use 'pure'. Not sure of all implications
          cssLoader.options.modules.mode = 'local';
        }
      }
    }

    return config;
  },
};

I’m not entirely sure of all the implications that this change might have…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Export Sass Variables into Javascript - Stack Overflow
Personally, I like to import my SCSS variables, and then, export them ... _export.scss is only utilised in JS files, and is not...
Read more >
Sass: @use
The simplest @use rule is written @use "<url>" , which loads the module at the given URL. Any styles loaded this way will...
Read more >
Features In-Depth | Less.js
Selector Interpolation with Extend. Extend is not able to match selectors with variables. If selector contains variable, extend will ignore it. However, extend ......
Read more >
sass-loader - webpack - JS.ORG
This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use. Note. We highly recommend...
Read more >
Global vs. Local Styling In Next.js - Smashing Magazine
Not only does Next.js support doing so with no additional setup; ... SASS in a project if I find I need other SASS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found