Open LorisSigrist opened 3 months ago
It worked with this example. Would you create a reproduction? https://stackblitz.com/edit/vitejs-vite-rapg5b?file=vite.config.js,main.js&terminal=dev
Of course, here is a minimal repo: https://github.com/LorisSigrist/vite-17898-reproduction
Ah, I found that the tree-shake works with console.log(m['new_year_' + __YEAR__]())
but not with console.log(m[`new_year_${__YEAR__}`]())
.
It seems it's because esbuild replaces 'new_year_' + __YEAR__
with "new_year_2024"
and 'new_year_' + __YEAR__
with new_year_${"2024"}
. (When minify is not enabled)
Description
I've been trying to get treeshaking to work with compile-time computed keys. Consider the following:
In this example, the call to
m['hello_' + __defined_value__]
could (should) be reduced tom['hello_world']
before treeshaking. That way only thehello_world
export from./my-module.js
is used and everything else is treeshaken.Currently all exports from
./my-module.js
would be included in the build-output.Suggested solution
AFAIK Rollup currently doesn't do this kind of transform, however, EsBuild does during it's minification step. It would output the following code
The issue is timing.
This transform would need to happen after the
vite:define
plugin, but before the build plugins. Currently EsBuild's minification only runs after the build has already completed.This results in a weird output where the key is pre-computed in the output, but no treeshaking has taken place:
Vite could achieve the desired optimization by running esbuild on
*.{js|ts|jsx|tsx}
files before running rollup. This wouldn't need to be full-blown minification, just the constant-folding step.Alternatively this optimization could be added to rollup itself.
Alternative
Users could manually provide a plugin that runs EsBuild minification after Vite's built-in plugins but before the build plugins. I believe
enforce: undefined
gives that behavior.Additional context
I work on the Paraglide i18n library, which could benefit greatly from this. See: https://github.com/opral/inlang-paraglide-js/issues/164
Validations