diff --git a/esm.mjs b/esm.mjs new file mode 100755 index 00000000..29f4af0f --- /dev/null +++ b/esm.mjs @@ -0,0 +1,13 @@ +import { fileURLToPath } from 'url'; +import { createRequire } from 'module'; +const require = createRequire(fileURLToPath(import.meta.url)); + +/** @type {import('./src/compatibility/node-register')} */ +const { nodeRegister } = require('./dist/compatibility/node-register'); + +/** @type {import('ts-node')} */ +import tsNode from 'ts-node'; + +const tsNodeInstance = nodeRegister(); + +export const { resolve, getFormat, transformSource } = tsNode.createEsmHooks(tsNodeInstance); diff --git a/package.json b/package.json index bb2ced5d..d68228a9 100755 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "rimraf": "^3.0.2", "standard-version": "^9.3.1", "ts-jest": "^27.0.4", - "ts-node": "^10.1.0", + "ts-node": "^10.4.0", "ts-patch": "^1.4.4", "ts3": "npm:typescript@3.7.7", "ts45": "npm:typescript@4.5.0-beta", diff --git a/register.js b/register.js index c6f41b0e..7d681960 100755 --- a/register.js +++ b/register.js @@ -7,5 +7,4 @@ try { ); } -tsNode.register(); require('./').nodeRegister(); diff --git a/src/compatibility/node-register.ts b/src/compatibility/node-register.ts index cba3369e..74799c84 100755 --- a/src/compatibility/node-register.ts +++ b/src/compatibility/node-register.ts @@ -68,7 +68,7 @@ export function mergeTransformers( // region: TsNode Registration Utility /* ****************************************************************************************************************** */ -export function nodeRegister(): TSNode.RegisterOptions | undefined { +export function nodeRegister(): TSNode.Service | undefined { const { tsNodeInstance, tsNode } = nodeRegister.initialize(); const transformerConfig = getProjectTransformerConfig(tsNodeInstance.config); @@ -96,8 +96,7 @@ export function nodeRegister(): TSNode.RegisterOptions | undefined { } // Re-register with new transformers - tsNode.register(registerOptions); - return registerOptions; + return tsNode.register(registerOptions); } export namespace nodeRegister { diff --git a/test/projects/esm-loader/package.json b/test/projects/esm-loader/package.json new file mode 100755 index 00000000..cc8fd44c --- /dev/null +++ b/test/projects/esm-loader/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "name": "@tests/esm-loader", + "version": "0.0.0", + "type": "module" +} diff --git a/test/projects/esm-loader/src/id.ts b/test/projects/esm-loader/src/id.ts new file mode 100755 index 00000000..01b2dc8e --- /dev/null +++ b/test/projects/esm-loader/src/id.ts @@ -0,0 +1 @@ +export const b = null; diff --git a/test/projects/esm-loader/src/index.ts b/test/projects/esm-loader/src/index.ts new file mode 100755 index 00000000..c0bb14c0 --- /dev/null +++ b/test/projects/esm-loader/src/index.ts @@ -0,0 +1,4 @@ +export * from "#identifier"; +import { b } from "#identifier"; + +console.log(b); diff --git a/test/projects/esm-loader/tsconfig.json b/test/projects/esm-loader/tsconfig.json new file mode 100755 index 00000000..49a3c232 --- /dev/null +++ b/test/projects/esm-loader/tsconfig.json @@ -0,0 +1,29 @@ +{ + "include": [ "src" ], + + "compilerOptions": { + "noEmit": true, + + "rootDir": ".", + "module": "ESNext", + "target": "ESNext", + "esModuleInterop": true, + "moduleResolution": "node", + "declaration": true, + + "baseUrl": "./src", + "paths": { + "#identifier": [ "./id.ts" ] + }, + + "plugins": [ + { + "transform": "typescript-transform-paths" + }, + { + "transform": "typescript-transform-paths", + "afterDeclarations": true + } + ] + } +} diff --git a/test/tests/compatibility/esm-loader.test.ts b/test/tests/compatibility/esm-loader.test.ts new file mode 100755 index 00000000..8641c1a9 --- /dev/null +++ b/test/tests/compatibility/esm-loader.test.ts @@ -0,0 +1,22 @@ +import { execSync } from "child_process"; +import path from "path"; +import ts from 'typescript' +import { projectsPath } from "../../src/config"; + +/* ****************************************************************************************************************** * + * Tests + * ****************************************************************************************************************** */ + +describe(`ESM Loader`, () => { + const esmProjectRoot = ts.normalizePath(path.join(projectsPath, "esm-loader")); + + // See: https://github.com/LeDDGroup/typescript-transform-paths/issues/134 + test(`Transforms ESM project`, () => { + const res = execSync( + `node --no-warnings --loader tstp/esm --es-module-specifier-resolution=node src/index.ts`, + { cwd: esmProjectRoot } + ).toString(); + + expect(res).toMatch(/^null($|\r?\n)/); + }); +}); diff --git a/test/tests/compatibility/node-register.test.ts b/test/tests/compatibility/node-register.test.ts index 59dd17b5..7f3d7cf5 100755 --- a/test/tests/compatibility/node-register.test.ts +++ b/test/tests/compatibility/node-register.test.ts @@ -60,6 +60,7 @@ describe(`[Compatibility: ts-node] Register script`, () => { registerSpy?.mockRestore(); } }); + test(`Uses existing ts-node if found`, () => { const fakeInstance: any = {}; @@ -172,7 +173,7 @@ describe(`[Compatibility: ts-node] Register script`, () => { }); const originalTsNodeInstance = global.process[instanceSymbol]; - registerResult = nodeRegister()!; + registerResult = nodeRegister()!.options; instanceRegistrationResult = global.process[instanceSymbol]!; global.process[instanceSymbol] = originalTsNodeInstance; diff --git a/yarn.lock b/yarn.lock index 6f98ae26..06dfd1d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -482,6 +482,18 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== +"@cspotcode/source-map-consumer@0.8.0": + version "0.8.0" + resolved "https://registry.yarnpkg.com/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz#33bf4b7b39c178821606f669bbc447a6a629786b" + integrity sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg== + +"@cspotcode/source-map-support@0.7.0": + version "0.7.0" + resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz#4789840aa859e46d2f3173727ab707c66bf344f5" + integrity sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA== + dependencies: + "@cspotcode/source-map-consumer" "0.8.0" + "@hutson/parse-repository-url@^3.0.0": version "3.0.2" resolved "https://registry.yarnpkg.com/@hutson/parse-repository-url/-/parse-repository-url-3.0.2.tgz#98c23c950a3d9b6c8f0daed06da6c3af06981340" @@ -717,7 +729,7 @@ resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2" integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg== -"@tsconfig/node16@^1.0.1": +"@tsconfig/node16@^1.0.2": version "1.0.2" resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e" integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA== @@ -876,6 +888,11 @@ acorn-walk@^7.1.1: resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-7.2.0.tgz#0de889a601203909b0fbe07b8938dc21d2e967bc" integrity sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA== +acorn-walk@^8.1.1: + version "8.2.0" + resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1" + integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA== + acorn@^7.1.1: version "7.4.1" resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.4.1.tgz#feaed255973d2e77555b83dbc08851a6c63520fa" @@ -886,6 +903,11 @@ acorn@^8.2.4: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.4.1.tgz#56c36251fc7cabc7096adc18f05afe814321a28c" integrity sha512-asabaBSkEKosYKMITunzX177CXxQ4Q8BSSzMTKD+FefUhipQC70gfW5SiUDhYQ3vk8G+81HqQk7Fv9OXwwn9KA== +acorn@^8.4.1: + version "8.5.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.5.0.tgz#4512ccb99b3698c752591e9bb4472e38ad43cee2" + integrity sha512-yXbYeFy+jUuYd3/CDcg2NkIYE991XYX/bje7LmjJigUciaeO1JR4XxXgCIV1/Zc/dRuFEyw1L0pbA+qynJkW5Q== + add-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa" @@ -3357,7 +3379,7 @@ slash@^3.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== -source-map-support@^0.5.17, source-map-support@^0.5.6: +source-map-support@^0.5.6: version "0.5.19" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== @@ -3665,20 +3687,22 @@ ts-jest@^27.0.4: semver "7.x" yargs-parser "20.x" -ts-node@^10.1.0: - version "10.1.0" - resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.1.0.tgz#e656d8ad3b61106938a867f69c39a8ba6efc966e" - integrity sha512-6szn3+J9WyG2hE+5W8e0ruZrzyk1uFLYye6IGMBadnOzDh8aP7t8CbFpsfCiEx2+wMixAhjFt7lOZC4+l+WbEA== +ts-node@^10.4.0: + version "10.4.0" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.4.0.tgz#680f88945885f4e6cf450e7f0d6223dd404895f7" + integrity sha512-g0FlPvvCXSIO1JDF6S232P5jPYqBkRL9qly81ZgAOSU7rwI0stphCgd2kLiCrU9DjQCrJMWEqcNSjQL02s6d8A== dependencies: + "@cspotcode/source-map-support" "0.7.0" "@tsconfig/node10" "^1.0.7" "@tsconfig/node12" "^1.0.7" "@tsconfig/node14" "^1.0.0" - "@tsconfig/node16" "^1.0.1" + "@tsconfig/node16" "^1.0.2" + acorn "^8.4.1" + acorn-walk "^8.1.1" arg "^4.1.0" create-require "^1.1.0" diff "^4.0.1" make-error "^1.1.1" - source-map-support "^0.5.17" yn "3.1.1" ts-patch@^1.4.4: