Last active
June 17, 2017 12:23
-
-
Save pyrho/d3916d33fe8415f5f181668afe69b763 to your computer and use it in GitHub Desktop.
Simple gulpfile for a typescript project, including sourcemaps support and mocha (support material for https://25.wf/programing/2017/06/13/gulp-and-typescript/)
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
const gulp = require('gulp'); | |
const ts = require('gulp-typescript'); | |
const sourcemaps = require('gulp-sourcemaps'); | |
const mocha = require('gulp-mocha'); | |
const del = require('del'); | |
const gutil = require('gulp-util'); | |
const config = { | |
outputDirectory: './build', | |
inputSourceFiles: [ | |
'./src/**/*.ts', | |
], | |
inputTestFiles: [ | |
'./test/**/*.ts', | |
], | |
tsConfigPath: 'tsconfig.json', | |
}; | |
const typescriptProjectSource = ts.createProject(config.tsConfigPath, { | |
"sourceMap": false | |
}); | |
const typescriptProjectTest = ts.createProject(config.tsConfigPath, { | |
"sourceMap": false | |
}); | |
// Helpers {{{ | |
function onErrorHandler(error) { | |
this.emit('end'); | |
} | |
// }}} | |
// Gulp tasks {{{ | |
// As recommended by https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md | |
gulp.task('cleanTest', () => | |
del([ | |
`${config.outputDirectory}/test`, | |
]) | |
); | |
gulp.task('compileTest', ['cleanTest'], () => gulp | |
.src(config.inputTestFiles) | |
.pipe(sourcemaps.init()) | |
.pipe(typescriptProjectTest()) | |
.on('error', onErrorHandler) | |
.pipe(sourcemaps.write('.', { | |
sourceRoot: function (file) { | |
return file.cwd + '/test'; | |
} | |
})) | |
.pipe(gulp.dest(config.outputDirectory + '/test')) | |
); | |
gulp.task('cleanSrc', () => | |
del([ | |
`${config.outputDirectory}/src`, | |
]) | |
); | |
gulp.task('compileSrc', ['cleanSrc'], () => gulp | |
.src(config.inputSourceFiles) | |
.pipe(sourcemaps.init()) | |
.pipe(typescriptProjectSource(ts.reporter.fullReporter(true))) | |
.on('error', gutil.log) | |
.pipe(sourcemaps.write('.', { | |
sourceRoot: function (file) { | |
return file.cwd + '/src'; | |
} | |
})) | |
.pipe(gulp.dest(config.outputDirectory + '/src')) | |
); | |
gulp.task('compile', ['compileSrc', 'compileTest']); | |
gulp.task('test', () => gulp | |
.src('./build/test/**/*.js', { read: false }) | |
.pipe(mocha({ | |
reporter: 'spec', | |
timeout: 5000, | |
require: ['source-map-support/register'], | |
})) | |
.on('error', onErrorHandler) | |
); | |
gulp.task('compileSrcForWatch', ['compileSrc'], () => gulp.start('test')); | |
gulp.task('compileTestForWatch', ['compileTest'], () => gulp.start('test')); | |
gulp.task('watchAndTest', () => { | |
gulp.watch(config.inputSourceFiles, ['compileSrcForWatch']); | |
gulp.watch(config.inputTestFiles, ['compileTestForWatch']); | |
}); | |
gulp.task('watch', () => { | |
gulp.watch(config.inputSourceFiles, ['compileSrc']); | |
gulp.watch(config.inputTestFiles, ['compileTest']); | |
}); | |
// }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment