Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VITE] Libraries shipping vanilla extract css loaded out of order when using vite #1376

Open
2 tasks done
otaviosoares opened this issue Apr 5, 2024 · 2 comments
Open
2 tasks done
Labels
has-workaround This issue has a workaround vite Issue related to vite

Comments

@otaviosoares
Copy link

Describe the bug

It happens when using vite and importing VE css from a library. The css is loaded in a non deterministic way, causing reset css to have a higher priority.

I created a small example using braid-design-system. Apparently, vite's dependency optimizer is bundling the styles in the wrong order, but I'm not sure.

Screenshot 2024-04-05 at 10 59 25 AM

Screenshot 2024-04-05 at 10 59 37 AM

DISCLAIMER: This only happens in dev mode. During build the css file that is extract has the correct order.

WORKAROUND I'm using css layers to avoid this issue but I'm intrigued and would love to know what's causing this behaviour. Any help is appreciated.

Reproduction

https://stackblitz.com/edit/vitejs-vite-qfbgzz

System Info

System:
    OS: macOS 14.2.1
    CPU: (16) arm64 Apple M3 Max
    Memory: 3.28 GB / 128.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 4.0.2 - ~/.yarn/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 123.0.6312.107
    Edge: 123.0.2420.81
    Safari: 17.2.1
  npmPackages:
    vite: ^5.1.4 => 5.1.4

Used Package Manager

yarn

Logs

No response

Validations

@rururux
Copy link

rururux commented May 8, 2024

maybe a duplicate of #725

@askoufis
Copy link
Contributor

askoufis commented Dec 17, 2024

You're correct that vite's dependency optimization is to blame here. This is our current workaround for this issue:

// vite.config.ts
import { createRequire } from 'node:module';
import { dirname } from 'node:path';
import { cssFileFilter } from '@vanilla-extract/integration';
import type { Plugin } from 'esbuild';

const require = createRequire(import.meta.url);

const fixViteVanillaExtractDepScanPlugin = (): Plugin => ({
  name: 'fix-vite-vanilla-extract-dep-scan',
  setup(build) {
    build.onResolve(
      { filter: cssFileFilter },
      ({ importer, path: pathName }) => ({
        path: require.resolve(pathName, { paths: [dirname(importer)] }),
        external: true,
      })
    );
  },
});

export default {
  optimizeDeps: {
    esbuildOptions: {
      plugins: [fixViteVanillaExtractDepScanPlugin()],
    },
  },
  // The rest of your vite config...
}

@askoufis askoufis added vite Issue related to vite has-workaround This issue has a workaround and removed pending triage labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-workaround This issue has a workaround vite Issue related to vite
Projects
None yet
Development

No branches or pull requests

3 participants