You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this example, the call to m['hello_' + __defined_value__] could (should) be reduced to m['hello_world'] before treeshaking. That way only the hello_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
import*asmfrom"./my-module.js"constthing="world";m["hello_world"]// the string get's computed and inlined
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:
// dist/assets/index-[hash].jsconste=Object.freeze(Object.defineProperty({__proto__: null,hello_world: "Hello World",extra: "I should have been treeshaken",},Symbol.toStringTag,{value: "Module"}));console.log(e.hello_world());// correctly computed & inlined
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.
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: opral/inlang-paraglide-js#164
Validations
The text was updated successfully, but these errors were encountered: