Skip to content

Dependency pre-bundling doesn't respect the order of imports in source files #19668

Open
@makedopamine

Description

@makedopamine

Describe the bug

In development mode, imports of common chunks are hoisted, which can lead to weird results if modules have side effects.

Reproduction

https://stackblitz.com/edit/vitejs-vite-emgasq4m?file=src%2Fmain.js,index.html&terminal=dev

Steps to reproduce

  1. Create the following project
    // index.html
    <!doctype html>
    <html lang="en">
       <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Vite App</title>
       </head>
       <body>
        <script type="module" src="./src/main.js"></script>
       </body>
    </html>
     // ./src/main.js
     import 'foo/js/bar.js';
     import 'foo/js/baz.js';
    
     // foo/js/bar.js
     import '../css/bar.css';
     import './baz.js';
    
      // foo/js/baz.js
     import '../css/baz.css';
    
      // foo/css/bar.css
     html {
        height: 100%;
        background-color: red;
     }
    
     // foo/css/baz.css
     html {
        height: 100%;
        background-color: green;
     }
  2. Run npm run dev

Excepted behavior:
Since baz.js is imported after bar.js, background-color: green should override background-color: red

Actual behavior:
background-color:red takes effect. Because baz.js is extracted into a common chunk and the import of this chunk is hoisted to the top.

System Info

Vite: 6.2.2

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions