From 21855e299aad305b149d657e804fa3f979f708fd Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Fri, 12 Jul 2019 12:46:04 +0200 Subject: [PATCH] feat(jsii): configure outDir and rootDir for tsc (#593) --- README.md | 2 ++ packages/jsii/lib/compiler.ts | 19 ++++++++++++++----- packages/jsii/lib/project-info.ts | 10 ++++++++++ packages/jsii/test/test.negatives.ts | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1636728535..c7d4f5a8d0 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,8 @@ jsii configuration is read from the `jsii` section in the module's __jsii-pacmak__. This is where target artifacts are emitted during packaging. Each artifact will be emitted under `/` (e.g. `dist/java`, `dist/js`, etc). Conventionally we use `"dist"` for outdir. + * `tsc` - this section allows to adjust the compiler options of the generated `tsconfig.json`. + Currently you can set `outDir` and `rootDir`. Setting `rootDir` automatically adjusts the `include` config. ### Java diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 07533c6089..80aed1252c 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -98,10 +98,11 @@ export class Compiler implements Emitter { } const tsconf = this.typescriptConfig!; + const pi = this.options.projectInfo; const prog = ts.createProgram({ rootNames: this.rootFiles.concat(_pathOfLibraries(this.compilerHost)), - options: COMPILER_OPTIONS, + options: {...pi.tsc, ...COMPILER_OPTIONS}, // Make the references absolute for the compiler projectReferences: tsconf.references && tsconf.references.map(ref => ({ path: path.resolve(ref.path) })), host: this.compilerHost @@ -115,10 +116,15 @@ export class Compiler implements Emitter { */ private async _startWatch(): Promise { return new Promise(async () => { - const projectRoot = this.options.projectInfo.projectRoot; + const pi = this.options.projectInfo; + const projectRoot = pi.projectRoot; const host = ts.createWatchCompilerHost( this.configPath, - { ...COMPILER_OPTIONS, noEmitOnError: false }, + { + ...pi.tsc, + ...COMPILER_OPTIONS, + noEmitOnError: false, + }, { ...ts.sys, getCurrentDirectory() { return projectRoot; } } ); if (!host.getDefaultLibLocation) { @@ -180,8 +186,11 @@ export class Compiler implements Emitter { composite = true; } + const pi = this.options.projectInfo; + this.typescriptConfig = { compilerOptions: { + ...pi.tsc, ...COMPILER_OPTIONS, composite, // Need to stip the `lib.` prefix and `.d.ts` suffix @@ -191,8 +200,8 @@ export class Compiler implements Emitter { target: COMPILER_OPTIONS.target && ts.ScriptTarget[COMPILER_OPTIONS.target], jsx: COMPILER_OPTIONS.jsx && Case.snake(ts.JsxEmit[COMPILER_OPTIONS.jsx]), }, - include: ["**/*.ts"], - exclude: ["node_modules"].concat(this.options.projectInfo.excludeTypescript), + include: [pi.tsc && pi.tsc.rootDir ? `${pi.tsc.rootDir}/**/*.ts` : "**/*.ts"], + exclude: ["node_modules"].concat(pi.excludeTypescript), // Change the references a little. We write 'originalpath' to the // file under the 'path' key, which is the same as what the // TypeScript compiler does. Make it relative so that the files are diff --git a/packages/jsii/lib/project-info.ts b/packages/jsii/lib/project-info.ts index 764068b8a5..e283e7eedb 100644 --- a/packages/jsii/lib/project-info.ts +++ b/packages/jsii/lib/project-info.ts @@ -10,6 +10,11 @@ const spdx: Set = require('spdx-license-list/simple'); const LOG = log4js.getLogger('jsii/package-info'); +export interface TSCompilerOptions { + readonly outDir?: string; + readonly rootDir?: string; +} + export interface ProjectInfo { readonly projectRoot: string; readonly packageJson: any; @@ -41,6 +46,7 @@ export interface ProjectInfo { readonly contributors?: ReadonlyArray; readonly excludeTypescript: string[]; readonly projectReferences?: boolean; + readonly tsc?: TSCompilerOptions; } export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies }: { fixPeerDependencies: boolean }): Promise { @@ -138,6 +144,10 @@ export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies excludeTypescript: (pkg.jsii && pkg.jsii.excludeTypescript) || [], projectReferences: pkg.jsii && pkg.jsii.projectReferences, + tsc: { + outDir: pkg.jsii && pkg.jsii.tsc && pkg.jsii.tsc.outDir, + rootDir: pkg.jsii && pkg.jsii.tsc && pkg.jsii.tsc.rootDir, + } }; } diff --git a/packages/jsii/test/test.negatives.ts b/packages/jsii/test/test.negatives.ts index 1ad8f7b536..3b4eeb8d43 100644 --- a/packages/jsii/test/test.negatives.ts +++ b/packages/jsii/test/test.negatives.ts @@ -76,6 +76,6 @@ function _makeProjectInfo(types: string): ProjectInfo { transitiveDependencies: [], bundleDependencies: {}, targets: {}, - excludeTypescript: [] + excludeTypescript: [], }; }