Skip to content

Commit e567002

Browse files
authored
Re-add "exports" declaration to package.json in backwards-compatible way (#1028)
* Re-add "exports" declaration to package.json in backwards-compatible way * add package.json "exports" tests * more * fix * fix * fix * tweak test to check esm loader entrypoint * narrow list of exported files * Update index.spec.ts
1 parent 3978f32 commit e567002

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
"version": "8.10.1",
44
"description": "TypeScript execution environment and REPL for node.js, with source map support",
55
"main": "dist/index.js",
6+
"exports": {
7+
".": "./dist/index.js",
8+
"./package": "./package.json",
9+
"./package.json": "./package.json",
10+
"./dist/bin": "./dist/bin.js",
11+
"./dist/bin.js": "./dist/bin.js",
12+
"./dist/bin-transpile": "./dist/bin-transpile.js",
13+
"./dist/bin-transpile.js": "./dist/bin-transpile.js",
14+
"./dist/bin-script": "./dist/bin-script.js",
15+
"./dist/bin-script.js": "./dist/bin-script.js",
16+
"./register": "./register/index.js",
17+
"./register/files": "./register/files.js",
18+
"./register/transpile-only": "./register/transpile-only.js",
19+
"./register/type-check": "./register/type-check.js",
20+
"./esm": "./esm.mjs",
21+
"./esm.mjs": "./esm.mjs"
22+
},
623
"types": "dist/index.d.ts",
724
"bin": {
825
"ts-node": "dist/bin.js",

src/index.spec.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { join } from 'path'
44
import semver = require('semver')
55
import ts = require('typescript')
66
import proxyquire = require('proxyquire')
7-
import { register, create, VERSION } from './index'
7+
import type * as tsNodeTypes from './index'
88
import { unlinkSync, existsSync, lstatSync } from 'fs'
99
import * as promisify from 'util.promisify'
1010
import { sync as rimrafSync } from 'rimraf'
11+
import { createRequire, createRequireFromPath } from 'module'
12+
import Module = require('module')
1113

1214
const execP = promisify(exec)
1315

@@ -18,13 +20,20 @@ const BIN_SCRIPT_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-script')
1820

1921
const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\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"
2230
before(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

3039
describe('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

Comments
 (0)