Skip to content

Commit 224faba

Browse files
authored
feat: allow custom tsconfig path for bundle (#94)
1 parent 1c654cb commit 224faba

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

src/bundler.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,14 @@ export class Bundler {
141141
/**
142142
* Runs tsc command to build the source.
143143
*/
144-
async #runTsc(outDir: string): Promise<boolean> {
144+
async #runTsc(args: { outDir: string; project?: string }): Promise<boolean> {
145145
try {
146146
await run(this.cwd, {
147147
stdio: 'inherit',
148148
script: 'tsc',
149-
scriptArgs: ['--outDir', outDir],
149+
scriptArgs: Object.entries(args).flatMap(([key, value]) =>
150+
value ? [`--${key}`, value] : []
151+
),
150152
})
151153
return true
152154
} catch {
@@ -211,13 +213,17 @@ export class Bundler {
211213
* @example
212214
* const success = await bundler.bundle(true, 'npm')
213215
*/
214-
async bundle(stopOnError: boolean = true, client?: SupportedPackageManager): Promise<boolean> {
216+
async bundle(
217+
stopOnError: boolean = true,
218+
client?: SupportedPackageManager,
219+
options: { tsconfigPath?: string } = {}
220+
): Promise<boolean> {
215221
this.packageManager = client ?? (await this.#detectPackageManager()) ?? 'npm'
216222

217223
/**
218224
* Step 1: Parse config file to get the build output directory
219225
*/
220-
const config = parseConfig(this.cwd, this.#ts)
226+
const config = parseConfig(this.cwd, this.#ts, options.tsconfigPath)
221227
if (!config) {
222228
return false
223229
}
@@ -251,7 +257,10 @@ export class Bundler {
251257
* Step 5: Build typescript source code
252258
*/
253259
this.ui.logger.info('compiling typescript source', { suffix: 'tsc' })
254-
const buildCompleted = await this.#runTsc(outDir)
260+
const buildCompleted = await this.#runTsc({
261+
outDir,
262+
project: options.tsconfigPath,
263+
})
255264
await this.#createAceFile(outDir)
256265

257266
/**

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ const DEFAULT_NODE_ARGS = ['--import=@poppinss/ts-exec', '--enable-source-maps']
4848
*/
4949
export function parseConfig(
5050
cwd: URL | string,
51-
ts: typeof tsStatic
51+
ts: typeof tsStatic,
52+
path = 'tsconfig.json'
5253
): tsStatic.ParsedCommandLine | undefined {
5354
const cwdPath = typeof cwd === 'string' ? cwd : fileURLToPath(cwd)
54-
const configFile = join(cwdPath, 'tsconfig.json')
55+
const configFile = join(cwdPath, path)
5556
debug('parsing config file "%s"', configFile)
5657

5758
let hardException: null | tsStatic.Diagnostic = null

tests/bundler.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,38 @@ test.group('Bundler', () => {
376376
"
377377
`)
378378
})
379+
380+
test('use custom tsconfig for build', async ({ assert, fs }) => {
381+
await Promise.all([
382+
fs.create(
383+
'tsconfig.build.json',
384+
JSON.stringify({
385+
compilerOptions: {
386+
outDir: 'build',
387+
skipLibCheck: true,
388+
target: 'ESNext',
389+
module: 'NodeNext',
390+
lib: ['ESNext'],
391+
},
392+
})
393+
),
394+
fs.create('adonisrc.ts', 'export default {}'),
395+
fs.create('index.ts', 'export default function main() {}'),
396+
fs.createJson('package.json', { type: 'module' }),
397+
fs.create('package-lock.json', '{}'),
398+
])
399+
400+
const bundler = new Bundler(fs.baseUrl, ts, {})
401+
bundler.ui.switchMode('raw')
402+
await bundler.bundle(true, 'npm', {
403+
tsconfigPath: './tsconfig.build.json',
404+
})
405+
406+
await Promise.all([
407+
assert.fileExists('./build/adonisrc.js'),
408+
assert.fileExists('./build/index.js'),
409+
assert.fileExists('./build/package.json'),
410+
assert.fileExists('./build/package-lock.json'),
411+
])
412+
})
379413
})

0 commit comments

Comments
 (0)