@@ -45,6 +45,17 @@ export interface ITypeScriptTaskConfig {
4545 /* tslint:disable:no-any */
4646 typescript ?: any ;
4747 /* tslint:enable:no-any */
48+
49+ /**
50+ * Removes comments from all generated `.js` files. Will **not** remove comments from generated `.d.ts` files.
51+ * Defaults to false.
52+ */
53+ removeCommentsFromJavaScript ?: boolean ;
54+
55+ /**
56+ * If true, creates sourcemap files which are useful for debugging. Defaults to true.
57+ */
58+ emitSourceMaps ?: boolean ;
4859}
4960
5061export class TypeScriptTask extends GulpTask < ITypeScriptTaskConfig > {
@@ -82,22 +93,25 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
8293 'src/**/*.js' ,
8394 'src/**/*.json' ,
8495 'src/**/*.jsx'
85- ]
96+ ] ,
97+ removeCommentsFromJavaScript : false ,
98+ emitSourceMaps : true
8699 } ;
87100
88101 private _tsProject : ts . Project ;
89102
90103 public executeTask ( gulp : gulpType . Gulp , completeCallback : ( result ?: string ) => void ) : void {
91104 /* tslint:disable:typedef */
92- const plumber = require ( 'gulp-plumber' ) ;
93- const sourcemaps = require ( 'gulp-sourcemaps' ) ;
94105 const assign = require ( 'object-assign' ) ;
95106 const merge = require ( 'merge2' ) ;
96107 /* tslint:enable:typedef */
97108
98- let errorCount : number = 0 ;
99109 const allStreams : NodeJS . ReadWriteStream [ ] = [ ] ;
100110
111+ const result : { errorCount : number } = {
112+ errorCount : 0
113+ } ;
114+
101115 /* tslint:disable:no-any */
102116 let tsConfig : any = this . readJSONSync ( 'tsconfig.json' ) ;
103117 /* tslint:enable:no-any */
@@ -121,65 +135,32 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
121135 this . log ( `Using custom version: ${ this . taskConfig . typescript . version } ` ) ;
122136 }
123137
124- const tsCompilerOptions : ts . Project = assign ( { } , tsConfig . compilerOptions , {
138+ const tsCompilerOptions : ts . Settings = assign ( { } , tsConfig . compilerOptions , {
125139 module : 'commonjs' ,
126140 typescript : this . taskConfig . typescript
127141 } ) ;
128142
129- const tsProject : ts . Project = this . _tsProject = this . _tsProject || ts . createProject ( tsCompilerOptions ) ;
130-
131- /* tslint:disable:typedef */
132- const { libFolder, libAMDFolder } = this . buildConfig ;
133- /* tslint:enable:typedef */
134- let tsResult : ts . CompileStream = gulp . src ( this . taskConfig . sourceMatch )
135- . pipe ( plumber ( {
136- errorHandler : ( ) : void => {
137- errorCount ++ ;
138- }
139- } ) )
140- . pipe ( sourcemaps . init ( ) )
141- . pipe ( ts ( tsProject , undefined , this . taskConfig . reporter ) ) ;
142-
143- allStreams . push ( tsResult . js
144- . pipe ( sourcemaps . write ( '.' , { sourceRoot : this . _resolveSourceMapRoot } ) )
145- . pipe ( gulp . dest ( libFolder ) ) ) ;
146-
147- allStreams . push ( tsResult . dts . pipe ( gulp . dest ( libFolder ) ) ) ;
143+ this . _compileProject ( gulp , tsCompilerOptions , this . buildConfig . libFolder , allStreams , result ) ;
148144
149145 // Static passthrough files.
150146 const staticSrc : NodeJS . ReadWriteStream = gulp . src ( this . taskConfig . staticMatch ) ;
151147
152148 allStreams . push (
153- staticSrc . pipe ( gulp . dest ( libFolder ) ) ) ;
149+ staticSrc . pipe ( gulp . dest ( this . buildConfig . libFolder ) ) ) ;
154150
155151 // If AMD modules are required, also build that.
156- if ( libAMDFolder ) {
152+ if ( this . buildConfig . libAMDFolder ) {
157153 allStreams . push (
158- staticSrc . pipe ( gulp . dest ( libAMDFolder ) ) ) ;
154+ staticSrc . pipe ( gulp . dest ( this . buildConfig . libAMDFolder ) ) ) ;
159155
160156 const tsAMDProject : ts . Project = ts . createProject ( assign ( { } , tsCompilerOptions , { module : 'amd' } ) ) ;
161-
162- tsResult = gulp . src ( this . taskConfig . sourceMatch )
163- . pipe ( plumber ( {
164- errorHandler : ( ) : void => {
165- errorCount ++ ;
166- }
167- } ) )
168- . pipe ( sourcemaps . write ( { sourceRoot : this . _resolveSourceMapRoot } ) )
169- . pipe ( ts ( tsAMDProject , undefined , this . taskConfig . reporter ) ) ;
170-
171- allStreams . push (
172- tsResult . js
173- . pipe ( sourcemaps . write ( '.' , { sourceRoot : this . _resolveSourceMapRoot } ) )
174- . pipe ( gulp . dest ( libAMDFolder ) ) ) ;
175-
176- allStreams . push ( tsResult . dts . pipe ( gulp . dest ( libAMDFolder ) ) ) ;
157+ this . _compileProject ( gulp , tsAMDProject , this . buildConfig . libAMDFolder , allStreams , result ) ;
177158 }
178159
179160 // Listen for pass/fail, and ensure that the task passes/fails appropriately.
180161 merge ( allStreams )
181162 . on ( 'queueDrain' , ( ) => {
182- if ( this . taskConfig . failBuildOnErrors && errorCount ) {
163+ if ( this . taskConfig . failBuildOnErrors && result . errorCount ) {
183164 completeCallback ( 'TypeScript error(s) occurred.' ) ;
184165 } else {
185166 completeCallback ( ) ;
@@ -193,6 +174,47 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
193174 throw 'Do not use mergeConfig with gulp-core-build-typescript' ;
194175 }
195176
177+ private _compileProject ( gulp : gulpType . Gulp , tsCompilerOptions : ts . Settings , destDir : string ,
178+ allStreams : NodeJS . ReadWriteStream [ ] , result : { errorCount : number } ) : void {
179+ /* tslint:disable:typedef */
180+ const plumber = require ( 'gulp-plumber' ) ;
181+ const sourcemaps = require ( 'gulp-sourcemaps' ) ;
182+ /* tslint:enable:typedef */
183+
184+ const tsProject : ts . Project = this . _tsProject = this . _tsProject || ts . createProject ( tsCompilerOptions ) ;
185+
186+ // tslint:disable-next-line:no-any
187+ let tsResult : any = gulp . src ( this . taskConfig . sourceMatch )
188+ . pipe ( plumber ( {
189+ errorHandler : ( ) : void => {
190+ result . errorCount ++ ;
191+ }
192+ } ) ) ;
193+
194+ if ( this . taskConfig . emitSourceMaps ) {
195+ tsResult = tsResult . pipe ( sourcemaps . init ( ) ) ;
196+ }
197+
198+ tsResult = tsResult
199+ . pipe ( ts ( tsProject , undefined , this . taskConfig . reporter ) ) ;
200+
201+ // tslint:disable-next-line:typedef
202+ let jsResult = ( this . taskConfig . removeCommentsFromJavaScript
203+ ? tsResult . js . pipe ( require ( 'gulp-decomment' ) ( {
204+ space : ! ! this . taskConfig . emitSourceMaps /* turn comments into spaces to preserve sourcemaps */
205+ } ) )
206+ : tsResult . js ) ;
207+
208+ if ( this . taskConfig . emitSourceMaps ) {
209+ jsResult = jsResult . pipe ( sourcemaps . write ( '.' , { sourceRoot : this . _resolveSourceMapRoot } ) ) ;
210+ }
211+
212+ allStreams . push ( jsResult
213+ . pipe ( gulp . dest ( destDir ) ) ) ;
214+
215+ allStreams . push ( tsResult . dts . pipe ( gulp . dest ( destDir ) ) ) ;
216+ }
217+
196218 private _resolveSourceMapRoot ( file : { relative : string , cwd : string } ) : string {
197219 return path . relative ( file . relative , path . join ( file . cwd , 'src' ) ) ;
198220 }
0 commit comments