Skip to content

Instantly share code, notes, and snippets.

@marcghorayeb
Created June 25, 2018 12:57
Show Gist options
  • Save marcghorayeb/a092cdac015a960dca6dd87abe8c7b00 to your computer and use it in GitHub Desktop.
Save marcghorayeb/a092cdac015a960dca6dd87abe8c7b00 to your computer and use it in GitHub Desktop.
Gulp + TSC hack
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'));
@xenod
Copy link

xenod commented Aug 27, 2018

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?

@xenod
Copy link

xenod commented Sep 3, 2018

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment