Skip to content

Commit

Permalink
Re-add "exports" declaration to package.json in backwards-compatible …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
cspotcode committed May 23, 2020
1 parent 3978f32 commit e567002
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
17 changes: 17 additions & 0 deletions package.json
Expand Up @@ -3,6 +3,23 @@
"version": "8.10.1",
"description": "TypeScript execution environment and REPL for node.js, with source map support",
"main": "dist/index.js",
"exports": {
".": "./dist/index.js",
"./package": "./package.json",
"./package.json": "./package.json",
"./dist/bin": "./dist/bin.js",
"./dist/bin.js": "./dist/bin.js",
"./dist/bin-transpile": "./dist/bin-transpile.js",
"./dist/bin-transpile.js": "./dist/bin-transpile.js",
"./dist/bin-script": "./dist/bin-script.js",
"./dist/bin-script.js": "./dist/bin-script.js",
"./register": "./register/index.js",
"./register/files": "./register/files.js",
"./register/transpile-only": "./register/transpile-only.js",
"./register/type-check": "./register/type-check.js",
"./esm": "./esm.mjs",
"./esm.mjs": "./esm.mjs"
},
"types": "dist/index.d.ts",
"bin": {
"ts-node": "dist/bin.js",
Expand Down
57 changes: 49 additions & 8 deletions src/index.spec.ts
Expand Up @@ -4,10 +4,12 @@ import { join } from 'path'
import semver = require('semver')
import ts = require('typescript')
import proxyquire = require('proxyquire')
import { register, create, VERSION } from './index'
import type * as tsNodeTypes from './index'
import { unlinkSync, existsSync, lstatSync } from 'fs'
import * as promisify from 'util.promisify'
import { sync as rimrafSync } from 'rimraf'
import { createRequire, createRequireFromPath } from 'module'
import Module = require('module')

const execP = promisify(exec)

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

const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/

// `createRequire` does not exist on older node versions
const testsDirRequire = (createRequire || createRequireFromPath)(join(TEST_DIR, 'index.js')) // tslint:disable-line

// Set after ts-node is installed locally
let { register, create, VERSION }: typeof tsNodeTypes = {} as any

// Pack and install ts-node locally, necessary to test package "exports"
before(async function () {
this.timeout(30000)
rimrafSync(join(TEST_DIR, 'node_modules'))
await execP(`npm install`, { cwd: TEST_DIR })
const packageLockPath = join(TEST_DIR, 'package-lock.json')
existsSync(packageLockPath) && unlinkSync(packageLockPath)
;({ register, create, VERSION } = testsDirRequire('ts-node'))
})

describe('ts-node', function () {
Expand All @@ -35,6 +44,34 @@ describe('ts-node', function () {
it('should export the correct version', function () {
expect(VERSION).to.equal(require('../package.json').version)
})
it('should export all CJS entrypoints', function () {
// Ensure our package.json "exports" declaration allows `require()`ing all our entrypoints
// https://github.com/TypeStrong/ts-node/pull/1026

testsDirRequire.resolve('ts-node')

// only reliably way to ask node for the root path of a dependency is Path.resolve(require.resolve('ts-node/package'), '..')
testsDirRequire.resolve('ts-node/package')
testsDirRequire.resolve('ts-node/package.json')

// All bin entrypoints for people who need to augment our CLI: `node -r otherstuff ./node_modules/ts-node/dist/bin`
testsDirRequire.resolve('ts-node/dist/bin')
testsDirRequire.resolve('ts-node/dist/bin.js')
testsDirRequire.resolve('ts-node/dist/bin-transpile')
testsDirRequire.resolve('ts-node/dist/bin-transpile.js')
testsDirRequire.resolve('ts-node/dist/bin-script')
testsDirRequire.resolve('ts-node/dist/bin-script.js')

// Must be `require()`able obviously
testsDirRequire.resolve('ts-node/register')
testsDirRequire.resolve('ts-node/register/files')
testsDirRequire.resolve('ts-node/register/transpile-only')
testsDirRequire.resolve('ts-node/register/type-check')

// `node --loader ts-node/esm`
testsDirRequire.resolve('ts-node/esm')
testsDirRequire.resolve('ts-node/esm.mjs')
})

describe('cli', function () {
this.slow(1000)
Expand Down Expand Up @@ -523,11 +560,14 @@ describe('ts-node', function () {
})

describe('register', function () {
const registered = register({
project: PROJECT,
compilerOptions: {
jsx: 'preserve'
}
let registered: tsNodeTypes.Register
before(() => {
registered = register({
project: PROJECT,
compilerOptions: {
jsx: 'preserve'
}
})
})

const moduleTestPath = require.resolve('../tests/module')
Expand Down Expand Up @@ -637,10 +677,11 @@ describe('ts-node', function () {
})

describe('JSX preserve', () => {
let old = require.extensions['.tsx'] // tslint:disable-line
let old: (m: Module, filename: string) => any
let compiled: string

before(function () {
old = require.extensions['.tsx']! // tslint:disable-line
require.extensions['.tsx'] = (m: any, fileName) => { // tslint:disable-line
const _compile = m._compile

Expand All @@ -649,7 +690,7 @@ describe('ts-node', function () {
return _compile.call(this, code, fileName)
}

return old!(m, fileName)
return old(m, fileName)
}
})

Expand Down

0 comments on commit e567002

Please sign in to comment.