Skip to content
Merged
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
16 changes: 14 additions & 2 deletions commands/Build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import hasYarn from 'has-yarn'
import { BaseCommand, flags } from '@adonisjs/core/build/standalone'
import { TSCONFIG_FILE_NAME } from '../config/paths'

/**
* Compile typescript project Javascript
Expand Down Expand Up @@ -42,6 +43,15 @@ export default class Build extends BaseCommand {
})
public ignoreTsErrors: boolean

/**
* Path to the TypeScript project configuration file. Defaults to "tsconfig.json"
*/
@flags.string({
description: 'Path to the TypeScript project configuration file',
default: TSCONFIG_FILE_NAME,
})
public tsconfig: string

/**
* Arguments to pass to the `encore` binary
*/
Expand Down Expand Up @@ -84,14 +94,16 @@ export default class Build extends BaseCommand {
this.application.appRoot,
this.encoreArgs,
this.assets,
this.logger
this.logger,
this.tsconfig
).compileForProduction(stopOnError, this.client)
} else {
await new Compiler(
this.application.appRoot,
this.encoreArgs,
this.assets,
this.logger
this.logger,
this.tsconfig
).compile(stopOnError)
}
} catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Compiler {
/**
* Reference to typescript compiler
*/
private ts = new Ts(this.appRoot, this.logger)
private ts: Ts

/**
* Reference to rc File
Expand All @@ -40,8 +40,10 @@ export class Compiler {
public appRoot: string,
private encoreArgs: string[],
private buildAssets: boolean,
private logger: typeof uiLogger = uiLogger
private logger: typeof uiLogger = uiLogger,
tsconfig?: string
) {
this.ts = new Ts(this.appRoot, this.logger, tsconfig)
this.ts.tsCompiler.use(() => {
return iocTransformer(this.ts.tsCompiler.ts, this.rcFile.application.rcFile)
}, 'after')
Expand Down
12 changes: 8 additions & 4 deletions src/Ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ export class Ts {
*/
public tsCompiler = new TypescriptCompiler(
this.appRoot,
TSCONFIG_FILE_NAME,
this.tsconfig,
require(resolveFrom(this.appRoot, 'typescript/lib/typescript'))
)

constructor(private appRoot: string, private logger: typeof uiLogger) {}
constructor(
private appRoot: string,
private logger: typeof uiLogger,
private tsconfig = TSCONFIG_FILE_NAME
) {}

/**
* Render ts diagnostics
Expand All @@ -44,13 +48,13 @@ export class Ts {
const { error, config } = this.tsCompiler.configParser().parse()

if (error) {
this.logger.error(`unable to parse ${TSCONFIG_FILE_NAME}`)
this.logger.error(`unable to parse ${this.tsconfig}`)
this.renderDiagnostics([error], this.tsCompiler.ts.createCompilerHost({}))
return
}

if (config && config.errors.length) {
this.logger.error(`unable to parse ${TSCONFIG_FILE_NAME}`)
this.logger.error(`unable to parse ${this.tsconfig}`)
this.renderDiagnostics(config.errors, this.tsCompiler.ts.createCompilerHost(config.options))
return
}
Expand Down
51 changes: 51 additions & 0 deletions test/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,55 @@ test.group('Compiler', (group) => {

assert.isFalse(require(join(fs.basePath, 'build', '.adonisrc.json')).typescript)
}).timeout(0)

test('build should support custom tsconfig file', async (assert) => {
await fs.add(
'.adonisrc.json',
JSON.stringify({
typescript: true,
})
)

await fs.add(
'package.json',
JSON.stringify({
name: 'my-dummy-app',
dependencies: {},
})
)

await fs.add(
'tsconfig.json',
JSON.stringify({
include: ['**/*'],
exclude: ['build'],
compilerOptions: {
outDir: 'build',
},
})
)

await fs.add(
'tsconfig.production.json',
JSON.stringify({
extends: './tsconfig.json',
exclude: ['build', 'src/ignored.ts'],
})
)

await fs.add('ace', '')
await fs.add('src/foo.ts', '')
await fs.add('src/ignored.ts', '')

const compiler = new Compiler(fs.basePath, [], false, ui.logger, 'tsconfig.production.json')
await compiler.compileForProduction(false, 'npm')

const hasFiles = await Promise.all(
['build/.adonisrc.json', 'build/src/foo.js', 'build/src/ignored.js'].map((file) =>
fs.fsExtra.pathExists(join(fs.basePath, file))
)
)

assert.deepEqual(hasFiles, [true, true, false])
}).timeout(0)
})