Created
December 18, 2012 18:38
-
-
Save colinhicks/4330701 to your computer and use it in GitHub Desktop.
Grunt task for TypeScript compilation
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
module.exports = function(grunt) { | |
'use strict'; | |
// TODO: ditch this when grunt v0.4 is released | |
grunt.util = grunt.util || grunt.utils; | |
var async = grunt.util.async; | |
grunt.registerMultiTask('typescript', 'Compile TypeScript to JavaScript', function() { | |
var helpers = require('grunt-contrib-lib').init(grunt), | |
path = require('path'), | |
options = helpers.options(this), | |
cb = this.async(), | |
args = helpers.optsToArgs(options); | |
grunt.verbose.writeflags(options, 'Options'); | |
// TODO: ditch this when grunt v0.4 is released | |
this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target); | |
async.forEachSeries(this.files, function(el, cb2) { | |
el.dest = path.normalize(el.dest); | |
var srcFiles = grunt.file.expandFiles(el.src), | |
useOutParam = !helpers.isIndividualDest(el.dest); | |
if (useOutParam) { | |
args = args.concat(['--out', el.dest]); | |
// Make sure grunt creates the destination folders | |
grunt.file.write(el.dest, ''); | |
} | |
args = args.concat(srcFiles); | |
var tsc = grunt.util.spawn({ | |
cmd: 'tsc', | |
args: args | |
}, function(error, result, code){ | |
cb2(code > 0); | |
}).on('exit', function (code){ | |
if (code !== 0) { | |
grunt.warn('Error code ' + code + ' returned from tsc.'); | |
} else if (useOutParam && !!options.sourcemap) { | |
// fix pathing in source maps. tsc computes relative paths correctly only | |
// if the source .ts exists within the same relative dir as the outfile | |
var sourcemapPath = el.dest + '.map', | |
sourcemapContents = grunt.file.read(sourcemapPath), | |
absolutePart = process.cwd()+'/app', | |
fixedContents = | |
sourcemapContents.replace(new RegExp(absolutePart, 'g'), ''); | |
grunt.file.write(sourcemapPath, fixedContents); | |
} | |
}); | |
tsc.stdout.pipe(process.stdout); | |
tsc.stderr.pipe(process.stderr); | |
}, function(error) { | |
cb(!error); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment