import { ITaskConfig , Context } from 'build-scripts' ; import chalk from 'chalk' ; import { ViteDevServer , createServer } from 'vite' ; import { wp2vite } from './wp2vite' ; type StartResult = void | ITaskConfig [ ] | ViteDevServer ; export async function viteStart ( context ) { const { applyHook , command , commandArgs } = context ; const configArr = context . getWebpackConfig ( ) ; const devConfig = configArr . length > 0 ? wp2vite ( context ) : { } ; await applyHook ( `before.${ command }.load` , { args : commandArgs , webpackConfig : configArr , viteConfig : devConfig , } ) ; if ( ! configArr . length ) { const errorMsg = 'No config found.' ; await applyHook ( 'error' , { err : new Error ( errorMsg ) } ) ; return ; } await applyHook ( `before.${ command }.run` , { args : commandArgs , config : devConfig , } ) ; try { const devServer = await createServer ( devConfig ) ; await devServer . listen ( ) ; applyHook ( `after.${ command }.devServer` , { devServer , url : generateDevServerUrl ( devConfig ) , } ) ; devServer . printUrls ( ) ; return devServer ; } catch ( err ) { console . error ( 'CONFIG' , chalk . red ( 'Failed to load vite config.' ) ) ; await applyHook ( 'error' , { err } ) ; throw err ; } } function generateDevServerUrl ( devConfig ) { const { server : { port , https , host } } = devConfig ; const protocol = https ? 'https' : 'http' ; return `${ protocol }://${ resolveHostname ( host ) }:${ port }/` ; } function resolveHostname ( host ) { if ( host === '0.0.0.0' || host === '::' || host === '127.0.0.1' ) { return 'localhost' ; } return host ; }