Skip to content

Commit 0d8ac1f

Browse files
committed
feat: integrate index generator with bundler
1 parent cf79673 commit 0d8ac1f

File tree

7 files changed

+165
-160
lines changed

7 files changed

+165
-160
lines changed

src/bundler.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import { detectPackageManager } from '@antfu/install-pkg'
1919

2020
import type { BundlerOptions } from './types/common.ts'
2121
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'
2323
import { type SupportedPackageManager } from './types/code_transformer.ts'
24+
import { IndexGenerator } from './index_generator/main.ts'
2425

2526
/**
2627
* List of package managers we support in order to
@@ -70,9 +71,18 @@ export class Bundler {
7071
/**
7172
* Hooks to execute custom actions during the build process
7273
*/
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
7686

7787
/**
7888
* CLI UI instance for displaying colorful messages and progress information
@@ -202,7 +212,6 @@ export class Bundler {
202212
* const success = await bundler.bundle(true, 'npm')
203213
*/
204214
async bundle(stopOnError: boolean = true, client?: SupportedPackageManager): Promise<boolean> {
205-
this.#hooks = await loadHooks(this.options.hooks, ['buildStarting', 'buildFinished'])
206215
this.packageManager = client ?? (await this.#detectPackageManager()) ?? 'npm'
207216

208217
/**
@@ -213,20 +222,33 @@ export class Bundler {
213222
return false
214223
}
215224

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+
216238
/**
217-
* Step 2: Cleanup existing build directory (if any)
239+
* Step 3: Cleanup existing build directory (if any)
218240
*/
219241
const outDir = config.options.outDir || fileURLToPath(new URL('build/', this.cwd))
220242
this.ui.logger.info('cleaning up output directory', { suffix: this.#getRelativeName(outDir) })
221243
await this.#cleanupBuildDirectory(outDir)
222244

223245
/**
224-
* Step 3: Execute build starting hook
246+
* Step 4: Execute build starting hook
225247
*/
226248
await this.#hooks.runner('buildStarting').run(this)
227249

228250
/**
229-
* Step 4: Build typescript source code
251+
* Step 5: Build typescript source code
230252
*/
231253
this.ui.logger.info('compiling typescript source', { suffix: 'tsc' })
232254
const buildCompleted = await this.#runTsc(outDir)
@@ -258,7 +280,7 @@ export class Bundler {
258280
}
259281

260282
/**
261-
* Step 5: Copy meta files to the build directory
283+
* Step 6: Copy meta files to the build directory
262284
*/
263285
const pkgFiles = [
264286
'package.json',
@@ -275,12 +297,12 @@ export class Bundler {
275297
.heading('Run the following commands to start the server in production')
276298

277299
/**
278-
* Step 6: Execute build completed hook
300+
* Step 7: Execute build completed hook
279301
*/
280302
await this.#hooks.runner('buildFinished').run(this, displayMessage)
281303

282304
/**
283-
* Next steps
305+
* Display next steps
284306
*/
285307
displayMessage
286308
.add(this.ui.colors.cyan(`cd ${this.#getRelativeName(outDir)}`))

src/dev_server.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class DevServer {
125125
/**
126126
* Index generator for managing auto-generated index files
127127
*/
128-
#indexGenerator: IndexGenerator
128+
#indexGenerator!: IndexGenerator
129129

130130
/**
131131
* Hooks to execute custom actions during the dev server lifecycle
@@ -145,7 +145,7 @@ export class DevServer {
145145
/**
146146
* CLI UI instance for displaying colorful messages and progress information
147147
*/
148-
#ui = cliui()
148+
ui = cliui()
149149

150150
/**
151151
* Restarts the HTTP server and throttle concurrent calls to
@@ -188,21 +188,6 @@ export class DevServer {
188188
this.#shortcutsManager?.cleanup()
189189
}
190190

191-
/**
192-
* CLI UI instance to log colorful messages and progress information
193-
*/
194-
get ui() {
195-
return this.#ui
196-
}
197-
198-
/**
199-
* CLI UI instance to log colorful messages and progress information
200-
*/
201-
set ui(ui: ReturnType<typeof cliui>) {
202-
this.#ui = ui
203-
this.#indexGenerator.setLogger(ui.logger)
204-
}
205-
206191
/**
207192
* The mode in which the DevServer is running.
208193
*/
@@ -240,7 +225,6 @@ export class DevServer {
240225
this.cwd = cwd
241226
this.options = options
242227
this.cwdPath = string.toUnixSlash(fileURLToPath(this.cwd))
243-
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
244228
}
245229

246230
/**
@@ -458,6 +442,7 @@ export class DevServer {
458442
this.#clearScreen()
459443
this.ui.logger.info(`starting server in ${this.#mode} mode...`)
460444

445+
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
461446
this.#stickyPort = String(await getPort(this.cwd))
462447
this.#fileSystem = new FileSystem(this.cwdPath, tsConfig, this.options)
463448

src/index_generator/main.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ export class IndexGenerator {
5252
this.#cliLogger = cliLogger
5353
}
5454

55-
/**
56-
* Set the logger instance used for CLI output
57-
*
58-
* Updates the CLI logger instance for this index generator and all
59-
* registered sources to use the new logger for output.
60-
*
61-
* @param cliLogger - New logger instance to use
62-
*/
63-
setLogger(cliLogger: Logger) {
64-
this.#cliLogger = cliLogger
65-
}
66-
6755
/**
6856
* Add a new index generator source
6957
*

src/index_generator/source.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ export class IndexGeneratorSource {
122122
})
123123
}
124124

125-
/**
126-
* Set the logger instance used for CLI output
127-
*
128-
* @param cliLogger - New logger instance to use
129-
*/
130-
setLogger(cliLogger: Logger) {
131-
this.#cliLogger = cliLogger
132-
}
133-
134125
/**
135126
* Converts a recursive file tree to a string representation
136127
*

src/test_runner.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ export class TestRunner {
9898
/**
9999
* Index generator for managing auto-generated index files
100100
*/
101-
#indexGenerator: IndexGenerator
101+
#indexGenerator!: IndexGenerator
102102

103103
/**
104104
* CLI UI instance for displaying colorful messages and progress information
105105
*/
106-
#ui = cliui()
106+
ui = cliui()
107107

108108
/**
109109
* Re-runs the test child process and throttle concurrent calls to
@@ -117,21 +117,6 @@ export class TestRunner {
117117
await this.#runTests(this.#stickyPort, filters)
118118
}, 'reRunTests')
119119

120-
/**
121-
* CLI UI instance to log colorful messages and progress information
122-
*/
123-
get ui() {
124-
return this.#ui
125-
}
126-
127-
/**
128-
* CLI UI instance to log colorful messages and progress information
129-
*/
130-
set ui(ui: ReturnType<typeof cliui>) {
131-
this.#ui = ui
132-
this.#indexGenerator.setLogger(ui.logger)
133-
}
134-
135120
/**
136121
* The script file to run as a child process
137122
*/
@@ -162,7 +147,6 @@ export class TestRunner {
162147
this.cwd = cwd
163148
this.options = options
164149
this.cwdPath = string.toUnixSlash(fileURLToPath(this.cwd))
165-
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
166150
}
167151

168152
/**
@@ -417,17 +401,12 @@ export class TestRunner {
417401
*/
418402
async run() {
419403
this.#stickyPort = String(await getPort(this.cwd))
404+
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
405+
420406
this.#clearScreen()
421407

422408
this.ui.logger.info('loading hooks...')
423-
this.#hooks = await loadHooks(this.options.hooks, [
424-
'init',
425-
'testsStarting',
426-
'testsFinished',
427-
'fileAdded',
428-
'fileChanged',
429-
'fileRemoved',
430-
])
409+
this.#hooks = await loadHooks(this.options.hooks, ['init', 'testsStarting', 'testsFinished'])
431410

432411
/**
433412
* Run init hooks and clear them as they won't be executed
@@ -462,6 +441,8 @@ export class TestRunner {
462441
}
463442

464443
this.#stickyPort = String(await getPort(this.cwd))
444+
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
445+
465446
this.#fileSystem = new FileSystem(this.cwdPath, tsConfig, {
466447
...this.options,
467448
suites: this.options.suites?.filter((suite) => {

tests/bundler.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import ts from 'typescript'
1111
import { test } from '@japa/runner'
1212

13-
import { Bundler } from '../index.ts'
13+
import { Bundler, hooks } from '../index.ts'
1414

1515
test.group('Bundler', () => {
1616
test('should copy metafiles to the build directory', async ({ assert, fs }) => {
@@ -338,4 +338,42 @@ test.group('Bundler', () => {
338338

339339
await assert.dirExists('./build')
340340
})
341+
342+
test('generator index file', async ({ assert, fs }) => {
343+
await Promise.all([
344+
fs.create(
345+
'tsconfig.json',
346+
JSON.stringify({ compilerOptions: { outDir: 'build', skipLibCheck: true } })
347+
),
348+
fs.create('adonisrc.ts', 'export default { hooks: { onBuildStarting: [() => {}] } }'),
349+
fs.create('package.json', '{}'),
350+
fs.create('package-lock.json', '{}'),
351+
])
352+
353+
const bundler = new Bundler(fs.baseUrl, ts, {
354+
hooks: {
355+
init: [
356+
async () => ({
357+
default: hooks.init((_, indexGenerator) => {
358+
indexGenerator.add('controllers', {
359+
as: 'barrelFile',
360+
output: '.adonisjs/server/controllers.ts',
361+
exportName: 'controllers',
362+
source: 'app/controllers',
363+
importAlias: '#controllers',
364+
})
365+
}),
366+
}),
367+
],
368+
},
369+
})
370+
371+
bundler.ui.switchMode('raw')
372+
await bundler.bundle()
373+
374+
assert.snapshot(await fs.contents('.adonisjs/server/controllers.ts')).matchInline(`
375+
"export const controllers = {
376+
}"
377+
`)
378+
})
341379
})

0 commit comments

Comments
 (0)