I'm trying to figure out how to render a typescript file programmatically to javascript file.
Is this possible to do with ts-node for example like this:
function tsMiddleware (req, res, next) {
var parsed = require('url').parse(req.url);
if (parsed.pathname.match(/\.ts$/)) {
return ts(parsed.pathname).then(function (o) {
res.setHeader('Content-Type', 'text/js');
res.end(o.js);
}).catch((err) => {
console.log(err);
});
}
next();
function ts(src) {
return new Promise((resolve, reject) => {
require('ts-node').render({
file: 'src' + src
}, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
}
I don't want to execute the ts file in nodejs, but instead just compile ts file and send the output back to the browser.