2026-02-16 13:45:45 +00:00
import { s as _ _toESM } from "./chunks/chunk.js" ;
import { N as VERSION , R as require _picocolors , n as createLogger } from "./chunks/logger.js" ;
import fs from "node:fs" ;
import path from "node:path" ;
import { performance } from "node:perf_hooks" ;
import { EventEmitter } from "events" ;
2024-01-05 12:14:38 +00:00
2026-02-16 13:45:45 +00:00
//#region ../../node_modules/.pnpm/cac@6.7.14/node_modules/cac/dist/index.mjs
2024-01-05 12:14:38 +00:00
function toArr ( any ) {
return any == null ? [ ] : Array . isArray ( any ) ? any : [ any ] ;
}
function toVal ( out , key , val , opts ) {
2026-02-16 13:45:45 +00:00
var x , old = out [ key ] , nxt = ! ! ~ opts . string . indexOf ( key ) ? val == null || val === true ? "" : String ( val ) : typeof val === "boolean" ? val : ! ! ~ opts . boolean . indexOf ( key ) ? val === "false" ? false : val === "true" || ( out . _ . push ( ( x = + val , x * 0 === 0 ) ? x : val ) , ! ! val ) : ( x = + val , x * 0 === 0 ) ? x : val ;
out [ key ] = old == null ? nxt : Array . isArray ( old ) ? old . concat ( nxt ) : [ old , nxt ] ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
function mri2 ( args , opts ) {
2024-01-05 12:14:38 +00:00
args = args || [ ] ;
opts = opts || { } ;
2026-02-16 13:45:45 +00:00
var k , arr , arg , name , val , out = { _ : [ ] } ;
var i = 0 , j = 0 , idx = 0 , len = args . length ;
2024-01-05 12:14:38 +00:00
const alibi = opts . alias !== void 0 ;
const strict = opts . unknown !== void 0 ;
const defaults = opts . default !== void 0 ;
opts . alias = opts . alias || { } ;
opts . string = toArr ( opts . string ) ;
opts . boolean = toArr ( opts . boolean ) ;
2026-02-16 13:45:45 +00:00
if ( alibi ) for ( k in opts . alias ) {
arr = opts . alias [ k ] = toArr ( opts . alias [ k ] ) ;
for ( i = 0 ; i < arr . length ; i ++ ) ( opts . alias [ arr [ i ] ] = arr . concat ( k ) ) . splice ( i , 1 ) ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
for ( i = opts . boolean . length ; i -- > 0 ; ) {
2024-01-05 12:14:38 +00:00
arr = opts . alias [ opts . boolean [ i ] ] || [ ] ;
2026-02-16 13:45:45 +00:00
for ( j = arr . length ; j -- > 0 ; ) opts . boolean . push ( arr [ j ] ) ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
for ( i = opts . string . length ; i -- > 0 ; ) {
2024-01-05 12:14:38 +00:00
arr = opts . alias [ opts . string [ i ] ] || [ ] ;
2026-02-16 13:45:45 +00:00
for ( j = arr . length ; j -- > 0 ; ) opts . string . push ( arr [ j ] ) ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
if ( defaults ) for ( k in opts . default ) {
name = typeof opts . default [ k ] ;
arr = opts . alias [ k ] = opts . alias [ k ] || [ ] ;
if ( opts [ name ] !== void 0 ) {
opts [ name ] . push ( k ) ;
for ( i = 0 ; i < arr . length ; i ++ ) opts [ name ] . push ( arr [ i ] ) ;
2024-01-05 12:14:38 +00:00
}
}
const keys = strict ? Object . keys ( opts . alias ) : [ ] ;
2026-02-16 13:45:45 +00:00
for ( i = 0 ; i < len ; i ++ ) {
2024-01-05 12:14:38 +00:00
arg = args [ i ] ;
2026-02-16 13:45:45 +00:00
if ( arg === "--" ) {
2024-01-05 12:14:38 +00:00
out . _ = out . _ . concat ( args . slice ( ++ i ) ) ;
break ;
}
2026-02-16 13:45:45 +00:00
for ( j = 0 ; j < arg . length ; j ++ ) if ( arg . charCodeAt ( j ) !== 45 ) break ;
if ( j === 0 ) out . _ . push ( arg ) ;
else if ( arg . substring ( j , j + 3 ) === "no-" ) {
2024-01-05 12:14:38 +00:00
name = arg . substring ( j + 3 ) ;
2026-02-16 13:45:45 +00:00
if ( strict && ! ~ keys . indexOf ( name ) ) return opts . unknown ( arg ) ;
2024-01-05 12:14:38 +00:00
out [ name ] = false ;
} else {
2026-02-16 13:45:45 +00:00
for ( idx = j + 1 ; idx < arg . length ; idx ++ ) if ( arg . charCodeAt ( idx ) === 61 ) break ;
2024-01-05 12:14:38 +00:00
name = arg . substring ( j , idx ) ;
2026-02-16 13:45:45 +00:00
val = arg . substring ( ++ idx ) || i + 1 === len || ( "" + args [ i + 1 ] ) . charCodeAt ( 0 ) === 45 || args [ ++ i ] ;
arr = j === 2 ? [ name ] : name ;
for ( idx = 0 ; idx < arr . length ; idx ++ ) {
2024-01-05 12:14:38 +00:00
name = arr [ idx ] ;
2026-02-16 13:45:45 +00:00
if ( strict && ! ~ keys . indexOf ( name ) ) return opts . unknown ( "-" . repeat ( j ) + name ) ;
toVal ( out , name , idx + 1 < arr . length || val , opts ) ;
2024-01-05 12:14:38 +00:00
}
}
}
if ( defaults ) {
2026-02-16 13:45:45 +00:00
for ( k in opts . default ) if ( out [ k ] === void 0 ) out [ k ] = opts . default [ k ] ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
if ( alibi ) for ( k in out ) {
arr = opts . alias [ k ] || [ ] ;
while ( arr . length > 0 ) out [ arr . shift ( ) ] = out [ k ] ;
2024-01-05 12:14:38 +00:00
}
return out ;
}
const removeBrackets = ( v ) => v . replace ( /[<[].+/ , "" ) . trim ( ) ;
const findAllBrackets = ( v ) => {
2026-02-16 13:45:45 +00:00
const ANGLED _BRACKET _RE _GLOBAL = /<([^>]+)>/g ;
const SQUARE _BRACKET _RE _GLOBAL = /\[([^\]]+)\]/g ;
const res = [ ] ;
const parse = ( match ) => {
let variadic = false ;
let value = match [ 1 ] ;
if ( value . startsWith ( "..." ) ) {
value = value . slice ( 3 ) ;
variadic = true ;
}
return {
required : match [ 0 ] . startsWith ( "<" ) ,
value ,
variadic
} ;
} ;
let angledMatch ;
while ( angledMatch = ANGLED _BRACKET _RE _GLOBAL . exec ( v ) ) res . push ( parse ( angledMatch ) ) ;
let squareMatch ;
while ( squareMatch = SQUARE _BRACKET _RE _GLOBAL . exec ( v ) ) res . push ( parse ( squareMatch ) ) ;
return res ;
2024-01-05 12:14:38 +00:00
} ;
const getMriOptions = ( options ) => {
2026-02-16 13:45:45 +00:00
const result = {
alias : { } ,
boolean : [ ]
} ;
for ( const [ index , option ] of options . entries ( ) ) {
if ( option . names . length > 1 ) result . alias [ option . names [ 0 ] ] = option . names . slice ( 1 ) ;
if ( option . isBoolean ) if ( option . negated ) {
if ( ! options . some ( ( o , i ) => {
return i !== index && o . names . some ( ( name ) => option . names . includes ( name ) ) && typeof o . required === "boolean" ;
} ) ) result . boolean . push ( option . names [ 0 ] ) ;
} else result . boolean . push ( option . names [ 0 ] ) ;
}
return result ;
2024-01-05 12:14:38 +00:00
} ;
const findLongest = ( arr ) => {
2026-02-16 13:45:45 +00:00
return arr . sort ( ( a , b ) => {
return a . length > b . length ? - 1 : 1 ;
} ) [ 0 ] ;
2024-01-05 12:14:38 +00:00
} ;
const padRight = ( str , length ) => {
2026-02-16 13:45:45 +00:00
return str . length >= length ? str : ` ${ str } ${ " " . repeat ( length - str . length ) } ` ;
2024-01-05 12:14:38 +00:00
} ;
const camelcase = ( input ) => {
2026-02-16 13:45:45 +00:00
return input . replace ( /([a-z])-([a-z])/g , ( _ , p1 , p2 ) => {
return p1 + p2 . toUpperCase ( ) ;
} ) ;
2024-01-05 12:14:38 +00:00
} ;
const setDotProp = ( obj , keys , val ) => {
2026-02-16 13:45:45 +00:00
let i = 0 ;
let length = keys . length ;
let t = obj ;
let x ;
for ( ; i < length ; ++ i ) {
x = t [ keys [ i ] ] ;
t = t [ keys [ i ] ] = i === length - 1 ? val : x != null ? x : ! ! ~ keys [ i + 1 ] . indexOf ( "." ) || ! ( + keys [ i + 1 ] > - 1 ) ? { } : [ ] ;
}
2024-01-05 12:14:38 +00:00
} ;
const setByType = ( obj , transforms ) => {
2026-02-16 13:45:45 +00:00
for ( const key of Object . keys ( transforms ) ) {
const transform = transforms [ key ] ;
if ( transform . shouldTransform ) {
obj [ key ] = Array . prototype . concat . call ( [ ] , obj [ key ] ) ;
if ( typeof transform . transformFunction === "function" ) obj [ key ] = obj [ key ] . map ( transform . transformFunction ) ;
}
}
2024-01-05 12:14:38 +00:00
} ;
const getFileName = ( input ) => {
2026-02-16 13:45:45 +00:00
const m = /([^\\\/]+)$/ . exec ( input ) ;
return m ? m [ 1 ] : "" ;
2024-01-05 12:14:38 +00:00
} ;
const camelcaseOptionName = ( name ) => {
2026-02-16 13:45:45 +00:00
return name . split ( "." ) . map ( ( v , i ) => {
return i === 0 ? camelcase ( v ) : v ;
} ) . join ( "." ) ;
} ;
var CACError = class extends Error {
constructor ( message ) {
super ( message ) ;
this . name = this . constructor . name ;
if ( typeof Error . captureStackTrace === "function" ) Error . captureStackTrace ( this , this . constructor ) ;
else this . stack = new Error ( message ) . stack ;
}
} ;
var Option = class {
constructor ( rawName , description , config ) {
this . rawName = rawName ;
this . description = description ;
this . config = Object . assign ( { } , config ) ;
rawName = rawName . replace ( /\.\*/g , "" ) ;
this . negated = false ;
this . names = removeBrackets ( rawName ) . split ( "," ) . map ( ( v ) => {
let name = v . trim ( ) . replace ( /^-{1,2}/ , "" ) ;
if ( name . startsWith ( "no-" ) ) {
this . negated = true ;
name = name . replace ( /^no-/ , "" ) ;
}
return camelcaseOptionName ( name ) ;
} ) . sort ( ( a , b ) => a . length > b . length ? 1 : - 1 ) ;
this . name = this . names [ this . names . length - 1 ] ;
if ( this . negated && this . config . default == null ) this . config . default = true ;
if ( rawName . includes ( "<" ) ) this . required = true ;
else if ( rawName . includes ( "[" ) ) this . required = false ;
else this . isBoolean = true ;
}
2024-01-05 12:14:38 +00:00
} ;
const processArgs = process . argv ;
const platformInfo = ` ${ process . platform } - ${ process . arch } node- ${ process . version } ` ;
2026-02-16 13:45:45 +00:00
var Command = class {
constructor ( rawName , description , config = { } , cli$1 ) {
this . rawName = rawName ;
this . description = description ;
this . config = config ;
this . cli = cli$1 ;
this . options = [ ] ;
this . aliasNames = [ ] ;
this . name = removeBrackets ( rawName ) ;
this . args = findAllBrackets ( rawName ) ;
this . examples = [ ] ;
}
usage ( text ) {
this . usageText = text ;
return this ;
}
allowUnknownOptions ( ) {
this . config . allowUnknownOptions = true ;
return this ;
}
ignoreOptionDefaultValue ( ) {
this . config . ignoreOptionDefaultValue = true ;
return this ;
}
version ( version , customFlags = "-v, --version" ) {
this . versionNumber = version ;
this . option ( customFlags , "Display version number" ) ;
return this ;
}
example ( example ) {
this . examples . push ( example ) ;
return this ;
}
option ( rawName , description , config ) {
const option = new Option ( rawName , description , config ) ;
this . options . push ( option ) ;
return this ;
}
alias ( name ) {
this . aliasNames . push ( name ) ;
return this ;
}
action ( callback ) {
this . commandAction = callback ;
return this ;
}
isMatched ( name ) {
return this . name === name || this . aliasNames . includes ( name ) ;
}
get isDefaultCommand ( ) {
return this . name === "" || this . aliasNames . includes ( "!" ) ;
}
get isGlobalCommand ( ) {
return this instanceof GlobalCommand ;
}
hasOption ( name ) {
name = name . split ( "." ) [ 0 ] ;
return this . options . find ( ( option ) => {
return option . names . includes ( name ) ;
} ) ;
}
outputHelp ( ) {
const { name , commands } = this . cli ;
const { versionNumber , options : globalOptions , helpCallback } = this . cli . globalCommand ;
let sections = [ { body : ` ${ name } ${ versionNumber ? ` / ${ versionNumber } ` : "" } ` } ] ;
sections . push ( {
title : "Usage" ,
body : ` $ ${ name } ${ this . usageText || this . rawName } `
} ) ;
if ( ( this . isGlobalCommand || this . isDefaultCommand ) && commands . length > 0 ) {
const longestCommandName = findLongest ( commands . map ( ( command ) => command . rawName ) ) ;
sections . push ( {
title : "Commands" ,
body : commands . map ( ( command ) => {
return ` ${ padRight ( command . rawName , longestCommandName . length ) } ${ command . description } ` ;
} ) . join ( "\n" )
} ) ;
sections . push ( {
title : ` For more info, run any command with the \` --help \` flag ` ,
body : commands . map ( ( command ) => ` $ ${ name } ${ command . name === "" ? "" : ` ${ command . name } ` } --help ` ) . join ( "\n" )
} ) ;
}
let options = this . isGlobalCommand ? globalOptions : [ ... this . options , ... globalOptions || [ ] ] ;
if ( ! this . isGlobalCommand && ! this . isDefaultCommand ) options = options . filter ( ( option ) => option . name !== "version" ) ;
if ( options . length > 0 ) {
const longestOptionName = findLongest ( options . map ( ( option ) => option . rawName ) ) ;
sections . push ( {
title : "Options" ,
body : options . map ( ( option ) => {
return ` ${ padRight ( option . rawName , longestOptionName . length ) } ${ option . description } ${ option . config . default === void 0 ? "" : ` (default: ${ option . config . default } ) ` } ` ;
} ) . join ( "\n" )
} ) ;
}
if ( this . examples . length > 0 ) sections . push ( {
title : "Examples" ,
body : this . examples . map ( ( example ) => {
if ( typeof example === "function" ) return example ( name ) ;
return example ;
} ) . join ( "\n" )
} ) ;
if ( helpCallback ) sections = helpCallback ( sections ) || sections ;
console . log ( sections . map ( ( section ) => {
return section . title ? ` ${ section . title } :
2024-01-05 12:14:38 +00:00
$ { section . body } ` : section.body;
2026-02-16 13:45:45 +00:00
} ) . join ( "\n\n" ) ) ;
}
outputVersion ( ) {
const { name } = this . cli ;
const { versionNumber } = this . cli . globalCommand ;
if ( versionNumber ) console . log ( ` ${ name } / ${ versionNumber } ${ platformInfo } ` ) ;
}
checkRequiredArgs ( ) {
const minimalArgsCount = this . args . filter ( ( arg ) => arg . required ) . length ;
if ( this . cli . args . length < minimalArgsCount ) throw new CACError ( ` missing required args for command \` ${ this . rawName } \` ` ) ;
}
checkUnknownOptions ( ) {
const { options , globalCommand } = this . cli ;
if ( ! this . config . allowUnknownOptions ) {
for ( const name of Object . keys ( options ) ) if ( name !== "--" && ! this . hasOption ( name ) && ! globalCommand . hasOption ( name ) ) throw new CACError ( ` Unknown option \` ${ name . length > 1 ? ` -- ${ name } ` : ` - ${ name } ` } \` ` ) ;
}
}
checkOptionValue ( ) {
const { options : parsedOptions , globalCommand } = this . cli ;
const options = [ ... globalCommand . options , ... this . options ] ;
for ( const option of options ) {
const value = parsedOptions [ option . name . split ( "." ) [ 0 ] ] ;
if ( option . required ) {
const hasNegated = options . some ( ( o ) => o . negated && o . names . includes ( option . name ) ) ;
if ( value === true || value === false && ! hasNegated ) throw new CACError ( ` option \` ${ option . rawName } \` value is missing ` ) ;
}
}
}
} ;
var GlobalCommand = class extends Command {
constructor ( cli$1 ) {
super ( "@@global@@" , "" , { } , cli$1 ) ;
}
} ;
2024-01-05 12:14:38 +00:00
var _ _assign = Object . assign ;
2026-02-16 13:45:45 +00:00
var CAC = class extends EventEmitter {
constructor ( name = "" ) {
super ( ) ;
this . name = name ;
this . commands = [ ] ;
this . rawArgs = [ ] ;
this . args = [ ] ;
this . options = { } ;
this . globalCommand = new GlobalCommand ( this ) ;
this . globalCommand . usage ( "<command> [options]" ) ;
}
usage ( text ) {
this . globalCommand . usage ( text ) ;
return this ;
}
command ( rawName , description , config ) {
const command = new Command ( rawName , description || "" , config , this ) ;
command . globalCommand = this . globalCommand ;
this . commands . push ( command ) ;
return command ;
}
option ( rawName , description , config ) {
this . globalCommand . option ( rawName , description , config ) ;
return this ;
}
help ( callback ) {
this . globalCommand . option ( "-h, --help" , "Display this message" ) ;
this . globalCommand . helpCallback = callback ;
this . showHelpOnExit = true ;
return this ;
}
version ( version , customFlags = "-v, --version" ) {
this . globalCommand . version ( version , customFlags ) ;
this . showVersionOnExit = true ;
return this ;
}
example ( example ) {
this . globalCommand . example ( example ) ;
return this ;
}
outputHelp ( ) {
if ( this . matchedCommand ) this . matchedCommand . outputHelp ( ) ;
else this . globalCommand . outputHelp ( ) ;
}
outputVersion ( ) {
this . globalCommand . outputVersion ( ) ;
}
setParsedInfo ( { args , options } , matchedCommand , matchedCommandName ) {
this . args = args ;
this . options = options ;
if ( matchedCommand ) this . matchedCommand = matchedCommand ;
if ( matchedCommandName ) this . matchedCommandName = matchedCommandName ;
return this ;
}
unsetMatchedCommand ( ) {
this . matchedCommand = void 0 ;
this . matchedCommandName = void 0 ;
}
parse ( argv = processArgs , { run = true } = { } ) {
this . rawArgs = argv ;
if ( ! this . name ) this . name = argv [ 1 ] ? getFileName ( argv [ 1 ] ) : "cli" ;
let shouldParse = true ;
for ( const command of this . commands ) {
const parsed = this . mri ( argv . slice ( 2 ) , command ) ;
const commandName = parsed . args [ 0 ] ;
if ( command . isMatched ( commandName ) ) {
shouldParse = false ;
const parsedInfo = _ _assign ( _ _assign ( { } , parsed ) , { args : parsed . args . slice ( 1 ) } ) ;
this . setParsedInfo ( parsedInfo , command , commandName ) ;
this . emit ( ` command: ${ commandName } ` , command ) ;
}
}
if ( shouldParse ) {
for ( const command of this . commands ) if ( command . name === "" ) {
shouldParse = false ;
const parsed = this . mri ( argv . slice ( 2 ) , command ) ;
this . setParsedInfo ( parsed , command ) ;
this . emit ( ` command:! ` , command ) ;
}
}
if ( shouldParse ) {
const parsed = this . mri ( argv . slice ( 2 ) ) ;
this . setParsedInfo ( parsed ) ;
}
if ( this . options . help && this . showHelpOnExit ) {
this . outputHelp ( ) ;
run = false ;
this . unsetMatchedCommand ( ) ;
}
if ( this . options . version && this . showVersionOnExit && this . matchedCommandName == null ) {
this . outputVersion ( ) ;
run = false ;
this . unsetMatchedCommand ( ) ;
}
const parsedArgv = {
args : this . args ,
options : this . options
} ;
if ( run ) this . runMatchedCommand ( ) ;
if ( ! this . matchedCommand && this . args [ 0 ] ) this . emit ( "command:*" ) ;
return parsedArgv ;
}
mri ( argv , command ) {
const cliOptions = [ ... this . globalCommand . options , ... command ? command . options : [ ] ] ;
const mriOptions = getMriOptions ( cliOptions ) ;
let argsAfterDoubleDashes = [ ] ;
const doubleDashesIndex = argv . indexOf ( "--" ) ;
if ( doubleDashesIndex > - 1 ) {
argsAfterDoubleDashes = argv . slice ( doubleDashesIndex + 1 ) ;
argv = argv . slice ( 0 , doubleDashesIndex ) ;
}
let parsed = mri2 ( argv , mriOptions ) ;
parsed = Object . keys ( parsed ) . reduce ( ( res , name ) => {
return _ _assign ( _ _assign ( { } , res ) , { [ camelcaseOptionName ( name ) ] : parsed [ name ] } ) ;
} , { _ : [ ] } ) ;
const args = parsed . _ ;
const options = { "--" : argsAfterDoubleDashes } ;
const ignoreDefault = command && command . config . ignoreOptionDefaultValue ? command . config . ignoreOptionDefaultValue : this . globalCommand . config . ignoreOptionDefaultValue ;
let transforms = Object . create ( null ) ;
for ( const cliOption of cliOptions ) {
if ( ! ignoreDefault && cliOption . config . default !== void 0 ) for ( const name of cliOption . names ) options [ name ] = cliOption . config . default ;
if ( Array . isArray ( cliOption . config . type ) ) {
if ( transforms [ cliOption . name ] === void 0 ) {
transforms [ cliOption . name ] = Object . create ( null ) ;
transforms [ cliOption . name ] [ "shouldTransform" ] = true ;
transforms [ cliOption . name ] [ "transformFunction" ] = cliOption . config . type [ 0 ] ;
}
}
}
for ( const key of Object . keys ( parsed ) ) if ( key !== "_" ) {
setDotProp ( options , key . split ( "." ) , parsed [ key ] ) ;
setByType ( options , transforms ) ;
}
return {
args ,
options
} ;
}
runMatchedCommand ( ) {
const { args , options , matchedCommand : command } = this ;
if ( ! command || ! command . commandAction ) return ;
command . checkUnknownOptions ( ) ;
command . checkOptionValue ( ) ;
command . checkRequiredArgs ( ) ;
const actionArgs = [ ] ;
command . args . forEach ( ( arg , index ) => {
if ( arg . variadic ) actionArgs . push ( args . slice ( index ) ) ;
else actionArgs . push ( args [ index ] ) ;
} ) ;
actionArgs . push ( options ) ;
return command . commandAction . apply ( this , actionArgs ) ;
}
} ;
2024-01-05 12:14:38 +00:00
const cac = ( name = "" ) => new CAC ( name ) ;
2026-02-16 13:45:45 +00:00
//#endregion
//#region src/node/cli.ts
var import _picocolors = /* @__PURE__ */ _ _toESM ( require _picocolors ( ) , 1 ) ;
function checkNodeVersion ( nodeVersion ) {
const currentVersion = nodeVersion . split ( "." ) ;
const major = parseInt ( currentVersion [ 0 ] , 10 ) ;
const minor = parseInt ( currentVersion [ 1 ] , 10 ) ;
return major === 20 && minor >= 19 || major === 22 && minor >= 12 || major > 22 ;
}
if ( ! checkNodeVersion ( process . versions . node ) ) console . warn ( import _picocolors . default . yellow ( ` You are using Node.js ${ process . versions . node } . Vite requires Node.js version 20.19+ or 22.12+. Please upgrade your Node.js version. ` ) ) ;
2026-02-11 16:20:26 +00:00
const cli = cac ( "vite" ) ;
2024-01-05 12:14:38 +00:00
let profileSession = global . _ _vite _profile _session ;
let profileCount = 0 ;
const stopProfiler = ( log ) => {
2026-02-16 13:45:45 +00:00
if ( ! profileSession ) return ;
return new Promise ( ( res , rej ) => {
profileSession . post ( "Profiler.stop" , ( err , { profile } ) => {
if ( ! err ) {
const outPath = path . resolve ( ` ./vite-profile- ${ profileCount ++ } .cpuprofile ` ) ;
fs . writeFileSync ( outPath , JSON . stringify ( profile ) ) ;
log ( import _picocolors . default . yellow ( ` CPU profile written to ${ import _picocolors . default . white ( import _picocolors . default . dim ( outPath ) ) } ` ) ) ;
profileSession = void 0 ;
res ( ) ;
} else rej ( err ) ;
} ) ;
} ) ;
2024-01-05 12:14:38 +00:00
} ;
const filterDuplicateOptions = ( options ) => {
2026-02-16 13:45:45 +00:00
for ( const [ key , value ] of Object . entries ( options ) ) if ( Array . isArray ( value ) ) options [ key ] = value [ value . length - 1 ] ;
2024-01-05 12:14:38 +00:00
} ;
2026-02-16 13:45:45 +00:00
/ * *
* removing global flags before passing as command specific sub - configs
* /
function cleanGlobalCLIOptions ( options ) {
const ret = { ... options } ;
delete ret [ "--" ] ;
delete ret . c ;
delete ret . config ;
delete ret . base ;
delete ret . l ;
delete ret . logLevel ;
delete ret . clearScreen ;
delete ret . configLoader ;
delete ret . d ;
delete ret . debug ;
delete ret . f ;
delete ret . filter ;
delete ret . m ;
delete ret . mode ;
delete ret . force ;
delete ret . w ;
if ( "sourcemap" in ret ) {
const sourcemap = ret . sourcemap ;
ret . sourcemap = sourcemap === "true" ? true : sourcemap === "false" ? false : ret . sourcemap ;
}
if ( "watch" in ret ) ret . watch = ret . watch ? { } : void 0 ;
return ret ;
2024-01-05 12:14:38 +00:00
}
2026-02-16 13:45:45 +00:00
/ * *
* removing builder flags before passing as command specific sub - configs
* /
function cleanBuilderCLIOptions ( options ) {
const ret = { ... options } ;
delete ret . app ;
return ret ;
}
/ * *
* host may be a number ( like 0 ) , should convert to string
* /
2024-01-05 12:14:38 +00:00
const convertHost = ( v ) => {
2026-02-16 13:45:45 +00:00
if ( typeof v === "number" ) return String ( v ) ;
return v ;
2024-01-05 12:14:38 +00:00
} ;
2026-02-16 13:45:45 +00:00
/ * *
* base may be a number ( like 0 ) , should convert to empty string
* /
2024-01-05 12:14:38 +00:00
const convertBase = ( v ) => {
2026-02-16 13:45:45 +00:00
if ( v === 0 ) return "" ;
return v ;
2024-01-05 12:14:38 +00:00
} ;
2026-02-16 13:45:45 +00:00
cli . option ( "-c, --config <file>" , ` [string] use specified config file ` ) . option ( "--base <path>" , ` [string] public base path (default: /) ` , { type : [ convertBase ] } ) . option ( "-l, --logLevel <level>" , ` [string] info | warn | error | silent ` ) . option ( "--clearScreen" , ` [boolean] allow/disable clear screen when logging ` ) . option ( "--configLoader <loader>" , ` [string] use 'bundle' to bundle the config with esbuild, or 'runner' (experimental) to process it on the fly, or 'native' (experimental) to load using the native runtime (default: bundle) ` ) . option ( "-d, --debug [feat]" , ` [string | boolean] show debug logs ` ) . option ( "-f, --filter <filter>" , ` [string] filter debug logs ` ) . option ( "-m, --mode <mode>" , ` [string] set env mode ` ) ;
cli . command ( "[root]" , "start dev server" ) . alias ( "serve" ) . alias ( "dev" ) . option ( "--host [host]" , ` [string] specify hostname ` , { type : [ convertHost ] } ) . option ( "--port <port>" , ` [number] specify port ` ) . option ( "--open [path]" , ` [boolean | string] open browser on startup ` ) . option ( "--cors" , ` [boolean] enable CORS ` ) . option ( "--strictPort" , ` [boolean] exit if specified port is already in use ` ) . option ( "--force" , ` [boolean] force the optimizer to ignore the cache and re-bundle ` ) . action ( async ( root , options ) => {
filterDuplicateOptions ( options ) ;
const { createServer } = await import ( "./chunks/server.js" ) ;
try {
const server = await createServer ( {
root ,
base : options . base ,
mode : options . mode ,
configFile : options . config ,
configLoader : options . configLoader ,
logLevel : options . logLevel ,
clearScreen : options . clearScreen ,
server : cleanGlobalCLIOptions ( options ) ,
forceOptimizeDeps : options . force
} ) ;
if ( ! server . httpServer ) throw new Error ( "HTTP server not available" ) ;
await server . listen ( ) ;
const info = server . config . logger . info ;
const modeString = options . mode && options . mode !== "development" ? ` ${ import _picocolors . default . bgGreen ( ` ${ import _picocolors . default . bold ( options . mode ) } ` ) } ` : "" ;
const viteStartTime = global . _ _vite _start _time ? ? false ;
const startupDurationString = viteStartTime ? import _picocolors . default . dim ( ` ready in ${ import _picocolors . default . reset ( import _picocolors . default . bold ( Math . ceil ( performance . now ( ) - viteStartTime ) ) ) } ms ` ) : "" ;
const hasExistingLogs = process . stdout . bytesWritten > 0 || process . stderr . bytesWritten > 0 ;
info ( ` \n ${ import _picocolors . default . green ( ` ${ import _picocolors . default . bold ( "VITE" ) } v ${ VERSION } ` ) } ${ modeString } ${ startupDurationString } \n ` , { clear : ! hasExistingLogs } ) ;
server . printUrls ( ) ;
const customShortcuts = [ ] ;
if ( profileSession ) customShortcuts . push ( {
key : "p" ,
description : "start/stop the profiler" ,
async action ( server$1 ) {
if ( profileSession ) await stopProfiler ( server$1 . config . logger . info ) ;
else {
const inspector = await import ( "node:inspector" ) . then ( ( r ) => r . default ) ;
await new Promise ( ( res ) => {
profileSession = new inspector . Session ( ) ;
profileSession . connect ( ) ;
profileSession . post ( "Profiler.enable" , ( ) => {
profileSession . post ( "Profiler.start" , ( ) => {
server$1 . config . logger . info ( "Profiler started" ) ;
res ( ) ;
} ) ;
} ) ;
} ) ;
}
}
} ) ;
server . bindCLIShortcuts ( {
print : true ,
customShortcuts
} ) ;
} catch ( e ) {
const logger = createLogger ( options . logLevel ) ;
logger . error ( import _picocolors . default . red ( ` error when starting dev server: \n ${ e . stack } ` ) , { error : e } ) ;
await stopProfiler ( logger . info ) ;
process . exit ( 1 ) ;
}
} ) ;
cli . command ( "build [root]" , "build for production" ) . option ( "--target <target>" , ` [string] transpile target (default: 'baseline-widely-available') ` ) . option ( "--outDir <dir>" , ` [string] output directory (default: dist) ` ) . option ( "--assetsDir <dir>" , ` [string] directory under outDir to place assets in (default: assets) ` ) . option ( "--assetsInlineLimit <number>" , ` [number] static asset base64 inline threshold in bytes (default: 4096) ` ) . option ( "--ssr [entry]" , ` [string] build specified entry for server-side rendering ` ) . option ( "--sourcemap [output]" , ` [boolean | "inline" | "hidden"] output source maps for build (default: false) ` ) . option ( "--minify [minifier]" , "[boolean | \"terser\" | \"esbuild\"] enable/disable minification, or specify minifier to use (default: esbuild)" ) . option ( "--manifest [name]" , ` [boolean | string] emit build manifest json ` ) . option ( "--ssrManifest [name]" , ` [boolean | string] emit ssr manifest json ` ) . option ( "--emptyOutDir" , ` [boolean] force empty outDir when it's outside of root ` ) . option ( "-w, --watch" , ` [boolean] rebuilds when modules have changed on disk ` ) . option ( "--app" , ` [boolean] same as \` builder: {} \` ` ) . action ( async ( root , options ) => {
filterDuplicateOptions ( options ) ;
const { createBuilder } = await import ( "./chunks/build.js" ) ;
const buildOptions = cleanGlobalCLIOptions ( cleanBuilderCLIOptions ( options ) ) ;
try {
await ( await createBuilder ( {
root ,
base : options . base ,
mode : options . mode ,
configFile : options . config ,
configLoader : options . configLoader ,
logLevel : options . logLevel ,
clearScreen : options . clearScreen ,
build : buildOptions ,
... options . app ? { builder : { } } : { }
} , null ) ) . buildApp ( ) ;
} catch ( e ) {
createLogger ( options . logLevel ) . error ( import _picocolors . default . red ( ` error during build: \n ${ e . stack } ` ) , { error : e } ) ;
process . exit ( 1 ) ;
} finally {
await stopProfiler ( ( message ) => createLogger ( options . logLevel ) . info ( message ) ) ;
}
2024-01-05 12:14:38 +00:00
} ) ;
2026-02-16 13:45:45 +00:00
cli . command ( "optimize [root]" , "pre-bundle dependencies (deprecated, the pre-bundle process runs automatically and does not need to be called)" ) . option ( "--force" , ` [boolean] force the optimizer to ignore the cache and re-bundle ` ) . action ( async ( root , options ) => {
filterDuplicateOptions ( options ) ;
const { resolveConfig } = await import ( "./chunks/config2.js" ) ;
const { optimizeDeps } = await import ( "./chunks/optimizer.js" ) ;
try {
await optimizeDeps ( await resolveConfig ( {
root ,
base : options . base ,
configFile : options . config ,
configLoader : options . configLoader ,
logLevel : options . logLevel ,
mode : options . mode
} , "serve" ) , options . force , true ) ;
} catch ( e ) {
createLogger ( options . logLevel ) . error ( import _picocolors . default . red ( ` error when optimizing deps: \n ${ e . stack } ` ) , { error : e } ) ;
process . exit ( 1 ) ;
}
} ) ;
cli . command ( "preview [root]" , "locally preview production build" ) . option ( "--host [host]" , ` [string] specify hostname ` , { type : [ convertHost ] } ) . option ( "--port <port>" , ` [number] specify port ` ) . option ( "--strictPort" , ` [boolean] exit if specified port is already in use ` ) . option ( "--open [path]" , ` [boolean | string] open browser on startup ` ) . option ( "--outDir <dir>" , ` [string] output directory (default: dist) ` ) . action ( async ( root , options ) => {
filterDuplicateOptions ( options ) ;
const { preview } = await import ( "./chunks/preview.js" ) ;
try {
const server = await preview ( {
root ,
base : options . base ,
configFile : options . config ,
configLoader : options . configLoader ,
logLevel : options . logLevel ,
mode : options . mode ,
build : { outDir : options . outDir } ,
preview : {
port : options . port ,
strictPort : options . strictPort ,
host : options . host ,
open : options . open
}
} ) ;
server . printUrls ( ) ;
server . bindCLIShortcuts ( { print : true } ) ;
} catch ( e ) {
createLogger ( options . logLevel ) . error ( import _picocolors . default . red ( ` error when starting preview server: \n ${ e . stack } ` ) , { error : e } ) ;
process . exit ( 1 ) ;
} finally {
await stopProfiler ( ( message ) => createLogger ( options . logLevel ) . info ( message ) ) ;
}
2024-01-05 12:14:38 +00:00
} ) ;
cli . help ( ) ;
cli . version ( VERSION ) ;
cli . parse ( ) ;
2026-02-16 13:45:45 +00:00
//#endregion
export { stopProfiler } ;