Skip to content

Commit

Permalink
Switch from bundled createRequire shim to npm "create-require" (#1156)
Browse files Browse the repository at this point in the history
* Switch from bundled createRequire shim to npm create-require

* Fix linter failures
  • Loading branch information
cspotcode authored Nov 22, 2020
1 parent 8b9489a commit a7aa0af
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 42 deletions.
29 changes: 0 additions & 29 deletions dist-raw/node-createrequire.js

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
},
"dependencies": {
"arg": "^4.1.0",
"create-require": "^1.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"source-map-support": "^0.5.17",
Expand Down
5 changes: 3 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ 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 type _createRequire from 'create-require'
const createRequire: typeof _createRequire = require('create-require')
import { pathToFileURL } from 'url'
import Module = require('module')

Expand All @@ -22,7 +23,7 @@ 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
const testsDirRequire = createRequire(join(TEST_DIR, 'index.js')) // tslint:disable-line

// Set after ts-node is installed locally
let { register, create, VERSION }: typeof tsNodeTypes = {} as any
Expand Down
14 changes: 4 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { BaseError } from 'make-error'
import * as util from 'util'
import { fileURLToPath } from 'url'
import type * as _ts from 'typescript'
import * as Module from 'module'
import { Module, createRequire as nodeCreateRequire, createRequireFromPath as nodeCreateRequireFromPath } from 'module'
import type _createRequire from 'create-require'
// tslint:disable-next-line
const createRequire = nodeCreateRequire ?? nodeCreateRequireFromPath ?? require('create-require') as typeof _createRequire

/**
* Does this version of node obey the package.json "type" field
Expand Down Expand Up @@ -1215,12 +1218,3 @@ function getTokenAtPosition (ts: typeof _ts, sourceFile: _ts.SourceFile, positio
return current
}
}

let nodeCreateRequire: (path: string) => NodeRequire
function createRequire (filename: string) {
if (!nodeCreateRequire) {
// tslint:disable-next-line
nodeCreateRequire = Module.createRequire || Module.createRequireFromPath || require('../dist-raw/node-createrequire').createRequireFromPath
}
return nodeCreateRequire(filename)
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "./tsconfig.schemastore-schema.json",
"compilerOptions": {
"target": "es2015",
"lib": ["es2015"],
"lib": ["es2015", "dom"],
"rootDir": "src",
"outDir": "dist",
"module": "commonjs",
Expand Down

0 comments on commit a7aa0af

Please sign in to comment.