Closed Mooncat25 closed 1 month ago
Update: I have tried the solution of Discussion https://github.com/vitejs/vite/discussions/14454, by adding path.join(process.cwd(), 'node_modules/svelte/src/runtime/index.js')
into the rollupOptions.input
option, setting preserveEntrySignatures: 'allow-extension'
, and changing the return value of entryFileNames()
according to the chunkInfo.facadeModuleId
so that the Svelte runtime chunk is named assets/svelte.[hash].js
. Now I get consistent output in the Svelte runtime chunk. I have committed the changes on the reproduction repo.
But not really a fan of it. It relies on the internal structure of Svelte runtime. (Maybe I can/should read the main
in Svelte's package.json
?) And the more I think about it, the more I think it is a bug on Vite. Setting moduleSideEffects: 'no-treeshake'
should not remove any export.
Another update: the solution above does not work in more complicated set up. My actual project still produces Svelte runtime chunks with different exports. One exports text
and set_data
while the other one doesn't. So the journey of searching for the solution still continues.
I don't understand how this is a Vite issue? Seems like a Rollup configuration issue. moduleSideEffects: 'no-treeshake'
is not related to exports, preserveEntrySignature
is which you configured. What moduleSideEffects
does is that it tells every module how it should be analyzed. When these modules get chunked, it's a different flow with its own set of treeshaking and bundle analysis.
Anyways, something you may also hit is https://github.com/rollup/rollup/issues/4180, manualChunks
may not be the best way to guarantee consistent shared chunk code unless you're ok with potentially additional code added.
I encountered a similar issue. Have you found a good solution?
Describe the bug
Setting
moduleSideEffects
tono-treeshake
does not completely disable Rollup's tree shaking.Short Story
I have used the
output.manualChunks
option to split the code Svelte to a separated chunk, and setmoduleSideEffects
tono-treeshake
in the result of thetransform
hook to disable tree shake, expecting the code in the Svelte chunk to be consistent. Unfortunately, exports are still being removed.Long Story
I'm trying to make something like micro-frontends for Vite + Svelte + TS. But module-federation does not support Vite. @originjs/vite-plugin-federation seems to be abandoned. After figuring out the
output.manualChunks
option in Rollup, I decided to study about it. Basically I want to use it to split the common modules, e.g. Svelte, Axios, common Svelte UI components, etc., into common chunks, so that different apps can share them, i.e. code sharing.But this issue is blocking the development because the Svelte chunks from different apps are different and thus not interchangeable.
Reproduction
https://github.com/Mooncat25/Rollup-ManualChunks-Problem
Steps to reproduce
Create 2 Vite + Svelte apps, app1 and app2.
Add
"lib": ["ESNext"],
in thecompilerOptions
of bothtsconfig.node.json
.Add the following plugin in the
plugins
of bothvite.config.ts
:In the
App.svelte
of either app1 or app2, add the following script in the<script>
tag so one of them uses Svelte'sonMount
:In my reproduction repo, I also add
<div>{randomText}</div>
to ensurerandomText
is used.Build both app1 and app2 with
vite build
. Observe thesvelte
chunk in theirdist/assets/
.By diffing their contents, you can see that their only difference is the export of
onMount
.System Info
Used Package Manager
pnpm
Logs
No response
Validations