Created
June 25, 2018 12:57
-
-
Save marcghorayeb/a092cdac015a960dca6dd87abe8c7b00 to your computer and use it in GitHub Desktop.
Gulp + TSC hack
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
let isWatching = false; | |
gulp.task('ts', () => { | |
return new Promise((resolve, reject) => { | |
const cmd = 'npx'; | |
const args = ['tsc']; | |
if (isWatching) { | |
args.push('-w', '--preserveWatchOutput'); | |
} | |
const tsc = spawn(cmd, args); | |
const sanitizeTSCOutput = (data) => data.toString().trim(); | |
tsc.stdout.on('data', (data) => { | |
const message = sanitizeTSCOutput(data); | |
log(message); | |
// Hack to let gulp know we have the watcher online | |
if (isWatching && message.includes('Watching for file changes')) { | |
resolve(); | |
} | |
}); | |
tsc.stderr.on('data', (data) => { | |
log(sanitizeTSCOutput(data)); | |
}); | |
tsc.on('error', log); | |
tsc.on('close', (code) => { | |
if (!isWatching) { | |
code !== 0 ? reject('TSC failed') : resolve(); | |
} | |
}); | |
}); | |
}); | |
gulp.task('watch', gulp.series(function setIsWatching(cb) { | |
isWatching = true; | |
cb(); | |
}, 'ts')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very intersting! I'd like to do something similar in my project to speed up incremental compilation. How do you call this? And how to ensure it uses tsc version that was downloaded with npm?