Skip to content

Commit 8185831

Browse files
committed
feat: Add esm support for ts-node register (closes #134)
1 parent ebefe66 commit 8185831

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

esm.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { fileURLToPath } from 'url';
2+
import { createRequire } from 'module';
3+
const require = createRequire(fileURLToPath(import.meta.url));
4+
5+
/** @type {import('./dist/register')} */
6+
const { register } = require('./dist/register');
7+
8+
/** @type {import('ts-node/dist/esm')} */
9+
import esm from 'ts-node/dist/esm';
10+
11+
const options = register();
12+
13+
export const {
14+
resolve,
15+
getFormat,
16+
transformSource,
17+
} = esm.registerAndCreateEsmHooks(options);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"types",
4949
"README.md",
5050
"CHANGELOG.md",
51-
"register.js"
51+
"register.js",
52+
"esm.mjs"
5253
],
5354
"devDependencies": {
5455
"@types/jest": "^26.0.24",

test/projects/esm/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"private": true,
3+
"name": "@tests/extras-esm",
4+
"version": "0.0.0",
5+
"dependencies": {
6+
"typescript-transform-paths": "link:../../../"
7+
},
8+
"type": "module"
9+
}

test/projects/esm/src/id.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = null;

test/projects/esm/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "#identifier";
2+
import { b } from "#identifier";
3+
4+
console.log(b);

test/projects/esm/tsconfig.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"include": [ "src" ],
3+
4+
"ts-node": {
5+
"transpileOnly": true,
6+
"require": [ "typescript-transform-paths/register" ],
7+
},
8+
9+
"compilerOptions": {
10+
"noEmit": true,
11+
12+
"rootDir": ".",
13+
"module": "ESNext",
14+
"target": "ESNext",
15+
"esModuleInterop": true,
16+
"moduleResolution": "node",
17+
"declaration": true,
18+
19+
"baseUrl": "./src",
20+
"paths": {
21+
"#identifier": [ "./id.ts" ]
22+
},
23+
24+
"plugins": [
25+
{
26+
"transform": "typescript-transform-paths"
27+
},
28+
{
29+
"transform": "typescript-transform-paths",
30+
"afterDeclarations": true
31+
}
32+
]
33+
}
34+
}

test/tests/extras.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { execSync } from "child_process";
1010
* ****************************************************************************************************************** */
1111

1212
describe(`Extra Tests`, () => {
13+
const esmProjectRoot = ts.normalizePath(path.join(projectsPaths, "esm"));
1314
const projectRoot = ts.normalizePath(path.join(projectsPaths, "extras"));
1415
const indexFile = ts.normalizePath(path.join(projectRoot, "src/index.ts"));
1516
const tsConfigFile = ts.normalizePath(path.join(projectRoot, "tsconfig.json"));
@@ -33,9 +34,20 @@ describe(`Extra Tests`, () => {
3334
}
3435
});
3536

36-
test(`Register script transforms with ts-node`, () => {
37-
const res = execSync("ts-node src/index.ts", { cwd: projectRoot }).toString();
38-
expect(res).toMatch(/^null($|\r?\n)/);
37+
describe(`Register script transforms with ts-node`, () => {
38+
test(`CommonJS`, () => {
39+
const res = execSync("ts-node src/index.ts", { cwd: projectRoot }).toString();
40+
expect(res).toMatch(/^null($|\r?\n)/);
41+
});
42+
43+
// See: https://github.com/LeDDGroup/typescript-transform-paths/issues/134
44+
test(`ESM`, () => {
45+
const res = execSync(
46+
`node --no-warnings --loader typescript-transform-paths/esm --es-module-specifier-resolution=node src/index.ts`,
47+
{ cwd: esmProjectRoot }
48+
).toString();
49+
expect(res).toMatch(/^null($|\r?\n)/);
50+
});
3951
});
4052
});
4153
});

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
"compilerOptions": {
77
"rootDir": "src",
88
"outDir": "dist",
9-
"declaration": true
109
}
1110
}

0 commit comments

Comments
 (0)