@@ -56,6 +56,7 @@ export const VERSION = require('../package.json').version
5656 * Registration options.
5757 */
5858export interface Options {
59+ build ?: boolean | null
5960 pretty ?: boolean | null
6061 typeCheck ?: boolean | null
6162 transpileOnly ?: boolean | null
@@ -64,8 +65,8 @@ export interface Options {
6465 compiler ?: string
6566 ignore ?: string [ ]
6667 project ?: string
67- skipIgnore ?: boolean | null
6868 skipProject ?: boolean | null
69+ skipIgnore ?: boolean | null
6970 preferTsExts ?: boolean | null
7071 compilerOptions ?: object
7172 ignoreDiagnostics ?: Array < number | string >
@@ -95,19 +96,20 @@ export interface TypeInfo {
9596 * Default register options.
9697 */
9798export const DEFAULTS : Options = {
98- files : yn ( process . env [ 'TS_NODE_FILES' ] ) ,
99- pretty : yn ( process . env [ 'TS_NODE_PRETTY' ] ) ,
100- compiler : process . env [ 'TS_NODE_COMPILER' ] ,
101- compilerOptions : parse ( process . env [ 'TS_NODE_COMPILER_OPTIONS' ] ) ,
102- ignore : split ( process . env [ 'TS_NODE_IGNORE' ] ) ,
103- project : process . env [ 'TS_NODE_PROJECT' ] ,
104- skipIgnore : yn ( process . env [ 'TS_NODE_SKIP_IGNORE' ] ) ,
105- skipProject : yn ( process . env [ 'TS_NODE_SKIP_PROJECT' ] ) ,
106- preferTsExts : yn ( process . env [ 'TS_NODE_PREFER_TS_EXTS' ] ) ,
107- ignoreDiagnostics : split ( process . env [ 'TS_NODE_IGNORE_DIAGNOSTICS' ] ) ,
108- typeCheck : yn ( process . env [ 'TS_NODE_TYPE_CHECK' ] ) ,
109- transpileOnly : yn ( process . env [ 'TS_NODE_TRANSPILE_ONLY' ] ) ,
110- logError : yn ( process . env [ 'TS_NODE_LOG_ERROR' ] )
99+ files : yn ( process . env . TS_NODE_FILES ) ,
100+ pretty : yn ( process . env . TS_NODE_PRETTY ) ,
101+ compiler : process . env . TS_NODE_COMPILER ,
102+ compilerOptions : parse ( process . env . TS_NODE_COMPILER_OPTIONS ) ,
103+ ignore : split ( process . env . TS_NODE_IGNORE ) ,
104+ project : process . env . TS_NODE_PROJECT ,
105+ skipProject : yn ( process . env . TS_NODE_SKIP_PROJECT ) ,
106+ skipIgnore : yn ( process . env . TS_NODE_SKIP_IGNORE ) ,
107+ preferTsExts : yn ( process . env . TS_NODE_PREFER_TS_EXTS ) ,
108+ ignoreDiagnostics : split ( process . env . TS_NODE_IGNORE_DIAGNOSTICS ) ,
109+ typeCheck : yn ( process . env . TS_NODE_TYPE_CHECK ) ,
110+ transpileOnly : yn ( process . env . TS_NODE_TRANSPILE_ONLY ) ,
111+ logError : yn ( process . env . TS_NODE_LOG_ERROR ) ,
112+ build : yn ( process . env . TS_NODE_BUILD )
111113}
112114
113115/**
@@ -191,7 +193,7 @@ function cachedLookup <T> (fn: (arg: string) => T): (arg: string) => T {
191193 * Register TypeScript compiler.
192194 */
193195export function register ( opts : Options = { } ) : Register {
194- const options = Object . assign ( { } , DEFAULTS , opts )
196+ const options = { ... DEFAULTS , ... opts }
195197 const originalJsHandler = require . extensions [ '.js' ] // tslint:disable-line
196198
197199 const ignoreDiagnostics = [
@@ -201,9 +203,7 @@ export function register (opts: Options = {}): Register {
201203 ...( options . ignoreDiagnostics || [ ] )
202204 ] . map ( Number )
203205
204- const ignore = options . skipIgnore ? [ ] : (
205- options . ignore || [ '/node_modules/' ]
206- ) . map ( str => new RegExp ( str ) )
206+ const ignore = options . skipIgnore ? [ ] : ( options . ignore || [ '/node_modules/' ] ) . map ( str => new RegExp ( str ) )
207207
208208 // Require the TypeScript compiler and configuration.
209209 const cwd = process . cwd ( )
@@ -369,31 +369,37 @@ export function register (opts: Options = {}): Register {
369369 const sourceFile = builderProgram . getSourceFile ( fileName )
370370 if ( ! sourceFile ) throw new TypeError ( `Unable to read file: ${ fileName } ` )
371371
372- const diagnostics = ts . getPreEmitDiagnostics ( builderProgram . getProgram ( ) , sourceFile )
372+ const program = builderProgram . getProgram ( )
373+ const diagnostics = ts . getPreEmitDiagnostics ( program , sourceFile )
373374 const diagnosticList = filterDiagnostics ( diagnostics , ignoreDiagnostics )
374375
375376 if ( diagnosticList . length ) reportTSError ( diagnosticList )
376377
377- const result = builderProgram . emit ( sourceFile , ( path , file ) => {
378+ const result = builderProgram . emit ( sourceFile , ( path , file , writeByteOrderMark ) => {
378379 if ( path . endsWith ( '.map' ) ) {
379380 output [ 1 ] = file
380381 } else {
381382 output [ 0 ] = file
382383 }
384+
385+ if ( options . build ) sys . writeFile ( path , file , writeByteOrderMark )
383386 } , undefined , undefined , getCustomTransformers ( ) )
384387
385388 if ( result . emitSkipped ) {
386389 throw new TypeError ( `${ relative ( cwd , fileName ) } : Emit skipped` )
387390 }
388391
389- // Throw an error when requiring `.d.ts` files.
392+ // Throw an error when requiring files that cannot be compiled .
390393 if ( output [ 0 ] === '' ) {
394+ if ( program . isSourceFileFromExternalLibrary ( sourceFile ) ) {
395+ throw new TypeError ( `Unable to compile file from external library: ${ relative ( cwd , fileName ) } ` )
396+ }
397+
391398 throw new TypeError (
392- ' Unable to require `.d.ts` file.\n' +
399+ ` Unable to require file: ${ relative ( cwd , fileName ) } \n` +
393400 'This is usually the result of a faulty configuration or import. ' +
394- 'Make sure there is a `.js`, `.json` or another executable extension and ' +
395- 'loader (attached before `ts-node`) available alongside ' +
396- `\`${ basename ( fileName ) } \`.`
401+ 'Make sure there is a `.js`, `.json` or other executable extension with ' +
402+ 'loader attached before `ts-node` available.'
397403 )
398404 }
399405
@@ -420,7 +426,8 @@ export function register (opts: Options = {}): Register {
420426 }
421427 }
422428
423- if ( config . options . incremental ) {
429+ // Write `.tsbuildinfo` when `--build` is enabled.
430+ if ( options . build && config . options . incremental ) {
424431 process . on ( 'exit' , ( ) => {
425432 // Emits `.tsbuildinfo` to filesystem.
426433 ( builderProgram . getProgram ( ) as any ) . emitBuildInfo ( )
0 commit comments