Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsii): configure outDir and rootDir for tsc #593

Merged
merged 6 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: [],
};
}