Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ export class Bundler {
/**
* Runs tsc command to build the source.
*/
async #runTsc(outDir: string): Promise<boolean> {
async #runTsc(args: { outDir: string; project?: string }): Promise<boolean> {
try {
await run(this.cwd, {
stdio: 'inherit',
script: 'tsc',
scriptArgs: ['--outDir', outDir],
scriptArgs: Object.entries(args).flatMap(([key, value]) =>
value ? [`--${key}`, value] : []
),
})
return true
} catch {
Expand Down Expand Up @@ -211,13 +213,17 @@ export class Bundler {
* @example
* const success = await bundler.bundle(true, 'npm')
*/
async bundle(stopOnError: boolean = true, client?: SupportedPackageManager): Promise<boolean> {
async bundle(
stopOnError: boolean = true,
client?: SupportedPackageManager,
options: { tsconfigPath?: string } = {}
): Promise<boolean> {
this.packageManager = client ?? (await this.#detectPackageManager()) ?? 'npm'

/**
* Step 1: Parse config file to get the build output directory
*/
const config = parseConfig(this.cwd, this.#ts)
const config = parseConfig(this.cwd, this.#ts, options.tsconfigPath)
if (!config) {
return false
}
Expand Down Expand Up @@ -251,7 +257,10 @@ export class Bundler {
* Step 5: Build typescript source code
*/
this.ui.logger.info('compiling typescript source', { suffix: 'tsc' })
const buildCompleted = await this.#runTsc(outDir)
const buildCompleted = await this.#runTsc({
outDir,
project: options.tsconfigPath,
})
await this.#createAceFile(outDir)

/**
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ const DEFAULT_NODE_ARGS = ['--import=@poppinss/ts-exec', '--enable-source-maps']
*/
export function parseConfig(
cwd: URL | string,
ts: typeof tsStatic
ts: typeof tsStatic,
path = 'tsconfig.json'
): tsStatic.ParsedCommandLine | undefined {
const cwdPath = typeof cwd === 'string' ? cwd : fileURLToPath(cwd)
const configFile = join(cwdPath, 'tsconfig.json')
const configFile = join(cwdPath, path)
debug('parsing config file "%s"', configFile)

let hardException: null | tsStatic.Diagnostic = null
Expand Down
34 changes: 34 additions & 0 deletions tests/bundler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,38 @@ test.group('Bundler', () => {
"
`)
})

test('use custom tsconfig for build', async ({ assert, fs }) => {
await Promise.all([
fs.create(
'tsconfig.build.json',
JSON.stringify({
compilerOptions: {
outDir: 'build',
skipLibCheck: true,
target: 'ESNext',
module: 'NodeNext',
lib: ['ESNext'],
},
})
),
fs.create('adonisrc.ts', 'export default {}'),
fs.create('index.ts', 'export default function main() {}'),
fs.createJson('package.json', { type: 'module' }),
fs.create('package-lock.json', '{}'),
])

const bundler = new Bundler(fs.baseUrl, ts, {})
bundler.ui.switchMode('raw')
await bundler.bundle(true, 'npm', {
tsconfigPath: './tsconfig.build.json',
})

await Promise.all([
assert.fileExists('./build/adonisrc.js'),
assert.fileExists('./build/index.js'),
assert.fileExists('./build/package.json'),
assert.fileExists('./build/package-lock.json'),
])
})
})