Skip to content

Commit

Permalink
feat(jsii): configure outDir and rootDir for tsc (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn authored and rix0rrr committed Jul 12, 2019
1 parent 90135f9 commit 21855e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<outdir>/<target>` (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

Expand Down
19 changes: 14 additions & 5 deletions packages/jsii/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -115,10 +116,15 @@ export class Compiler implements Emitter {
*/
private async _startWatch(): Promise<never> {
return new Promise<never>(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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/jsii/lib/project-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const spdx: Set<string> = 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;
Expand Down Expand Up @@ -41,6 +46,7 @@ export interface ProjectInfo {
readonly contributors?: ReadonlyArray<spec.Person>;
readonly excludeTypescript: string[];
readonly projectReferences?: boolean;
readonly tsc?: TSCompilerOptions;
}

export async function loadProjectInfo(projectRoot: string, { fixPeerDependencies }: { fixPeerDependencies: boolean }): Promise<ProjectInfo> {
Expand Down Expand Up @@ -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,
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jsii/test/test.negatives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ function _makeProjectInfo(types: string): ProjectInfo {
transitiveDependencies: [],
bundleDependencies: {},
targets: {},
excludeTypescript: []
excludeTypescript: [],
};
}

0 comments on commit 21855e2

Please sign in to comment.