4

Consider this code:

// File A1.scss
.abc {
  // styles for abc
}

.def {
  // styles for def
}


// File B2.scss
.xyz {
  // styles for xyz
}

Is it possible to import and use .abc from A1.scss into B2.scss without having the class .def in B2.css (i.e. the output of B2.scss)? A1.scss is large in real project, so can't afford to have B2.scss be bloated with other styles. I am using this in a React app with Webpack.

darKnight
  • 5,651
  • 13
  • 47
  • 87

1 Answers1

0

Try using one of the relatively new sass rules - @use or @forward. Both rules only load the given .sass / .scss file once, even if used multiply in your project. Those two are part of SASS Modules. Both @use and @forward partially resolves one of the oldest css problems - namespaces and private variables.

// get ui/forms.scss file
@use 'ui/forms' as forms;

.footer-button{
  // use this specific mixin from ui/forms.scss
  @include forms.form-button;
}

More examples:

https://css-tricks.com/introducing-sass-modules/

https://sass-lang.com/documentation/at-rules/use/

https://sass-lang.com/documentation/at-rules/forward/