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 4 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
21 changes: 16 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: {...COMPILER_OPTIONS, outDir: pi.tsc && pi.tsc.outDir, rootDir: pi.tsc && pi.tsc.rootDir},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be (since we decided to use the same name...):

{ ...pi.tsc, ...COMPILER_OPTIONS }

The order is important here - we don't want pi.tsc to be able to override the required settings we have in COMPILER_OPTIONS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now this would work, but if someone adds rootDir or outDir to COMPILER_OPTIONS manual overrides would no longer be possible. But I am fine with the change.

// 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,16 @@ 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 },
{
...COMPILER_OPTIONS,
noEmitOnError: false,
outDir: pi.tsc && pi.tsc.outDir,
rootDir: pi.tsc && pi.tsc.rootDir,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, here I'd just ...pi.tsc before ...COMPILER_OPTIONS.

},
{ ...ts.sys, getCurrentDirectory() { return projectRoot; } }
);
if (!host.getDefaultLibLocation) {
Expand Down Expand Up @@ -180,6 +187,8 @@ export class Compiler implements Emitter {
composite = true;
}

const pi = this.options.projectInfo;

this.typescriptConfig = {
compilerOptions: {
...COMPILER_OPTIONS,
Expand All @@ -188,11 +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: pi.tsc && pi.tsc.outDir,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here :)

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: ["**/*.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: [],
};
}