Skip to content

Commit

Permalink
feat: Added esm loader
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Nov 15, 2021
1 parent dbc9bdc commit 9d4779d
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 14 deletions.
13 changes: 13 additions & 0 deletions esm.mjs
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]",
"ts45": "npm:[email protected]",
Expand Down
1 change: 0 additions & 1 deletion register.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ try {
);
}

tsNode.register();
require('./').nodeRegister();
5 changes: 2 additions & 3 deletions src/compatibility/node-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions test/projects/esm-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"name": "@tests/esm-loader",
"version": "0.0.0",
"type": "module"
}
1 change: 1 addition & 0 deletions test/projects/esm-loader/src/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = null;
4 changes: 4 additions & 0 deletions test/projects/esm-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "#identifier";
import { b } from "#identifier";

console.log(b);
29 changes: 29 additions & 0 deletions test/projects/esm-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
22 changes: 22 additions & 0 deletions test/tests/compatibility/esm-loader.test.ts
Original file line number Diff line number Diff line change
@@ -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)/);
});
});
3 changes: 2 additions & 1 deletion test/tests/compatibility/node-register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe(`[Compatibility: ts-node] Register script`, () => {
registerSpy?.mockRestore();
}
});

test(`Uses existing ts-node if found`, () => {
const fakeInstance: any = {};

Expand Down Expand Up @@ -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;

Expand Down
40 changes: 32 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@cspotcode/[email protected]":
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/[email protected]":
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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9d4779d

Please sign in to comment.