8
8
*/
9
9
10
10
import { type Instructions } from '@poppinss/cliui'
11
- import { type UnWrapLazyImport , type AsyncOrSync , type LazyImport } from '@poppinss/utils/types'
11
+ import { type AsyncOrSync , type LazyImport } from '@poppinss/utils/types'
12
12
13
13
import { type Bundler } from '../bundler.ts'
14
14
import { type DevServer } from '../dev_server.ts'
@@ -17,6 +17,24 @@ import { type RoutesListItem } from './code_scanners.ts'
17
17
import { type IndexGenerator } from '../index_generator/main.ts'
18
18
import { type RoutesScanner } from '../code_scanners/routes_scanner/main.ts'
19
19
20
+ /**
21
+ * Defines a hook that can be either a lazy import or an object with a run method.
22
+ * This type provides flexibility in how hooks are defined and imported, supporting
23
+ * both dynamic imports for code splitting and direct object definitions.
24
+ *
25
+ * @template Fn - The function signature that the hook must conform to
26
+ *
27
+ * @example
28
+ * // Using lazy import
29
+ * const hook: DefineHook<(server: DevServer) => void> = () => import('./my-hook')
30
+ *
31
+ * // Using direct object
32
+ * const hook: DefineHook<(server: DevServer) => void> = {
33
+ * run: (server) => console.log('Hook executed')
34
+ * }
35
+ */
36
+ type DefineHook < Fn extends ( ...args : any ) => any > = LazyImport < Fn > | { run : Fn }
37
+
20
38
/**
21
39
* Common hooks executed by the dev-server, test runner and the bundler.
22
40
* These hooks are shared across all assembler operations and provide
@@ -37,7 +55,7 @@ export type CommonHooks = {
37
55
*
38
56
* @param parent - The parent instance (DevServer, TestRunner, or Bundler)
39
57
*/
40
- init : LazyImport <
58
+ init : DefineHook <
41
59
( parent : DevServer | TestRunner | Bundler , indexGenerator : IndexGenerator ) => AsyncOrSync < void >
42
60
> [ ]
43
61
}
@@ -67,7 +85,7 @@ export type RouterHooks = {
67
85
* @param parent - The DevServer instance
68
86
* @param routes - Record of routes grouped by domain or method
69
87
*/
70
- routesCommitted : LazyImport <
88
+ routesCommitted : DefineHook <
71
89
( parent : DevServer , routes : Record < string , RoutesListItem [ ] > ) => AsyncOrSync < void >
72
90
> [ ]
73
91
@@ -78,7 +96,7 @@ export type RouterHooks = {
78
96
* @param parent - The DevServer instance
79
97
* @param routesScanner - The RoutesScanner instance being used
80
98
*/
81
- routesScanning : LazyImport <
99
+ routesScanning : DefineHook <
82
100
( parent : DevServer , routesScanner : RoutesScanner ) => AsyncOrSync < void >
83
101
> [ ]
84
102
@@ -89,7 +107,7 @@ export type RouterHooks = {
89
107
* @param parent - The DevServer instance
90
108
* @param routesScanner - The RoutesScanner instance that was used
91
109
*/
92
- routesScanned : LazyImport <
110
+ routesScanned : DefineHook <
93
111
( parent : DevServer , routesScanner : RoutesScanner ) => AsyncOrSync < void >
94
112
> [ ]
95
113
}
@@ -126,7 +144,7 @@ export type WatcherHooks = {
126
144
* @param info.fullReload - Whether a full server reload is required
127
145
* @param parent - The parent DevServer or TestRunner instance
128
146
*/
129
- fileChanged : LazyImport <
147
+ fileChanged : DefineHook <
130
148
(
131
149
relativePath : string ,
132
150
absolutePath : string ,
@@ -149,7 +167,7 @@ export type WatcherHooks = {
149
167
* @param filePath - The absolute path to the added file
150
168
* @param server - The DevServer or TestRunner instance
151
169
*/
152
- fileAdded : LazyImport <
170
+ fileAdded : DefineHook <
153
171
(
154
172
relativePath : string ,
155
173
absolutePath : string ,
@@ -164,7 +182,7 @@ export type WatcherHooks = {
164
182
* @param filePath - The absolute path to the removed file
165
183
* @param server - The DevServer or TestRunner instance
166
184
*/
167
- fileRemoved : LazyImport <
185
+ fileRemoved : DefineHook <
168
186
(
169
187
relativePath : string ,
170
188
absolutePath : string ,
@@ -193,7 +211,7 @@ export type DevServerHooks = {
193
211
*
194
212
* @param server - The DevServer instance that is about to start
195
213
*/
196
- devServerStarting : LazyImport < ( server : DevServer ) => AsyncOrSync < void > > [ ]
214
+ devServerStarting : DefineHook < ( server : DevServer ) => AsyncOrSync < void > > [ ]
197
215
198
216
/**
199
217
* The hook is executed after the child process has been started.
@@ -205,7 +223,7 @@ export type DevServerHooks = {
205
223
* @param info.host - The host address the server is bound to
206
224
* @param uiInstructions - UI instructions for displaying server information
207
225
*/
208
- devServerStarted : LazyImport <
226
+ devServerStarted : DefineHook <
209
227
(
210
228
server : DevServer ,
211
229
info : { port : number ; host : string } ,
@@ -234,7 +252,7 @@ export type BundlerHooks = {
234
252
*
235
253
* @param server - The Bundler instance that will create the build
236
254
*/
237
- buildStarting : LazyImport < ( server : Bundler ) => AsyncOrSync < void > > [ ]
255
+ buildStarting : DefineHook < ( server : Bundler ) => AsyncOrSync < void > > [ ]
238
256
239
257
/**
240
258
* The hook is executed after the production build has been created.
@@ -243,7 +261,7 @@ export type BundlerHooks = {
243
261
* @param server - The Bundler instance that created the build
244
262
* @param uiInstructions - UI instructions for displaying build information
245
263
*/
246
- buildFinished : LazyImport < ( server : Bundler , uiInstructions : Instructions ) => AsyncOrSync < void > > [ ]
264
+ buildFinished : DefineHook < ( server : Bundler , uiInstructions : Instructions ) => AsyncOrSync < void > > [ ]
247
265
}
248
266
249
267
/**
@@ -266,15 +284,15 @@ export type TestRunnerHooks = {
266
284
*
267
285
* @param server - The TestRunner instance that will execute the tests
268
286
*/
269
- testsStarting : LazyImport < ( server : TestRunner ) => AsyncOrSync < void > > [ ]
287
+ testsStarting : DefineHook < ( server : TestRunner ) => AsyncOrSync < void > > [ ]
270
288
271
289
/**
272
290
* The hook is executed after the tests have been executed.
273
291
* Use this hook to clean up resources or generate test reports.
274
292
*
275
293
* @param server - The TestRunner instance that executed the tests
276
294
*/
277
- testsFinished : LazyImport < ( server : TestRunner ) => AsyncOrSync < void > > [ ]
295
+ testsFinished : DefineHook < ( server : TestRunner ) => AsyncOrSync < void > > [ ]
278
296
}
279
297
280
298
/**
@@ -319,6 +337,5 @@ export type AllHooks = CommonHooks &
319
337
* // Result: [DevServer, {port: number, host: string}, Instructions]
320
338
* ```
321
339
*/
322
- export type HookParams < Hook extends keyof AllHooks > = Parameters <
323
- UnWrapLazyImport < AllHooks [ Hook ] [ number ] >
324
- >
340
+ export type HookParams < Hook extends keyof AllHooks > =
341
+ AllHooks [ Hook ] [ number ] extends DefineHook < infer A > ? Parameters < A > : never
0 commit comments