vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.28k stars 6.05k forks source link

incorrect CSS order after build when manualChunk is used #6375

Open mutongwu opened 2 years ago

mutongwu commented 2 years ago

Describe the bug

backgound:

The project needs a third party lib css ( libs/pink.css), so I include the file in the main.js, and make it as a manual chunk with vite.config.js:

// third party lib css
import './libs/pink.css';

import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('libs/pink.css')) {
            return 'vendor-pink';
          }
        },
      },
    },
  },

then I need to override some selector , so I add selector in App.vue ,

// app.vue
<style>
body {
  background: lightblue;
}
</style>

The Problem:

In dev mode, npm run dev and the body background color is "lightbue". And this is what I want. But after I run build command (npm run build , npm run preview), the page background color is "pink". Because in vendor-pink.css is injected behide index.css:

// file: dist/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
    <script type="module" crossorigin src="/assets/index.5988f3cb.js"></script>
    <link rel="stylesheet" href="/assets/index.94bf859f.css">
    <link rel="stylesheet" href="/assets/vendor-pink.52a267b7.css">
  </head>
  <body>
    <div id="app"></div>

  </body>
</html>

How

As I known, pink.css is treated as a "vendor" asserts, then it should be injected before index.css. how can i achive this ? like

    <script type="module" crossorigin src="/assets/index.5988f3cb.js"></script>
    <link rel="stylesheet" href="/assets/vendor-pink.52a267b7.css">
    <link rel="stylesheet" href="/assets/index.94bf859f.css">

Reproduction

https://stackblitz.com/edit/vitejs-vite-21apjj?file=dist%2Findex.html&terminal=dev

System Info

❯ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers
success Install finished in 1.522s

  System:
    OS: Linux undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: Unknown - /bin/jsh
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    @vitejs/plugin-vue: ^2.0.0 => 2.0.1 
    vite: ^2.7.2 => 2.7.10

Used Package Manager

npm

Logs

No response

Validations

akhilmhdh commented 2 years ago

Hey there. I am also facing same issues. Is there a way to solve this?

wangfengyuan commented 2 years ago

also want to known how to solve this

hanaTsuk1 commented 2 years ago

I have the same problem, but when I upgrade vite to '2.9.0-beta.2' and modify 'vite.config.ts', it will return to normal

If you still return 'vendor-pink', it will not take effect

https://stackblitz.com/edit/vitejs-vite-1pgemr?file=vite.config.js&terminal=dev

consegrado commented 2 years ago

+1

mrenwu commented 4 months ago

same issues

BranceLee commented 3 months ago

There are two lines should be took cared:

  1. build: { cssCodeSplit: true, }

  2. imported external css should be place before your imported main component.
  3. manual chunks the imported the external css or scss into a verder.css like: manualChunks(id) { if (id.includes('node_modules/xxx/src') && id.includes('.scss')) { return 'vendor'; } } which will be linked the index.html and check the external and your internal css order should be right.