10
10
import fs from 'fs-extra'
11
11
import slash from 'slash'
12
12
import copyfiles from 'cpy'
13
- import { execa } from 'execa'
14
- import { join , relative } from 'node:path'
15
13
import type tsStatic from 'typescript'
16
14
import { fileURLToPath } from 'node:url'
17
- import { ConfigParser } from '@poppinss/chokidar-ts '
15
+ import { join , relative } from 'node:path '
18
16
import { cliui , type Logger } from '@poppinss/cliui'
19
17
18
+ import { run } from './run.js'
19
+ import { parseConfig } from './parse_config.js'
20
20
import type { BundlerOptions } from './types.js'
21
21
22
22
/**
@@ -59,18 +59,37 @@ export class Bundler {
59
59
await fs . remove ( outDir )
60
60
}
61
61
62
+ /**
63
+ * Runs assets bundler command to build assets.
64
+ */
65
+ async #buildAssets( ) : Promise < boolean > {
66
+ const assetsBundler = this . #options. assets
67
+ if ( ! assetsBundler ?. serve ) {
68
+ return true
69
+ }
70
+
71
+ try {
72
+ this . #logger. info ( 'compiling frontend assets' , { suffix : assetsBundler . cmd } )
73
+ await run ( this . #cwd, {
74
+ stdio : 'inherit' ,
75
+ script : assetsBundler . cmd ,
76
+ scriptArgs : [ ] ,
77
+ } )
78
+ return true
79
+ } catch {
80
+ return false
81
+ }
82
+ }
83
+
62
84
/**
63
85
* Runs tsc command to build the source.
64
86
*/
65
- async #runTsc( outDir : string ) {
87
+ async #runTsc( outDir : string ) : Promise < boolean > {
66
88
try {
67
- await execa ( 'tsc' , [ '--outDir' , outDir ] , {
68
- cwd : this . #cwd,
69
- preferLocal : true ,
70
- localDir : this . #cwd,
71
- windowsHide : false ,
72
- buffer : false ,
89
+ await run ( this . #cwd, {
73
90
stdio : 'inherit' ,
91
+ script : 'tsc' ,
92
+ scriptArgs : [ '--outDir' , outDir ] ,
74
93
} )
75
94
return true
76
95
} catch {
@@ -161,21 +180,7 @@ export class Bundler {
161
180
/**
162
181
* Step 1: Parse config file to get the build output directory
163
182
*/
164
- const { config, error } = new ConfigParser ( this . #cwd, 'tsconfig.json' , this . #ts) . parse ( )
165
- if ( error ) {
166
- const compilerHost = this . #ts. createCompilerHost ( { } )
167
- this . #logger. logError ( this . #ts. formatDiagnosticsWithColorAndContext ( [ error ] , compilerHost ) )
168
- return false
169
- }
170
-
171
- if ( config ! . errors . length ) {
172
- const compilerHost = this . #ts. createCompilerHost ( { } )
173
- this . #logger. logError (
174
- this . #ts. formatDiagnosticsWithColorAndContext ( config ! . errors , compilerHost )
175
- )
176
- return false
177
- }
178
-
183
+ const config = parseConfig ( this . #cwd, this . #ts)
179
184
if ( ! config ) {
180
185
return false
181
186
}
@@ -188,7 +193,14 @@ export class Bundler {
188
193
await this . #cleanupBuildDirectory( outDir )
189
194
190
195
/**
191
- * Step 3: Build typescript source code
196
+ * Step 3: Build frontend assets
197
+ */
198
+ if ( ! ( await this . #buildAssets( ) ) ) {
199
+ return false
200
+ }
201
+
202
+ /**
203
+ * Step 4: Build typescript source code
192
204
*/
193
205
this . #logger. info ( 'compiling typescript source' , { suffix : 'tsc' } )
194
206
const buildCompleted = await this . #runTsc( outDir )
@@ -219,21 +231,24 @@ export class Bundler {
219
231
}
220
232
221
233
/**
222
- * Step 4 : Copy meta files to the build directory
234
+ * Step 5 : Copy meta files to the build directory
223
235
*/
224
236
const pkgFiles = [ 'package.json' , this . #getClientLockFile( client ) ]
225
237
this . #logger. info ( 'copying meta files to the output directory' )
226
238
await this . #copyMetaFiles( outDir , pkgFiles )
227
239
228
240
/**
229
- * Step 5 : Copy .adonisrc.json file to the build directory
241
+ * Step 6 : Copy .adonisrc.json file to the build directory
230
242
*/
231
243
this . #logger. info ( 'copying .adonisrc.json file to the output directory' )
232
244
await this . #copyAdonisRcFile( outDir )
233
245
234
246
this . #logger. success ( 'build completed' )
235
247
this . #logger. log ( '' )
236
248
249
+ /**
250
+ * Next steps
251
+ */
237
252
ui . instructions ( )
238
253
. useRenderer ( this . #logger. getRenderer ( ) )
239
254
. heading ( 'Run the following commands to start the server in production' )
0 commit comments