Created
August 10, 2016 17:09
-
-
Save andrewconnell/b9a3c3081892ddc3bd42935691b2e114 to your computer and use it in GitHub Desktop.
Gulp Task for Building Project & Application TypeScript Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as gulp from 'gulp'; | |
import * as yargs from 'yargs'; | |
import * as merge from 'merge2'; | |
import { BaseGulpTask } from '../BaseGulpTask'; | |
import { BuildConfig, ProjectArtifact, ProjectPaths, Utils } from '../buildBarrel'; | |
import { gulpIf, gulpPlumber, gulpPrint, gulpSourcemaps, gulpTypeScript } from '../gulpPlugins'; | |
/** | |
* Builds the project, then the application. | |
* | |
* @class | |
*/ | |
export class GulpTask extends BaseGulpTask { | |
/** | |
* @property {string} description - Help description for the task. | |
*/ | |
public description: string = 'Builds the project and/or application.'; | |
/** | |
* @property {string[]} aliases - Different options to run the task. | |
*/ | |
public aliases: string[] = ['b']; | |
/** | |
* @property {string[]} dependencies - Array of all tasks that should be run before this one. | |
*/ | |
public dependencies: string[] = []; | |
/** | |
* @property {Object} options - Any command line flags that can be passed to the task. | |
*/ | |
public options: any = { | |
'app': 'Build application code', | |
'project': 'Build project build code (gulp)' | |
}; | |
/** | |
* Gulp task logic. | |
* | |
* @param {gulp.TaskCallback} done - callback when task completes | |
* @returns void | |
*/ | |
public ExecuteTask(done: gulp.TaskCallback): void { | |
// get command line args | |
let args: ICommandLineArgs = <ICommandLineArgs>yargs.argv; | |
// if no parameters supplied, default to all | |
if (!args.app && !args.project) { | |
args.app = args.project = true; | |
} | |
let build: (description: string, tsProject: gulpTypeScript.Project, outputPath: string) | |
=> void = (description: string, tsProject: gulpTypeScript.Project, outputPath: string) => { | |
Utils.log(description); | |
// set the sourcemap write options if building app | |
let writeOptions: gulpSourcemaps.WriteOptions = null; | |
if (args.app) { | |
writeOptions = <gulpSourcemaps.WriteOptions>{ | |
// inline the source typescript into the sourcemap | |
includeContent: true, | |
// point to the root of the project | |
sourceRoot: function (file: any): string { | |
return BuildConfig.GetAbsolutePath(ProjectArtifact.Source); | |
} | |
}; | |
} | |
// compile all TypeScript files, including sourcemaps inline in the generated JavaScript | |
let tsResult: any = tsProject.src() | |
.pipe(gulpPlumber()) | |
.pipe(gulpIf(args.verbose, gulpPrint())) | |
.pipe(gulpSourcemaps.init()) | |
.pipe(gulpTypeScript(tsProject)); | |
// merge dts & js output streams... | |
return merge([ | |
// type definitions | |
tsResult.dts | |
.pipe(gulp.dest(outputPath)), | |
// javascript | |
tsResult.js | |
.pipe(gulpSourcemaps.write(writeOptions)) | |
.pipe(gulp.dest(outputPath)) | |
]); | |
}; | |
// build project | |
if (args.project) { | |
let tsProjectProject: gulpTypeScript.Project = gulpTypeScript.createProject('tsconfig.build.json'); | |
build('Building the project', tsProjectProject, ''); | |
} | |
// build app | |
if (args.app) { | |
// setup parameters for gulp-typescript | |
let tsProjectParams: gulpTypeScript.Params = { | |
declaration: true, | |
noExternalResolve: true | |
}; | |
// load typescript project | |
let tsProjectApp: gulpTypeScript.Project = gulpTypeScript.createProject('tsconfig.json', tsProjectParams); | |
// change default excludes | |
// > default excludes are for build TS files... excludes /src | |
// > need the inverse so exlude the TS build files & include /src, /typings /etc | |
tsProjectApp.config.exclude = [ | |
'gulpfile.ts', | |
'wallaby.conf.ts', | |
ProjectPaths.Build, | |
ProjectPaths.NodeModules, | |
ProjectPaths.Output, | |
ProjectPaths.Reports | |
]; | |
build('Building the application', tsProjectApp, ProjectPaths.Output); | |
} | |
done(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! But how run gulp-tasks written in typescript?