vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.92k stars 6.23k forks source link

Vite ignores 'no-treeshake' from plugins #13544

Closed kwangure closed 1 year ago

kwangure commented 1 year ago

Describe the bug

I have created a simple plugin that prevents Vite from treeshaking a JavaScript file.

{
    enforce: 'pre',
    name: 'plugin-notreeshake',
    resolveId(id) {
        return { id, moduleSideEffects: 'no-treeshake' };
    }
}

Given the input file:

function notPure() {
    UnknownGlobalCouldHaveSideEffect.test('value');
}

console.log('test');

I expect an indentical file in the output, but Vite ignores no-treeshake and I get the following output file, keeping only code it thinks has side effects.

console.log('test');

When I use the same plugin with Rollup directly, I get the correct non-treeshaken output as desired. It's an issue with Vite.

Reproduction

https://github.com/kwangure/no-treeshake-repro

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
    Memory: 526.91 MB / 1.79 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.3.0 - ~/.nvm/versions/node/v20.3.0/bin/node
    npm: 9.6.7 - ~/.nvm/versions/node/v20.3.0/bin/npm
  npmPackages:
    vite: ^4.3.9 => 4.3.9

Used Package Manager

npm

Logs

No response

Validations

sapphi-red commented 1 year ago

In this case, the functions are removed by the minify step that uses esbuild (repl). You can turn off this behavior with build.minify: false.