@@ -19,8 +19,9 @@ import { detectPackageManager } from '@antfu/install-pkg'
19
19
20
20
import type { BundlerOptions } from './types/common.ts'
21
21
import { run , parseConfig , copyFiles , loadHooks } from './utils.ts'
22
- import { type HookParams , type BundlerHooks } from './types/hooks.ts'
22
+ import { type HookParams , type BundlerHooks , type CommonHooks } from './types/hooks.ts'
23
23
import { type SupportedPackageManager } from './types/code_transformer.ts'
24
+ import { IndexGenerator } from './index_generator/main.ts'
24
25
25
26
/**
26
27
* List of package managers we support in order to
@@ -70,9 +71,18 @@ export class Bundler {
70
71
/**
71
72
* Hooks to execute custom actions during the build process
72
73
*/
73
- #hooks! : Hooks < {
74
- [ K in keyof BundlerHooks ] : [ HookParams < K > , HookParams < K > ]
75
- } >
74
+ #hooks! : Hooks <
75
+ {
76
+ [ K in keyof CommonHooks ] : [ HookParams < K > , HookParams < K > ]
77
+ } & {
78
+ [ K in keyof BundlerHooks ] : [ HookParams < K > , HookParams < K > ]
79
+ }
80
+ >
81
+
82
+ /**
83
+ * Index generator for managing auto-generated index files
84
+ */
85
+ #indexGenerator! : IndexGenerator
76
86
77
87
/**
78
88
* CLI UI instance for displaying colorful messages and progress information
@@ -202,7 +212,6 @@ export class Bundler {
202
212
* const success = await bundler.bundle(true, 'npm')
203
213
*/
204
214
async bundle ( stopOnError : boolean = true , client ?: SupportedPackageManager ) : Promise < boolean > {
205
- this . #hooks = await loadHooks ( this . options . hooks , [ 'buildStarting' , 'buildFinished' ] )
206
215
this . packageManager = client ?? ( await this . #detectPackageManager( ) ) ?? 'npm'
207
216
208
217
/**
@@ -213,20 +222,33 @@ export class Bundler {
213
222
return false
214
223
}
215
224
225
+ this . ui . logger . info ( 'loading hooks...' )
226
+ this . #hooks = await loadHooks ( this . options . hooks , [ 'init' , 'buildStarting' , 'buildFinished' ] )
227
+ this . #indexGenerator = new IndexGenerator ( this . cwdPath , this . ui . logger )
228
+
229
+ /**
230
+ * Step 2: Run init hook and the index generator
231
+ */
232
+ await this . #hooks. runner ( 'init' ) . run ( this , this . #indexGenerator)
233
+ this . #hooks. clear ( 'init' )
234
+
235
+ this . ui . logger . info ( 'generating indexes...' )
236
+ await this . #indexGenerator. generate ( )
237
+
216
238
/**
217
- * Step 2 : Cleanup existing build directory (if any)
239
+ * Step 3 : Cleanup existing build directory (if any)
218
240
*/
219
241
const outDir = config . options . outDir || fileURLToPath ( new URL ( 'build/' , this . cwd ) )
220
242
this . ui . logger . info ( 'cleaning up output directory' , { suffix : this . #getRelativeName( outDir ) } )
221
243
await this . #cleanupBuildDirectory( outDir )
222
244
223
245
/**
224
- * Step 3 : Execute build starting hook
246
+ * Step 4 : Execute build starting hook
225
247
*/
226
248
await this . #hooks. runner ( 'buildStarting' ) . run ( this )
227
249
228
250
/**
229
- * Step 4 : Build typescript source code
251
+ * Step 5 : Build typescript source code
230
252
*/
231
253
this . ui . logger . info ( 'compiling typescript source' , { suffix : 'tsc' } )
232
254
const buildCompleted = await this . #runTsc( outDir )
@@ -258,7 +280,7 @@ export class Bundler {
258
280
}
259
281
260
282
/**
261
- * Step 5 : Copy meta files to the build directory
283
+ * Step 6 : Copy meta files to the build directory
262
284
*/
263
285
const pkgFiles = [
264
286
'package.json' ,
@@ -275,12 +297,12 @@ export class Bundler {
275
297
. heading ( 'Run the following commands to start the server in production' )
276
298
277
299
/**
278
- * Step 6 : Execute build completed hook
300
+ * Step 7 : Execute build completed hook
279
301
*/
280
302
await this . #hooks. runner ( 'buildFinished' ) . run ( this , displayMessage )
281
303
282
304
/**
283
- * Next steps
305
+ * Display next steps
284
306
*/
285
307
displayMessage
286
308
. add ( this . ui . colors . cyan ( `cd ${ this . #getRelativeName( outDir ) } ` ) )
0 commit comments