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.

Allow modular SCSS import and avoid duplicated styles on development mode

See original GitHub issue

Is your proposal related to a problem?

We’ve been trying to use a SCSS module oriented design system that provides a way to explicitly import the SCSS modules as needed when creating styles for a given component.

@import "~@some-repo/design-system/base"; /* settings, mixins, extends */
@import "~@some-repo/design-system/page"; /* utility classes and general stylings */
@import "~@some-repo/design-system/bridge/buttons"; /* button specific styles */

This is fine when generating the final build, because any duplicated css will be ignored.

But when running the project in development mode, duplicated styles are prepended to the head section of the generated html file

Describe the solution you’d like

A solution to this problem would be to add an opt-in configuration for doing something like this:

https://github.com/maoberlehner/node-sass-magic-importer/tree/master/packages/node-sass-once-importer

This would allow for the generated prepended style tags to avoid duplicated styles:

// Example:
@import 'style1.scss';
@import 'style1.scss'; /* Duplicated on a different component .scss file */
@import 'style2.scss';

Generates:

// Contents of style1.scss:
.selector1 { }
.selector2 { }

// Contents of style2.scss:
.selector3 { }
.selector4 { }

Describe alternatives you’ve considered

This: https://github.com/webpack-contrib/sass-loader/issues/145#issuecomment-173984354

And also, importing all SCSS modules that are common to more than one component style into the main .scss file and add ad-hoc scss modules as needed into this main file (not ideal as it would limit us from sharing the components with embedded styles)

Additional context

I’d love to hear of any other alternatives you might have used that wouldn’t require us to import all base files when using the design system.

I’ve considered using CSS Modules and CSS in JS, but this would limit us from making the design-system framework agnostic

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

5reactions
mylnikovDcommented, Dec 17, 2021

Hello guys Can we expect any progress on that issue? Or is there a manual way to get rid of duplicates?

1reaction
mauriciotascacommented, Jun 9, 2021

@mauriciotasca i’ve tried the dart version on a new CRA app and it doesn’t solve the issue when using “@use” or something similar. Did you find a solution for this? Our project is getting quite large and inspecting the DOM in chrome is becoming a pain.

Unfortunately not.

I usually have this type of issue when using libs that weren’t previously prepared to work with “@use” and heavily depend on “@import”.

Using partial imports works for some cases, have you tried it?

_aubmodule.scss

Sass discourages the use of “@import” and will phase it out as per this link: https://sass-lang.com/documentation/at-rules/import

Interestingly enough, they seem to have created a tool That migrates code that relies on “@import” to employ usage of the “@use” keyword instead:

https://sass-lang.com/documentation/cli/migrator

Please test it out and come back with your feedback about the tool!

Best of luck!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modular SCSS and Why You Need It
This scoping enables you to have duplicate class names between files without any conflicts.
Read more >
Duplicated styles on head and a lot of <style> elements
The behavior you're seeing is caused by ViewEncapsulation.Emulated defined for material components. First, you don't need to add styles for ...
Read more >
Introducing Sass Modules
Sass just launched a major new feature you might recognize from other languages: a module system. This is a big step forward for...
Read more >
Avoid Importing Sass StyleSheet Multiple Times in Angular
Specify commonly used stylesheets using the styles property in angular.json for your project configuration to avoid sending duplicate ...
Read more >
Strict mode - JavaScript - MDN Web Docs
For example, NaN is a non-writable global variable. In sloppy mode, assigning to NaN does nothing; the developer receives no failure feedback.
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