Skip to content

Commit 21855e2

Browse files
hoegertnrix0rrr
authored andcommitted
feat(jsii): configure outDir and rootDir for tsc (#593)
1 parent 90135f9 commit 21855e2

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ jsii configuration is read from the `jsii` section in the module's
281281
__jsii-pacmak__. This is where target artifacts are emitted during packaging.
282282
Each artifact will be emitted under `<outdir>/<target>` (e.g. `dist/java`,
283283
`dist/js`, etc). Conventionally we use `"dist"` for outdir.
284+
* `tsc` - this section allows to adjust the compiler options of the generated `tsconfig.json`.
285+
Currently you can set `outDir` and `rootDir`. Setting `rootDir` automatically adjusts the `include` config.
284286

285287
### Java
286288

packages/jsii/lib/compiler.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ export class Compiler implements Emitter {
9898
}
9999

100100
const tsconf = this.typescriptConfig!;
101+
const pi = this.options.projectInfo;
101102

102103
const prog = ts.createProgram({
103104
rootNames: this.rootFiles.concat(_pathOfLibraries(this.compilerHost)),
104-
options: COMPILER_OPTIONS,
105+
options: {...pi.tsc, ...COMPILER_OPTIONS},
105106
// Make the references absolute for the compiler
106107
projectReferences: tsconf.references && tsconf.references.map(ref => ({ path: path.resolve(ref.path) })),
107108
host: this.compilerHost
@@ -115,10 +116,15 @@ export class Compiler implements Emitter {
115116
*/
116117
private async _startWatch(): Promise<never> {
117118
return new Promise<never>(async () => {
118-
const projectRoot = this.options.projectInfo.projectRoot;
119+
const pi = this.options.projectInfo;
120+
const projectRoot = pi.projectRoot;
119121
const host = ts.createWatchCompilerHost(
120122
this.configPath,
121-
{ ...COMPILER_OPTIONS, noEmitOnError: false },
123+
{
124+
...pi.tsc,
125+
...COMPILER_OPTIONS,
126+
noEmitOnError: false,
127+
},
122128
{ ...ts.sys, getCurrentDirectory() { return projectRoot; } }
123129
);
124130
if (!host.getDefaultLibLocation) {
@@ -180,8 +186,11 @@ export class Compiler implements Emitter {
180186
composite = true;
181187
}
182188

189+
const pi = this.options.projectInfo;
190+
183191
this.typescriptConfig = {
184192
compilerOptions: {
193+
...pi.tsc,
185194
...COMPILER_OPTIONS,
186195
composite,
187196
// Need to stip the `lib.` prefix and `.d.ts` suffix
@@ -191,8 +200,8 @@ export class Compiler implements Emitter {
191200
target: COMPILER_OPTIONS.target && ts.ScriptTarget[COMPILER_OPTIONS.target],
192201
jsx: COMPILER_OPTIONS.jsx && Case.snake(ts.JsxEmit[COMPILER_OPTIONS.jsx]),
193202
},
194-
include: ["**/*.ts"],
195-
exclude: ["node_modules"].concat(this.options.projectInfo.excludeTypescript),
203+
include: [pi.tsc && pi.tsc.rootDir ? `${pi.tsc.rootDir}/**/*.ts` : "**/*.ts"],
204+
exclude: ["node_modules"].concat(pi.excludeTypescript),
196205
// Change the references a little. We write 'originalpath' to the
197206
// file under the 'path' key, which is the same as what the
198207
// TypeScript compiler does. Make it relative so that the files are

packages/jsii/lib/project-info.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const spdx: Set<string> = require('spdx-license-list/simple');
1010

1111
const LOG = log4js.getLogger('jsii/package-info');
1212

13+
export interface TSCompilerOptions {
14+
readonly outDir?: string;
15+
readonly rootDir?: string;
16+
}
17+
1318
export interface ProjectInfo {
1419
readonly projectRoot: string;
1520
readonly packageJson: any;
@@ -41,6 +46,7 @@ export interface ProjectInfo {
4146
readonly contributors?: ReadonlyArray<spec.Person>;
4247
readonly excludeTypescript: string[];
4348
readonly projectReferences?: boolean;
49+
readonly tsc?: TSCompilerOptions;
4450
}
4551

4652
export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies }: { fixPeerDependencies: boolean }): Promise<ProjectInfo> {
@@ -138,6 +144,10 @@ export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies
138144

139145
excludeTypescript: (pkg.jsii && pkg.jsii.excludeTypescript) || [],
140146
projectReferences: pkg.jsii && pkg.jsii.projectReferences,
147+
tsc: {
148+
outDir: pkg.jsii && pkg.jsii.tsc && pkg.jsii.tsc.outDir,
149+
rootDir: pkg.jsii && pkg.jsii.tsc && pkg.jsii.tsc.rootDir,
150+
}
141151
};
142152
}
143153

packages/jsii/test/test.negatives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ function _makeProjectInfo(types: string): ProjectInfo {
7676
transitiveDependencies: [],
7777
bundleDependencies: {},
7878
targets: {},
79-
excludeTypescript: []
79+
excludeTypescript: [],
8080
};
8181
}

0 commit comments

Comments
 (0)