@@ -4,10 +4,12 @@ import { join } from 'path'
44import semver = require( 'semver' )
55import ts = require( 'typescript' )
66import proxyquire = require( 'proxyquire' )
7- import { register , create , VERSION } from './index'
7+ import type * as tsNodeTypes from './index'
88import { unlinkSync , existsSync , lstatSync } from 'fs'
99import * as promisify from 'util.promisify'
1010import { sync as rimrafSync } from 'rimraf'
11+ import { createRequire , createRequireFromPath } from 'module'
12+ import Module = require( 'module' )
1113
1214const execP = promisify ( exec )
1315
@@ -18,13 +20,20 @@ const BIN_SCRIPT_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-script')
1820
1921const SOURCE_MAP_REGEXP = / \/ \/ # s o u r c e M a p p i n g U R L = d a t a : a p p l i c a t i o n \/ j s o n ; c h a r s e t = u t f \- 8 ; b a s e 6 4 , [ \w \+ ] + = * $ /
2022
23+ // `createRequire` does not exist on older node versions
24+ const testsDirRequire = ( createRequire || createRequireFromPath ) ( join ( TEST_DIR , 'index.js' ) ) // tslint:disable-line
25+
26+ // Set after ts-node is installed locally
27+ let { register, create, VERSION } : typeof tsNodeTypes = { } as any
28+
2129// Pack and install ts-node locally, necessary to test package "exports"
2230before ( async function ( ) {
2331 this . timeout ( 30000 )
2432 rimrafSync ( join ( TEST_DIR , 'node_modules' ) )
2533 await execP ( `npm install` , { cwd : TEST_DIR } )
2634 const packageLockPath = join ( TEST_DIR , 'package-lock.json' )
2735 existsSync ( packageLockPath ) && unlinkSync ( packageLockPath )
36+ ; ( { register, create, VERSION } = testsDirRequire ( 'ts-node' ) )
2837} )
2938
3039describe ( 'ts-node' , function ( ) {
@@ -35,6 +44,34 @@ describe('ts-node', function () {
3544 it ( 'should export the correct version' , function ( ) {
3645 expect ( VERSION ) . to . equal ( require ( '../package.json' ) . version )
3746 } )
47+ it ( 'should export all CJS entrypoints' , function ( ) {
48+ // Ensure our package.json "exports" declaration allows `require()`ing all our entrypoints
49+ // https://github.com/TypeStrong/ts-node/pull/1026
50+
51+ testsDirRequire . resolve ( 'ts-node' )
52+
53+ // only reliably way to ask node for the root path of a dependency is Path.resolve(require.resolve('ts-node/package'), '..')
54+ testsDirRequire . resolve ( 'ts-node/package' )
55+ testsDirRequire . resolve ( 'ts-node/package.json' )
56+
57+ // All bin entrypoints for people who need to augment our CLI: `node -r otherstuff ./node_modules/ts-node/dist/bin`
58+ testsDirRequire . resolve ( 'ts-node/dist/bin' )
59+ testsDirRequire . resolve ( 'ts-node/dist/bin.js' )
60+ testsDirRequire . resolve ( 'ts-node/dist/bin-transpile' )
61+ testsDirRequire . resolve ( 'ts-node/dist/bin-transpile.js' )
62+ testsDirRequire . resolve ( 'ts-node/dist/bin-script' )
63+ testsDirRequire . resolve ( 'ts-node/dist/bin-script.js' )
64+
65+ // Must be `require()`able obviously
66+ testsDirRequire . resolve ( 'ts-node/register' )
67+ testsDirRequire . resolve ( 'ts-node/register/files' )
68+ testsDirRequire . resolve ( 'ts-node/register/transpile-only' )
69+ testsDirRequire . resolve ( 'ts-node/register/type-check' )
70+
71+ // `node --loader ts-node/esm`
72+ testsDirRequire . resolve ( 'ts-node/esm' )
73+ testsDirRequire . resolve ( 'ts-node/esm.mjs' )
74+ } )
3875
3976 describe ( 'cli' , function ( ) {
4077 this . slow ( 1000 )
@@ -523,11 +560,14 @@ describe('ts-node', function () {
523560 } )
524561
525562 describe ( 'register' , function ( ) {
526- const registered = register ( {
527- project : PROJECT ,
528- compilerOptions : {
529- jsx : 'preserve'
530- }
563+ let registered : tsNodeTypes . Register
564+ before ( ( ) => {
565+ registered = register ( {
566+ project : PROJECT ,
567+ compilerOptions : {
568+ jsx : 'preserve'
569+ }
570+ } )
531571 } )
532572
533573 const moduleTestPath = require . resolve ( '../tests/module' )
@@ -637,10 +677,11 @@ describe('ts-node', function () {
637677 } )
638678
639679 describe ( 'JSX preserve' , ( ) => {
640- let old = require . extensions [ '.tsx' ] // tslint:disable-line
680+ let old : ( m : Module , filename : string ) => any
641681 let compiled : string
642682
643683 before ( function ( ) {
684+ old = require . extensions [ '.tsx' ] ! // tslint:disable-line
644685 require . extensions [ '.tsx' ] = ( m : any , fileName ) => { // tslint:disable-line
645686 const _compile = m . _compile
646687
@@ -649,7 +690,7 @@ describe('ts-node', function () {
649690 return _compile . call ( this , code , fileName )
650691 }
651692
652- return old ! ( m , fileName )
693+ return old ( m , fileName )
653694 }
654695 } )
655696
0 commit comments