From a7aa0af9aefae1a7d801bbfe969148866c852a5c Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 22 Nov 2020 01:18:07 -0500 Subject: [PATCH] Switch from bundled createRequire shim to npm "create-require" (#1156) * Switch from bundled createRequire shim to npm create-require * Fix linter failures --- dist-raw/node-createrequire.js | 29 ----------------------------- package-lock.json | 5 +++++ package.json | 1 + src/index.spec.ts | 5 +++-- src/index.ts | 14 ++++---------- tsconfig.json | 2 +- 6 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 dist-raw/node-createrequire.js diff --git a/dist-raw/node-createrequire.js b/dist-raw/node-createrequire.js deleted file mode 100644 index 649deb10d..000000000 --- a/dist-raw/node-createrequire.js +++ /dev/null @@ -1,29 +0,0 @@ -// Extracted from https://github.com/nodejs/node/blob/ec2ffd6b9d255e19818b6949d2f7dc7ac70faee9/lib/internal/modules/cjs/loader.js -// then modified to suit our needs - -const path = require('path'); -const Module = require('module'); - -exports.createRequireFromPath = createRequireFromPath; - -function createRequireFromPath(filename) { - // Allow a directory to be passed as the filename - const trailingSlash = - filename.endsWith('/') || (isWindows && filename.endsWith('\\')); - - const proxyPath = trailingSlash ? - path.join(filename, 'noop.js') : - filename; - - const m = new Module(proxyPath); - m.filename = proxyPath; - - m.paths = Module._nodeModulePaths(m.path); - return makeRequireFunction(m, proxyPath); -} - -// This trick is much smaller than copy-pasting from https://github.com/nodejs/node/blob/ec2ffd6b9d255e19818b6949d2f7dc7ac70faee9/lib/internal/modules/cjs/helpers.js#L32-L101 -function makeRequireFunction(module, filename) { - module._compile('module.exports = require;', filename) - return mod.exports -} diff --git a/package-lock.json b/package-lock.json index df7e8885d..d7a2f0d25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -709,6 +709,11 @@ "integrity": "sha1-ZSKUwUZR2yj6k70tX/KYOk8IxjY=", "dev": true }, + "create-require": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.0.tgz", + "integrity": "sha512-yEFVS7dQjDXp5iOEtWisN4uFmL+pUTyIaEizKda9Eb77XX58p6pgFOLAPaBCP+IR6ZPZ1jgJLAuf+ABk0zXYBQ==" + }, "cross-spawn": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.2.tgz", diff --git a/package.json b/package.json index e04ad3548..7bc36e5ad 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.spec.ts b/src/index.spec.ts index 74a2959a4..58fc64c56 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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') @@ -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 diff --git a/src/index.ts b/src/index.ts index 277378226..3c51f8094 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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) -} diff --git a/tsconfig.json b/tsconfig.json index 465293630..0132c19cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "$schema": "./tsconfig.schemastore-schema.json", "compilerOptions": { "target": "es2015", - "lib": ["es2015"], + "lib": ["es2015", "dom"], "rootDir": "src", "outDir": "dist", "module": "commonjs",