99
1010import { rimraf } from 'rimraf'
1111import { tsc } from '@athenna/tsconfig/tsc'
12- import { Path , Color } from '@athenna/common'
13- import { BaseCommand } from '@athenna/artisan'
1412import { isAbsolute , join , parse } from 'node:path'
13+ import { Path , Color , Module } from '@athenna/common'
14+ import { BaseCommand , Option } from '@athenna/artisan'
1515import { copyfiles } from '@athenna/tsconfig/copyfiles'
1616import { UndefinedOutDirException } from '#src/exceptions/UndefinedOutDirException'
1717
1818export class BuildCommand extends BaseCommand {
19+ @Option ( {
20+ signature : '-v, --vite' ,
21+ description : 'Use vite to build your application static files.' ,
22+ default : false
23+ } )
24+ public vite : boolean
25+
1926 public static signature ( ) : string {
2027 return 'build'
2128 }
@@ -40,7 +47,7 @@ export class BuildCommand extends BaseCommand {
4047 }
4148
4249 const compiler = Color . yellow . bold ( 'tsc' )
43- const includedFiles = Color . gray ( include . join ( ', ' ) )
50+ const includedPaths = Color . gray ( include . join ( ', ' ) )
4451
4552 const outDir = this . getOutDir ( )
4653 const outDirName = Color . yellow . bold ( parse ( outDir ) . name )
@@ -56,11 +63,52 @@ export class BuildCommand extends BaseCommand {
5663
5764 if ( include . length ) {
5865 tasks . addPromise (
59- `Copying included paths to ${ outDirName } folder: ${ includedFiles } ` ,
66+ `Copying included paths to ${ outDirName } folder: ${ includedPaths } ` ,
6067 ( ) => copyfiles ( include , outDir )
6168 )
6269 }
6370
71+ if ( this . vite ) {
72+ const vite = this . getVite ( )
73+
74+ tasks . addPromise ( 'Compiling static files using vite' , async ( ) => {
75+ const defaultConfig = {
76+ root : Path . pwd ( ) ,
77+ assetsUrl : '/assets' ,
78+ buildDirectory : 'public/assets' ,
79+ logLevel : Config . get ( 'rc.bootLogs' , true ) ? 'info' : 'silent' ,
80+ build : {
81+ assetsDir : '' ,
82+ manifest : true ,
83+ emptyOutDir : true ,
84+ outDir : 'public/assets' ,
85+ assetsInlineLimit : 0 ,
86+ rollupOptions : {
87+ output : {
88+ entryFileNames : '[name].js' ,
89+ chunkFileNames : '[name].js' ,
90+ assetFileNames : '[name].[ext]'
91+ } ,
92+ input : [ 'src/resources/css/app.scss' , 'src/resources/js/app.js' ]
93+ }
94+ }
95+ }
96+
97+ const { config : fileConfig } = await vite . loadConfigFromFile (
98+ {
99+ command : 'build' ,
100+ mode : 'production'
101+ } ,
102+ undefined ,
103+ Path . pwd ( )
104+ )
105+
106+ const config = vite . mergeConfig ( defaultConfig , fileConfig )
107+
108+ return vite . build ( config )
109+ } )
110+ }
111+
64112 await tasks . run ( )
65113
66114 console . log ( )
@@ -84,4 +132,10 @@ export class BuildCommand extends BaseCommand {
84132
85133 return Path . pwd ( Config . get ( 'rc.commands.build.outDir' ) )
86134 }
135+
136+ public getVite ( ) {
137+ const require = Module . createRequire ( import . meta. url )
138+
139+ return require ( 'vite' )
140+ }
87141}
0 commit comments