vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.48k stars 6.08k forks source link

Build mode onSuccess flag mainly for watch mode #10779

Closed kalvenschraut closed 1 year ago

kalvenschraut commented 1 year ago

Description

I use tsup for more simpler projects that don't require all the configuration of vite (mainly vue plugins) and it has a feature I find very useful. An onSuccess flag which then allows me to run a command whenever its watch mode has successfully rebuilt. In this I would like to trigger vue-tsc and then tsc-alias to replace aliases. Currently I am having to run concurrently and each individual program in watch mode which is not ideal.

Suggested solution

Add another plugin called onBuildSuccess or something similar and use the rollup writeBundle hook.

Something like the below

import { execSync } from 'node:child_process' 
import type { Plugin } from '../plugin'  

 export function onBuildSuccessPlugin(   onSuccess: (() => Promise<void> | void) | string): Plugin { 
  return {
    name: 'vite:on-build-success',
    writeBundle() {
       if (typeof onSuccess === 'string') {                                                                                                                                            
          execSync(onSuccess)
      } else {
          return onSuccess()
      }                            
    }
  }
}                                                                                                                                                                                                                                                                                                                                                                    

Alternative

No response

Additional context

Currently in a monorepo, I have vite in build mode with -m development and -w flags bundling my "common" package for my UI code. I would like to trigger the types code to be rebuilt anytime vite's watch triggers.

I half started a potential PR for this if this is something the team is looking to accept I can look at finishing it. Although not sure if any effort is being done to avoid node specific APIs like exec if vite is trying to run in deno.

Validations

bluwy commented 1 year ago

I think this should be implemented in userland like the proposed solution. I'm not sure if there's many usecase for build --watch users where this is required as a first-class API. But happy to reconsider too if there's more request for this.