Skip to content

Commit 00ef75d

Browse files
author
nickpape-msft
authored
Merge pull request microsoft#34 from Microsoft/nickpape/inject-ts-compiler-config
Allow us to inject compiler options and also control where the files get written
2 parents 8121a6c + 92f0395 commit 00ef75d

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build-typescript",
5+
"comment": "Allow the injection of compiler options and allow control over where generated TS files are written.",
6+
"type": "minor"
7+
}
8+
],
9+
"email": "nickpape@microsoft.com"
10+
}

gulp-core-build-typescript/src/TypeScriptTask.ts

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import gulpType = require('gulp');
33
import ts = require('gulp-typescript');
44
import * as path from 'path';
55

6+
import { IBuildConfig } from '@microsoft/gulp-core-build';
7+
68
interface ITypeScriptErrorObject {
79
diagnostic: {
810
messageText: string | { messageText: string };
@@ -17,12 +19,17 @@ interface ITypeScriptErrorObject {
1719
};
1820
}
1921

22+
/** Includes the experimental stripInternal feature */
23+
export interface ICompilerOptions extends ts.Settings {
24+
stripInternal?: boolean;
25+
}
26+
2027
export interface ITypeScriptTaskConfig {
2128
/**
2229
* Fails the build when errors occur.
2330
* @default true
2431
*/
25-
failBuildOnErrors: boolean;
32+
failBuildOnErrors?: boolean;
2633

2734
/**
2835
* Glob matches for files to be included in the build.
@@ -46,6 +53,11 @@ export interface ITypeScriptTaskConfig {
4653
typescript?: any;
4754
/* tslint:enable:no-any */
4855

56+
/**
57+
* Compiler options. Overrides values from the tsconfig.json
58+
*/
59+
compilerOptions?: ICompilerOptions;
60+
4961
/**
5062
* Removes comments from all generated `.js` files. Will **not** remove comments from generated `.d.ts` files.
5163
* Defaults to false.
@@ -56,6 +68,16 @@ export interface ITypeScriptTaskConfig {
5668
* If true, creates sourcemap files which are useful for debugging. Defaults to true.
5769
*/
5870
emitSourceMaps?: boolean;
71+
72+
/**
73+
* The directory to write the compiled javascript and typings files to. Defaults to buildConfig.libFolder
74+
*/
75+
libDir?: string;
76+
77+
/**
78+
* If defined, drop typescript files from an AMD build here. Defaults to buildConfig.libAMDFolder
79+
*/
80+
libAMDDir?: string;
5981
}
6082

6183
export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
@@ -96,7 +118,10 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
96118
'src/**/*.jsx'
97119
],
98120
removeCommentsFromJavaScript: false,
99-
emitSourceMaps: true
121+
emitSourceMaps: true,
122+
compilerOptions: {},
123+
libDir: undefined,
124+
libAMDDir: undefined
100125
};
101126

102127
private _tsProject: ts.Project;
@@ -132,33 +157,40 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
132157
};
133158
}
134159

160+
this._normalizeConfig();
161+
135162
// Log the compiler version for custom verisons.
136163
if (this.taskConfig.typescript && this.taskConfig.typescript.version) {
137164
this.log(`Using custom version: ${this.taskConfig.typescript.version}`);
138165
}
139166

140-
const tsCompilerOptions: ts.Settings = assign({}, tsConfig.compilerOptions, {
141-
module: 'commonjs',
142-
typescript: this.taskConfig.typescript
143-
});
167+
let compilerOptions: ICompilerOptions = assign(
168+
{},
169+
tsConfig.compilerOptions,
170+
{
171+
module: 'commonjs',
172+
typescript: this.taskConfig.typescript
173+
},
174+
this.taskConfig.compilerOptions
175+
);
144176

145-
this._tsProject = this._tsProject || ts.createProject(tsCompilerOptions);
177+
this._tsProject = this._tsProject || ts.createProject(compilerOptions);
146178

147-
this._compileProject(gulp, this._tsProject, this.buildConfig.libFolder, allStreams, result);
179+
this._compileProject(gulp, this._tsProject, this.taskConfig.libDir, allStreams, result);
148180

149181
// Static passthrough files.
150182
const staticSrc: NodeJS.ReadWriteStream = gulp.src(this.taskConfig.staticMatch);
151183

152184
allStreams.push(
153-
staticSrc.pipe(gulp.dest(this.buildConfig.libFolder)));
185+
staticSrc.pipe(gulp.dest(this.taskConfig.libDir)));
154186

155187
// If AMD modules are required, also build that.
156-
if (this.buildConfig.libAMDFolder) {
188+
if (this.taskConfig.libAMDDir) {
157189
allStreams.push(
158-
staticSrc.pipe(gulp.dest(this.buildConfig.libAMDFolder)));
190+
staticSrc.pipe(gulp.dest(this.taskConfig.libAMDDir)));
159191

160-
this._tsAMDProject = this._tsAMDProject || ts.createProject(assign({}, tsCompilerOptions, { module: 'amd' }));
161-
this._compileProject(gulp, this._tsAMDProject, this.buildConfig.libAMDFolder, allStreams, result);
192+
this._tsAMDProject = this._tsAMDProject || ts.createProject(assign({}, compilerOptions, { module: 'amd' }));
193+
this._compileProject(gulp, this._tsAMDProject, this.taskConfig.libAMDDir, allStreams, result);
162194
}
163195

164196
// Listen for pass/fail, and ensure that the task passes/fails appropriately.
@@ -173,11 +205,32 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
173205
.on('error', completeCallback);
174206
}
175207

208+
public getCleanMatch(buildConfig: IBuildConfig, taskConfig: ITypeScriptTaskConfig = this.taskConfig): string[] {
209+
this._normalizeConfig(buildConfig);
210+
const cleanMatch: string[] = [
211+
this.taskConfig.libDir
212+
];
213+
if (this.taskConfig.libAMDDir) {
214+
cleanMatch.push(this.taskConfig.libAMDDir);
215+
}
216+
return cleanMatch;
217+
}
218+
176219
/** Override the new mergeConfig API */
177220
public mergeConfig(config: ITypeScriptTaskConfig): void {
178221
throw 'Do not use mergeConfig with gulp-core-build-typescript';
179222
}
180223

224+
private _normalizeConfig(buildConfig: IBuildConfig = this.buildConfig): void {
225+
if (!this.taskConfig.libDir) {
226+
this.taskConfig.libDir = buildConfig.libFolder;
227+
}
228+
229+
if (!this.taskConfig.libAMDDir) {
230+
this.taskConfig.libAMDDir = buildConfig.libAMDFolder;
231+
}
232+
}
233+
181234
private _compileProject(gulp: gulpType.Gulp, tsProject: ts.Project, destDir: string,
182235
allStreams: NodeJS.ReadWriteStream[], result: { errorCount: number }): void {
183236
/* tslint:disable:typedef */

gulp-core-build-typescript/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { TextTask } from './TextTask';
44
import { RemoveTripleSlashReferenceTask } from './RemoveTripleSlashReferenceTask';
55
import { IExecutable, parallel, serial } from '@microsoft/gulp-core-build';
66

7+
export { TypeScriptTask } from './TypeScriptTask';
8+
79
export const typescript: TypeScriptTask = new TypeScriptTask();
810
export const tslint: TSLintTask = new TSLintTask();
911
export const text: TextTask = new TextTask();

0 commit comments

Comments
 (0)