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
It seems that buildEnd hook is not trigger anymore in vite 5.0.10 with a custom plugin. I run npm run dev then ctl-c to exit the process and buildEnd isn't trigger.
I tried with vite 4.5.1 and it works.
For the reproduction, I created a small plugin to test it on stackblitz ! It should console.log buildStart then buildEnd when the process is exited.
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
I took a look, and it seemed to work in Vite 4 because we took control of Ctrl+C via setRawMode, which was part of the breaking change related to the CLI shortcuts. Any versions other than v4 didn't work, as if you're relying on this behaviour, it's similar to listening to the Node.js process-exit-hook, and it's known to not always be reliable.
We're currently using SIGTERM to listen for process exit, but that only works in controlled environments such as child processes. I tested, and only SIGINT seems to reliably get triggered, but it was removed in a04db16.
SIGINT is generally very tricky to work with unfortunately. You need to call process.exit() manually if you add a handler. But if you have multiple handlers (maybe some framework adds theirs too), then calling process.exit() will halt the subsequent added handlers.
This SO answer has some more details. If we do something similar, it might also help lessen the impact of #9470
the custom plugin to end the build correctly from this stackoverflow thread helped me to finally use vite 5 in my deployment workflow...
// file vite-plugin-close.tsexportdefaultfunctionClosePlugin(){return{name: 'ClosePlugin',// required, will show up in warnings and errors// use this to catch errors when buildingbuildEnd(error){if(error){console.error('Error bundling')console.error(error)process.exit(1)}else{console.log('Build ended')}},// use this to catch the end of a build without errorscloseBundle(id){console.log('Bundle closed')process.exit(0)},}}
// file vite.config.tsimportClosePluginfrom'./vite-plugin-close.ts'// ...// place the plugin in the *vite* plugins section// NOT the rollupOptions.plugins sectionplugins: [ClosePlugin()],
Activity
bolt-new-by-stackblitz commentedon Dec 23, 2023
userquin commentedon Dec 23, 2023
why
apply: 'serve'? It shouldn't be called, if Vite 4 callingbuildEndit should be a bug (no makes sense havingbuildEndin a dev plugin).MaxRenglet commentedon Dec 24, 2023
Hi @userquin, yeah that what i thought, buildEnd is trigger in build mode but shouldn't be trigger in dev mode.. but it does in Vite 4.5.1.
Do you know if there is a similar hook working in dev ?
userquin commentedon Dec 24, 2023
No, but you can try adding build hooks (https://rollupjs.org/plugin-development/ and https://vitejs.dev/guide/api-plugin#transformindexhtml)... (apply can be also a function)
MaxRenglet commentedon Dec 24, 2023
Thanks @userquin, I will figure this out.
Should I rename the issue about the buildEnd triggered in dev in Vite 4.5.1 ?
bluwy commentedon Dec 26, 2023
buildEndshould be called in dev too, it's documented in https://vitejs.dev/guide/api-plugin.html#universal-hooks. Seems like a bug it's not being called now.bluwy commentedon Dec 26, 2023
I took a look, and it seemed to work in Vite 4 because we took control of Ctrl+C via
setRawMode, which was part of the breaking change related to the CLI shortcuts. Any versions other than v4 didn't work, as if you're relying on this behaviour, it's similar to listening to the Node.js process-exit-hook, and it's known to not always be reliable.We're currently using SIGTERM to listen for process exit, but that only works in controlled environments such as child processes. I tested, and only SIGINT seems to reliably get triggered, but it was removed in a04db16.
SIGINT is generally very tricky to work with unfortunately. You need to call
process.exit()manually if you add a handler. But if you have multiple handlers (maybe some framework adds theirs too), then callingprocess.exit()will halt the subsequent added handlers.This SO answer has some more details. If we do something similar, it might also help lessen the impact of #9470
userquin commentedon Dec 26, 2023
@bluwy the plugin shouldn't be called in
build, it hasapply: 'serve', check Conditional ApplicationEDIT: I think I have misunderstood the issue
bluwy commentedon Dec 26, 2023
I don't see the plugin running when running
npm run buildin the repro 🤔MaxRenglet commentedon Dec 27, 2023
Hi, @bluwy thanks for the heads up and explaination !
I will wait to see if a fix is push to migrate to Vite 5 !
Thanks ;)
sapphi-red commentedon Sep 19, 2024
Maybe we can try
signal-exit. It's used by rollup and might work well.TimKraemer commentedon Sep 26, 2024
the custom plugin to end the build correctly from this stackoverflow thread helped me to finally use vite 5 in my deployment workflow...