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.

Vite generates multiple copies of the same stylesheet

See original GitHub issue

Describe the bug

I am using the following vite config option to make global styles available in React components without requirement of usage of “@import '…/…/syles/main” in each component file:

css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @import "./src/styles/main";
        `
      }
    }
  }

it comes from https://github.com/vitejs/vite/issues/832

My main.scss looks like:

@import "./node_modules/normalize.css/normalize";

@import 'fonts';
@import 'colors';
@import 'breakpoints';
@import 'animations';
@import 'layers';

where fonts, colors, breakpoints, animations, layers include global variables.

It works fine in part I can use my variables inside component without importing the main SCSS file, but such approach causes an issue that I have multiple copies of the same stylesheet in a head section of page ( see the screenshot: https://imgur.com/a/k6hGnbP )

Reproduction

  1. Create empty app (Vite, React, TSX)
  2. Create a global main.scss file
  3. Import partials _colors.scss and _fonts.scss to main.scss using @import
  4. Create vite.config.js with option similar to described in a bug description section
  5. Create at least two different components and add it to your App
  6. Add some variables to colors.scss and fonts.scss
  7. Use variables from colors.scss and fonts.scss in your two components from step 5 using build-in CSS modules support ([component].module.scss)
  8. Run your app and look at head section

Expected result: There is only one global stylesheet. Actual result: There are multiple copies of global stylesheet, where number of copies equals number of unique components,

UPDATE: I’ve created a simplified app which one reproduces the issue: https://github.com/tastytea-github/vite-global-import-issue-reproduction

  1. Clone repo
  2. Install deps with npm i
  3. Run app with npm run dev
  4. Look at head section of the rendered page in devtools

Expected state: styles from main.scss are added globally only once. Actual state: styles from main.scss are included to each unique component.

System Info

No

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:12
  • Comments:28 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
tastytea-githubcommented, Oct 14, 2021

@patak-js Hello sir. Could you be so kind to pay attention to this issue?

3reactions
sodateacommented, Aug 9, 2021

feat: css means the issue is related to the CSS bundling feature of Vite. It’s not indicating that it is a feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared Options | Vite
If you have duplicated copies of the same dependency in your app (likely due to hoisting or linked packages in monorepos), use this...
Read more >
Features | Vite
However, Vite provides many enhancements over native ESM imports to support various features that are typically seen in bundler-based setups.
Read more >
Build Options - Vite
This is the same as options that can be exported from a Rollup config file and will be merged with Vite's internal Rollup...
Read more >
Building for Production - Vite
Running vite build with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats: es and...
Read more >
Configuring Vite
More advanced custom resolution can be achieved through plugins. resolve.dedupe #. Type: string[]. If you have duplicated copies of the same dependency in...
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