From f9a73a2c3008ebff563f170bd8559313040097df Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Wed, 10 Jul 2019 01:11:58 +0200 Subject: [PATCH 1/5] feat(jsii): configure outDIr and rootDir for tsc --- packages/jsii/lib/compiler.ts | 13 ++++++++++--- packages/jsii/lib/project-info.ts | 4 ++++ packages/jsii/test/test.negatives.ts | 4 +++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 07533c6089..f39ee5d419 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -101,7 +101,7 @@ export class Compiler implements Emitter { const prog = ts.createProgram({ rootNames: this.rootFiles.concat(_pathOfLibraries(this.compilerHost)), - options: COMPILER_OPTIONS, + options: {...COMPILER_OPTIONS, outDir: this.options.projectInfo.tscOutDir, rootDir: this.options.projectInfo.tscRootDir}, // Make the references absolute for the compiler projectReferences: tsconf.references && tsconf.references.map(ref => ({ path: path.resolve(ref.path) })), host: this.compilerHost @@ -118,7 +118,12 @@ export class Compiler implements Emitter { const projectRoot = this.options.projectInfo.projectRoot; const host = ts.createWatchCompilerHost( this.configPath, - { ...COMPILER_OPTIONS, noEmitOnError: false }, + { + ...COMPILER_OPTIONS, + noEmitOnError: false, + outDir: this.options.projectInfo.tscOutDir, + rootDir: this.options.projectInfo.tscRootDir, + }, { ...ts.sys, getCurrentDirectory() { return projectRoot; } } ); if (!host.getDefaultLibLocation) { @@ -188,10 +193,12 @@ export class Compiler implements Emitter { lib: COMPILER_OPTIONS.lib && COMPILER_OPTIONS.lib.map(name => name.slice(4, name.length - 5)), // Those int-enums, we need to output the names instead module: COMPILER_OPTIONS.module && ts.ModuleKind[COMPILER_OPTIONS.module], + outDir: this.options.projectInfo.tscOutDir, + rootDir: this.options.projectInfo.tscRootDir, target: COMPILER_OPTIONS.target && ts.ScriptTarget[COMPILER_OPTIONS.target], jsx: COMPILER_OPTIONS.jsx && Case.snake(ts.JsxEmit[COMPILER_OPTIONS.jsx]), }, - include: ["**/*.ts"], + include: [this.options.projectInfo.tscRootDir + "**/*.ts"], exclude: ["node_modules"].concat(this.options.projectInfo.excludeTypescript), // Change the references a little. We write 'originalpath' to the // file under the 'path' key, which is the same as what the diff --git a/packages/jsii/lib/project-info.ts b/packages/jsii/lib/project-info.ts index 764068b8a5..1580722c07 100644 --- a/packages/jsii/lib/project-info.ts +++ b/packages/jsii/lib/project-info.ts @@ -41,6 +41,8 @@ export interface ProjectInfo { readonly contributors?: ReadonlyArray; readonly excludeTypescript: string[]; readonly projectReferences?: boolean; + readonly tscOutDir?: string; + readonly tscRootDir?: string; } export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies }: { fixPeerDependencies: boolean }): Promise { @@ -138,6 +140,8 @@ export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies excludeTypescript: (pkg.jsii && pkg.jsii.excludeTypescript) || [], projectReferences: pkg.jsii && pkg.jsii.projectReferences, + tscOutDir: pkg.jsii && pkg.jsii.tscoutdir, + tscRootDir: pkg.jsii && pkg.jsii.tscrootdir, }; } diff --git a/packages/jsii/test/test.negatives.ts b/packages/jsii/test/test.negatives.ts index 1ad8f7b536..fb400414b6 100644 --- a/packages/jsii/test/test.negatives.ts +++ b/packages/jsii/test/test.negatives.ts @@ -76,6 +76,8 @@ function _makeProjectInfo(types: string): ProjectInfo { transitiveDependencies: [], bundleDependencies: {}, targets: {}, - excludeTypescript: [] + excludeTypescript: [], + tscOutDir: undefined, + tscRootDir: undefined, }; } From cbf1594326cfc7a4576fad5562ede7181c35009b Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Wed, 10 Jul 2019 01:31:19 +0200 Subject: [PATCH 2/5] fix: path --- packages/jsii/lib/compiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index f39ee5d419..00379b04fd 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -198,7 +198,7 @@ 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: [this.options.projectInfo.tscRootDir + "**/*.ts"], + include: [this.options.projectInfo.tscRootDir ? `${this.options.projectInfo.tscRootDir}/**/*.ts` : "**/*.ts"], exclude: ["node_modules"].concat(this.options.projectInfo.excludeTypescript), // Change the references a little. We write 'originalpath' to the // file under the 'path' key, which is the same as what the From ab4a8c5fd35e4c179133b917c262ed350ecccc1c Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Wed, 10 Jul 2019 11:01:20 +0200 Subject: [PATCH 3/5] add docs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1636728535..ed403291d8 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,10 @@ 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. + * `tscoutdir` - the output directory for the compiles `*.js` files. By default the are put + next to the TypeScript source files. (this is the `outDir` configuration in `tsconfig.json`) + * `tscrootdir` - the optional root directory for the TypeScript compiler to look for `*.ts` files. + (this is the `rootDir` configuration in `tsconfig.json`) ### Java From ac57eb777cda54ee4029d5f3f923bda256fb95f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20H=C3=B6ger?= Date: Wed, 10 Jul 2019 11:24:07 +0200 Subject: [PATCH 4/5] fix: move tsconfig options to sub struct --- README.md | 6 ++---- packages/jsii/lib/compiler.ts | 20 ++++++++++++-------- packages/jsii/lib/project-info.ts | 14 ++++++++++---- packages/jsii/test/test.negatives.ts | 2 -- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ed403291d8..c7d4f5a8d0 100644 --- a/README.md +++ b/README.md @@ -281,10 +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. - * `tscoutdir` - the output directory for the compiles `*.js` files. By default the are put - next to the TypeScript source files. (this is the `outDir` configuration in `tsconfig.json`) - * `tscrootdir` - the optional root directory for the TypeScript compiler to look for `*.ts` files. - (this is the `rootDir` configuration in `tsconfig.json`) + * `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 00379b04fd..daf7ad157a 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, outDir: this.options.projectInfo.tscOutDir, rootDir: this.options.projectInfo.tscRootDir}, + options: {...COMPILER_OPTIONS, outDir: pi.tsc && pi.tsc.outDir, rootDir: pi.tsc && pi.tsc.rootDir}, // Make the references absolute for the compiler projectReferences: tsconf.references && tsconf.references.map(ref => ({ path: path.resolve(ref.path) })), host: this.compilerHost @@ -115,14 +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, - outDir: this.options.projectInfo.tscOutDir, - rootDir: this.options.projectInfo.tscRootDir, + outDir: pi.tsc && pi.tsc.outDir, + rootDir: pi.tsc && pi.tsc.rootDir, }, { ...ts.sys, getCurrentDirectory() { return projectRoot; } } ); @@ -185,6 +187,8 @@ export class Compiler implements Emitter { composite = true; } + const pi = this.options.projectInfo; + this.typescriptConfig = { compilerOptions: { ...COMPILER_OPTIONS, @@ -193,13 +197,13 @@ export class Compiler implements Emitter { lib: COMPILER_OPTIONS.lib && COMPILER_OPTIONS.lib.map(name => name.slice(4, name.length - 5)), // Those int-enums, we need to output the names instead module: COMPILER_OPTIONS.module && ts.ModuleKind[COMPILER_OPTIONS.module], - outDir: this.options.projectInfo.tscOutDir, - rootDir: this.options.projectInfo.tscRootDir, + outDir: pi.tsc && pi.tsc.outDir, + rootDir: pi.tsc && pi.tsc.rootDir, target: COMPILER_OPTIONS.target && ts.ScriptTarget[COMPILER_OPTIONS.target], jsx: COMPILER_OPTIONS.jsx && Case.snake(ts.JsxEmit[COMPILER_OPTIONS.jsx]), }, - include: [this.options.projectInfo.tscRootDir ? `${this.options.projectInfo.tscRootDir}/**/*.ts` : "**/*.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 1580722c07..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,8 +46,7 @@ export interface ProjectInfo { readonly contributors?: ReadonlyArray; readonly excludeTypescript: string[]; readonly projectReferences?: boolean; - readonly tscOutDir?: string; - readonly tscRootDir?: string; + readonly tsc?: TSCompilerOptions; } export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies }: { fixPeerDependencies: boolean }): Promise { @@ -140,8 +144,10 @@ export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies excludeTypescript: (pkg.jsii && pkg.jsii.excludeTypescript) || [], projectReferences: pkg.jsii && pkg.jsii.projectReferences, - tscOutDir: pkg.jsii && pkg.jsii.tscoutdir, - tscRootDir: pkg.jsii && pkg.jsii.tscrootdir, + 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 fb400414b6..3b4eeb8d43 100644 --- a/packages/jsii/test/test.negatives.ts +++ b/packages/jsii/test/test.negatives.ts @@ -77,7 +77,5 @@ function _makeProjectInfo(types: string): ProjectInfo { bundleDependencies: {}, targets: {}, excludeTypescript: [], - tscOutDir: undefined, - tscRootDir: undefined, }; } From 45aa01a2b9f9dc578e77a8f42abd069738e24ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20H=C3=B6ger?= Date: Fri, 12 Jul 2019 11:56:39 +0200 Subject: [PATCH 5/5] chore: review comments --- packages/jsii/lib/compiler.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index daf7ad157a..80aed1252c 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -102,7 +102,7 @@ export class Compiler implements Emitter { const prog = ts.createProgram({ rootNames: this.rootFiles.concat(_pathOfLibraries(this.compilerHost)), - options: {...COMPILER_OPTIONS, outDir: pi.tsc && pi.tsc.outDir, rootDir: pi.tsc && pi.tsc.rootDir}, + 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 @@ -121,10 +121,9 @@ export class Compiler implements Emitter { const host = ts.createWatchCompilerHost( this.configPath, { + ...pi.tsc, ...COMPILER_OPTIONS, noEmitOnError: false, - outDir: pi.tsc && pi.tsc.outDir, - rootDir: pi.tsc && pi.tsc.rootDir, }, { ...ts.sys, getCurrentDirectory() { return projectRoot; } } ); @@ -191,14 +190,13 @@ export class Compiler implements Emitter { this.typescriptConfig = { compilerOptions: { + ...pi.tsc, ...COMPILER_OPTIONS, composite, // Need to stip the `lib.` prefix and `.d.ts` suffix lib: COMPILER_OPTIONS.lib && COMPILER_OPTIONS.lib.map(name => name.slice(4, name.length - 5)), // Those int-enums, we need to output the names instead module: COMPILER_OPTIONS.module && ts.ModuleKind[COMPILER_OPTIONS.module], - outDir: pi.tsc && pi.tsc.outDir, - rootDir: pi.tsc && pi.tsc.rootDir, target: COMPILER_OPTIONS.target && ts.ScriptTarget[COMPILER_OPTIONS.target], jsx: COMPILER_OPTIONS.jsx && Case.snake(ts.JsxEmit[COMPILER_OPTIONS.jsx]), },