kit icon indicating copy to clipboard operation
kit copied to clipboard

Vite plugins run twice per build

Open v423 opened this issue 1 year ago • 5 comments

Describe the bug

I hope to package the files in the build folder into a zip file after the project is built. Currently, the build function triggers twice (client & server), resulting in the generation of two zip files. How can I trigger the compression command after the entire Svelte project is built?

Reproduction

svelte

// +layout.js
export const ssr = false;

vite config

	plugins: [
		sveltekit(),
		zipPack({  
			inDir: 'build',
			outDir: 'build-zip',
			outFileName
		})
	],

Logs

output  "Creating zip archive" 2 times

✓ 808 modules transformed.
.svelte-kit/output/client/_app/version.json                                  0.03 kB │ gzip:  0.05 kB
.svelte-kit/output/client/.vite/manifest.json                                5.08 kB │ gzip:  0.77 kB
.svelte-kit/output/client/_app/immutable/assets/3.BI_tCB0a.css               0.63 kB │ gzip:  0.28 kB
.svelte-kit/output/client/_app/immutable/assets/5.7-mY4b00.css               0.92 kB │ gzip:  0.40 kB
.svelte-kit/output/client/_app/immutable/assets/0.B9W28r6b.css               7.75 kB │ gzip:  2.35 kB
.svelte-kit/output/client/_app/immutable/assets/compUploader.CNcAM2vh.css    7.95 kB │ gzip:  3.16 kB
.svelte-kit/output/client/_app/immutable/nodes/2.DzfIOfLj.js                 0.19 kB │ gzip:  0.17 kB
.svelte-kit/output/client/_app/immutable/chunks/stores.DVSKkPny.js           0.24 kB │ gzip:  0.17 kB
.svelte-kit/output/client/_app/immutable/nodes/1.DP5H3zni.js                 0.78 kB │ gzip:  0.49 kB
.svelte-kit/output/client/_app/immutable/nodes/4.CY5mipM5.js                 1.37 kB │ gzip:  0.77 kB
.svelte-kit/output/client/_app/immutable/chunks/Center.BKocSWWJ.js           2.00 kB │ gzip:  1.11 kB
.svelte-kit/output/client/_app/immutable/chunks/singletons.9L1s8hAq.js       2.98 kB │ gzip:  1.52 kB
.svelte-kit/output/client/_app/immutable/chunks/index.kbufqIef.js            4.53 kB │ gzip:  2.30 kB
.svelte-kit/output/client/_app/immutable/entry/app.D72VJaQq.js               6.64 kB │ gzip:  2.63 kB
.svelte-kit/output/client/_app/immutable/chunks/scheduler.BHTdNEtY.js        9.20 kB │ gzip:  3.60 kB
.svelte-kit/output/client/_app/immutable/nodes/0.eCldHc39.js                14.70 kB │ gzip:  5.18 kB
.svelte-kit/output/client/_app/immutable/nodes/3.ClASAOnp.js                24.84 kB │ gzip:  6.58 kB
.svelte-kit/output/client/_app/immutable/entry/start.D2KNPmGn.js            28.00 kB │ gzip: 11.21 kB
.svelte-kit/output/client/_app/immutable/chunks/Text.Y5nHkiLP.js            34.27 kB │ gzip: 10.24 kB
.svelte-kit/output/client/_app/immutable/chunks/Box.NWas6taL.js             41.21 kB │ gzip: 15.65 kB
.svelte-kit/output/client/_app/immutable/nodes/5.CWCGiZLi.js                43.30 kB │ gzip: 11.32 kB
.svelte-kit/output/client/_app/immutable/chunks/compUploader.BjxlQUIS.js   109.95 kB │ gzip: 34.76 kB
Zip packing - "build" folder :
  - Preparing files.
✓ built in 13.33s
  - Creating zip archive.
  - Done.
.svelte-kit/output/server/_app/immutable/assets/_page.BI_tCB0a.css           0.63 kB
.svelte-kit/output/server/_app/immutable/assets/_page.7-mY4b00.css           0.92 kB
.svelte-kit/output/server/_app/immutable/assets/_layout.B9W28r6b.css         7.75 kB
.svelte-kit/output/server/_app/immutable/assets/compUploader.BGyWnUn5.css    7.95 kB
.svelte-kit/output/server/entries/pages/_layout.js                           0.04 kB
.svelte-kit/output/server/entries/pages/_page.svelte.js                      0.19 kB
.svelte-kit/output/server/internal.js                                        0.22 kB
.svelte-kit/output/server/entries/fallbacks/error.svelte.js                  0.47 kB
.svelte-kit/output/server/chunks/stores.js                                   0.52 kB
.svelte-kit/output/server/entries/pages/result/_page.svelte.js               0.84 kB
.svelte-kit/output/server/chunks/index.js                                    1.33 kB
.svelte-kit/output/server/chunks/Center.js                                   2.04 kB
.svelte-kit/output/server/chunks/internal.js                                 5.82 kB
.svelte-kit/output/server/chunks/ssr.js                                      9.37 kB
.svelte-kit/output/server/entries/pages/_layout.svelte.js                   13.27 kB
.svelte-kit/output/server/entries/pages/house/_page.svelte.js               15.01 kB
.svelte-kit/output/server/entries/pages/user/_page.svelte.js                24.04 kB
.svelte-kit/output/server/chunks/Text.js                                    34.95 kB
.svelte-kit/output/server/chunks/Box.js                                     63.26 kB
.svelte-kit/output/server/chunks/compUploader.js                            69.18 kB
.svelte-kit/output/server/index.js                                         114.41 kB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-static
  Wrote site to "build/build"
  ✔ done
Zip packing - "build" folder :
  - Preparing files.
✓ built in 28.50s
  - Creating zip archive.
  - Done.


### System Info

```Shell
System:
    OS: Windows 10 10.0.19045
    CPU: (6) x64 Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
    Memory: 42.30 GB / 63.93 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Local\pnpm\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.5.1 - ~\AppData\Local\pnpm\pnpm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.128), ChromiumDev (123.0.2420.6)
    Internet Explorer: 11.0.19041.3636
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.0.0
    @sveltejs/adapter-static: ^3.0.1 => 3.0.1
    @sveltejs/kit: ^2.0.0 => 2.0.0
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.0
    svelte: ^4.2.7 => 4.2.7
    vite: ^5.0.3 => 5.0.3

Severity

annoyance

Additional Information

No response

v423 avatar Feb 29 '24 05:02 v423

This happens because we run Vite twice per build: once to build the server bundle and once for the client bundle. The server bundle is needed to prerender pages (and for SSR in other adapters) while the client bundle is needed for CSR.

I think we’ll need to find a way to run plugins that hook into the build either only during the server build or client build?

teemingc avatar Feb 29 '24 14:02 teemingc

This happens because we run Vite twice per build: once to build the server bundle and once for the client bundle. The server bundle is needed to prerender pages (and for SSR in other adapters) while the client bundle is needed for CSR.

I think we’ll need to find a way to run plugins that hook into the build either only during the server build or client build?

I would like to obtain the progress of the project build, and trigger the operation of compressing the files upon completion. For example, if it's "step 2/2" during server build, then I know it's the final step and I can compress it into a zip file. Pure SSR projects may not trigger client bundling, which would be indicated as "step 1/1".

v423 avatar Mar 01 '24 01:03 v423

This happens because we run Vite twice per build: once to build the server bundle and once for the client bundle. The server bundle is needed to prerender pages (and for SSR in other adapters) while the client bundle is needed for CSR. I think we’ll need to find a way to run plugins that hook into the build either only during the server build or client build?

I would like to obtain the progress of the project build, and trigger the operation of compressing the files upon completion. For example, if it's "step 2/2" during server build, then I know it's the final step and I can compress it into a zip file. Pure SSR projects may not trigger client bundling, which would be indicated as "step 1/1".

Agreed. I’m not very familiar with the Vite plugin API but is there a way to differentiate plugins that need to run for SSR builds, client builds, or both?

teemingc avatar Mar 01 '24 02:03 teemingc

Agreed. I’m not very familiar with the Vite plugin API but is there a way to differentiate plugins that need to run for SSR builds, client builds, or both?

Yes, it does! https://vitejs.dev/guide/ssr.html#ssr-specific-plugin-logic

knd775 avatar Mar 23 '24 02:03 knd775

The upcoming Vite Environment API should improve things on that front https://github.com/vitejs/vite/discussions/16358

mquandalle avatar Jun 07 '24 10:06 mquandalle