<p dir="auto">A require hook, when installed, will hook into the require-function, replace the current functionality, to make TypeScript compiler transpile files on-the-fly (through Node.js) when required.</p>
<p dir="auto">Should be configurable.</p>
<p dir="auto">Babel (formerly e6to5) has this feature: <a href="https://babeljs.io/docs/usage/require/" rel="nofollow">https://babeljs.io/docs/usage/require/</a></p>
<p dir="auto">Required for a Node CLI <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="60147673" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/2231" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/2231/hovercard" href="https://github.com/microsoft/TypeScript/issues/2231">#2231</a>.</p>
<h1 dir="auto">Proposal</h1>
<p dir="auto">You simply do <code class="notranslate">require('typescript').register()</code> and then all <code class="notranslate">require</code> calls e.g. <code class="notranslate">var foo = require('./foo')</code> would load <code class="notranslate">foo.ts</code> if <code class="notranslate">foo.js/json/etc</code> are not found.</p>
<p dir="auto">This would mean we would compile in-memory and completely skip all type checking and do a fast emit. This would however greatly help increase <code class="notranslate">.ts</code> adaption IMHO.</p>
<p dir="auto">Currently there is a userland maintained : <a href="https://github.com/TypeStrong/ts-node">https://github.com/TypeStrong/ts-node</a></p>
<h1 dir="auto">Motivation</h1>
<p dir="auto">CoffeeScript supports this via <code class="notranslate">require('coffee-script').register();</code> (synonym for <code class="notranslate">require('coffee-script/register');</code>) so that once you call this function it patches <code class="notranslate">require</code> to support <code class="notranslate">.coffee</code> files.</p>
<p dir="auto">E.g.</p>
<ul dir="auto">
<li>Atom does this for coffee : <a href="https://github.com/atom/atom/blob/a9d215970f042f9454ff74bcfb678664a582f8e2/static/index.js#L24-L26">here</a> and <a href="https://github.com/atom/atom/blob/a9d215970f042f9454ff74bcfb678664a582f8e2/static/index.js#L43-L45">here</a></li>
<li>Grunt does this <a href="https://github.com/gruntjs/grunt/blob/d47c75543b09218be95869eed4c285d6044b538b/lib/grunt.js#L15-L16">here</a></li>
</ul>
<p dir="auto">The implementation for coffeescript can be found here : <a href="http://coffeescript.org/documentation/docs/register.html" rel="nofollow">http://coffeescript.org/documentation/docs/register.html</a></p>
<h1 dir="auto">Where in <code class="notranslate">typescript</code></h1>
<p dir="auto">This can potentially be done in an external NPM package, but:</p>
<ul dir="auto">
<li>having it officially support would enhance adaption.</li>
<li>give better statistics for <code class="notranslate">typescript</code> npm package as users would start <code class="notranslate">require</code>ing <code class="notranslate">typescript</code> (coffeescript downloads <a href="https://www.npmjs.com/package/coffee-script" rel="nofollow">https://www.npmjs.com/package/coffee-script</a>)</li>
</ul>