Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass: module loop when using @use and @forward

In my style.scss I am loading a scss file by using:

@use "bootstrap/functions";

Somewhat later in the style.scss I load another scss file called _variables.scss. In this file I also want to use the functions file, for example by doing:

$link-color:                functions.theme-color("primary") !default;
@include functions.assert-ascending($grid-breakpoints, "$grid-breakpoints");

The problem is that I am getting the following error:

SassError: Module loop: this module is already being loaded.
   ┌──> src/assets/scss/bootstrap/_variables.scss
4  │ @forward "functions";
   │ ^^^^^^^^^^^^^^^^^^^^ new load
   ╵
   ┌──> src/assets/scss/style.scss
22 │ @use "bootstrap/functions";
   │ ━━━━━━━━━━━━━━━━━━━━━━━━━━ original load
   ╵

I tried loading it with @use and @forward, I also tried not loading it to see if it can find it in the current context, but nothing works. What am I doing wrong?

I am using Webpack with the sass-loader that uses dart-sass.

like image 464
Bart Bergmans Avatar asked Nov 08 '20 18:11

Bart Bergmans


People also ask

What is @each in Sass?

The @each rule makes it easy to emit styles or evaluate code for each element of a list or each pair in a map. It's great for repetitive styles that only have a few variations between them. It's usually written @each <variable> in <expression> { ... } , where the expression returns a list.

Can I use SCSS in CSS?

SCSS syntax is CSS compatible, so you just have to rename your . css file to . scss et voilà! Your first SCSS file has been created, just like that.

What is the use of the @import function in Sass?

The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.

What does @forward do in SCSS?

The @forward rule loads a Sass stylesheet and makes its mixins, functions, and variables available when your stylesheet is loaded with the @use rule. It makes it possible to organize Sass libraries across many files, while allowing their users to load a single entrypoint file. The rule is written @forward "<url>" .


2 Answers

I'm currently having the same issue, due to @import being depreciated in October 2022.

This is because you're effectively loading bootstrap/functions into your style.scss twice, once as itself, and once within _variables.scss.

Because @use isn't global, like @import is, it seems to automatically reject any duplications, throwing an error rather than ignoring the duplicate.

like image 159
Abi Harrison Avatar answered Oct 12 '22 22:10

Abi Harrison


I think the main reason you're having this issue is that you're trying to load two files that depend on each other; _functions.scss & _variables.scss and this just isn't possible. To load a file, it must be fully executed and this isn't possible when both are depending on each other. There can only be one dependent between two files.

A similar issue was raised on the sass GitHub repo by jacobScripts.

like image 2
Eti Avatar answered Oct 12 '22 22:10

Eti