feature(tsc-wrapped): accept any tsc command line option (#13471)
This commit is contained in:
parent
5047d9780d
commit
f816319e41
|
@ -23,7 +23,8 @@ export type CodegenExtension =
|
|||
Promise<void>;
|
||||
|
||||
export function main(
|
||||
project: string, cliOptions: CliOptions, codegen?: CodegenExtension): Promise<any> {
|
||||
project: string, cliOptions: CliOptions, codegen?: CodegenExtension,
|
||||
options?: ts.CompilerOptions): Promise<any> {
|
||||
try {
|
||||
let projectDir = project;
|
||||
if (fs.lstatSync(project).isFile()) {
|
||||
|
@ -34,7 +35,7 @@ export function main(
|
|||
const basePath = path.resolve(process.cwd(), cliOptions.basePath || projectDir);
|
||||
|
||||
// read the configuration options from wherever you store them
|
||||
const {parsed, ngOptions} = tsc.readConfiguration(project, basePath);
|
||||
const {parsed, ngOptions} = tsc.readConfiguration(project, basePath, options);
|
||||
ngOptions.basePath = basePath;
|
||||
const createProgram = (host: ts.CompilerHost, oldProgram?: ts.Program) =>
|
||||
ts.createProgram(parsed.fileNames, parsed.options, host, oldProgram);
|
||||
|
@ -111,10 +112,15 @@ export function main(
|
|||
|
||||
// CLI entry point
|
||||
if (require.main === module) {
|
||||
const args = require('minimist')(process.argv.slice(2));
|
||||
const project = args.p || args.project || '.';
|
||||
const cliOptions = new CliOptions(args);
|
||||
main(project, cliOptions).then((exitCode: any) => process.exit(exitCode)).catch((e: any) => {
|
||||
const args = process.argv.slice(2);
|
||||
let {options, fileNames, errors} = (ts as any).parseCommandLine(args);
|
||||
check(errors);
|
||||
const project = options.project || '.';
|
||||
// TODO(alexeagle): command line should be TSC-compatible, remove "CliOptions" here
|
||||
const cliOptions = new CliOptions(require('minimist')(args));
|
||||
main(project, cliOptions, null, options)
|
||||
.then((exitCode: any) => process.exit(exitCode))
|
||||
.catch((e: any) => {
|
||||
console.error(e.stack);
|
||||
console.error('Compilation failed');
|
||||
process.exit(1);
|
||||
|
|
|
@ -18,7 +18,7 @@ import AngularCompilerOptions from './options';
|
|||
* you should implement a similar interface.
|
||||
*/
|
||||
export interface CompilerInterface {
|
||||
readConfiguration(project: string, basePath: string):
|
||||
readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions):
|
||||
{parsed: ts.ParsedCommandLine, ngOptions: AngularCompilerOptions};
|
||||
typeCheck(compilerHost: ts.CompilerHost, program: ts.Program): void;
|
||||
emit(program: ts.Program): number;
|
||||
|
@ -97,7 +97,7 @@ export class Tsc implements CompilerInterface {
|
|||
|
||||
constructor(private readFile = ts.sys.readFile, private readDirectory = ts.sys.readDirectory) {}
|
||||
|
||||
readConfiguration(project: string, basePath: string) {
|
||||
readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions) {
|
||||
this.basePath = basePath;
|
||||
|
||||
// Allow a directory containing tsconfig.json as the project value
|
||||
|
@ -123,7 +123,7 @@ export class Tsc implements CompilerInterface {
|
|||
fileExists: existsSync,
|
||||
readDirectory: this.readDirectory
|
||||
};
|
||||
this.parsed = ts.parseJsonConfigFileContent(config, host, basePath);
|
||||
this.parsed = ts.parseJsonConfigFileContent(config, host, basePath, existingOptions);
|
||||
|
||||
check(this.parsed.errors);
|
||||
|
||||
|
|
|
@ -25,14 +25,16 @@ describe('options parsing', () => {
|
|||
() => ['tsconfig.json']);
|
||||
|
||||
it('should combine all options into ngOptions', () => {
|
||||
const {parsed, ngOptions} = tsc.readConfiguration('projectDir', 'basePath');
|
||||
const {parsed, ngOptions} =
|
||||
tsc.readConfiguration('projectDir', 'basePath', {target: ts.ScriptTarget.ES2015});
|
||||
|
||||
expect(ngOptions).toEqual({
|
||||
genDir: 'basePath',
|
||||
googleClosureOutput: true,
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
outDir: 'basePath/built',
|
||||
configFilePath: undefined
|
||||
configFilePath: undefined,
|
||||
target: ts.ScriptTarget.ES2015
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue