Skip to content

Commit

Permalink
feat: Add esm support for ts-node register (closes #134)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Aug 21, 2021
1 parent ebefe66 commit 8185831
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 5 deletions.
17 changes: 17 additions & 0 deletions esm.mjs
@@ -0,0 +1,17 @@
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
const require = createRequire(fileURLToPath(import.meta.url));

/** @type {import('./dist/register')} */
const { register } = require('./dist/register');

/** @type {import('ts-node/dist/esm')} */
import esm from 'ts-node/dist/esm';

const options = register();

export const {
resolve,
getFormat,
transformSource,
} = esm.registerAndCreateEsmHooks(options);
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,8 @@
"types",
"README.md",
"CHANGELOG.md",
"register.js"
"register.js",
"esm.mjs"
],
"devDependencies": {
"@types/jest": "^26.0.24",
Expand Down
9 changes: 9 additions & 0 deletions test/projects/esm/package.json
@@ -0,0 +1,9 @@
{
"private": true,
"name": "@tests/extras-esm",
"version": "0.0.0",
"dependencies": {
"typescript-transform-paths": "link:../../../"
},
"type": "module"
}
1 change: 1 addition & 0 deletions test/projects/esm/src/id.ts
@@ -0,0 +1 @@
export const b = null;
4 changes: 4 additions & 0 deletions test/projects/esm/src/index.ts
@@ -0,0 +1,4 @@
export * from "#identifier";
import { b } from "#identifier";

console.log(b);
34 changes: 34 additions & 0 deletions test/projects/esm/tsconfig.json
@@ -0,0 +1,34 @@
{
"include": [ "src" ],

"ts-node": {
"transpileOnly": true,
"require": [ "typescript-transform-paths/register" ],
},

"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
}
]
}
}
18 changes: 15 additions & 3 deletions test/tests/extras.test.ts
Expand Up @@ -10,6 +10,7 @@ import { execSync } from "child_process";
* ****************************************************************************************************************** */

describe(`Extra Tests`, () => {
const esmProjectRoot = ts.normalizePath(path.join(projectsPaths, "esm"));
const projectRoot = ts.normalizePath(path.join(projectsPaths, "extras"));
const indexFile = ts.normalizePath(path.join(projectRoot, "src/index.ts"));
const tsConfigFile = ts.normalizePath(path.join(projectRoot, "tsconfig.json"));
Expand All @@ -33,9 +34,20 @@ describe(`Extra Tests`, () => {
}
});

test(`Register script transforms with ts-node`, () => {
const res = execSync("ts-node src/index.ts", { cwd: projectRoot }).toString();
expect(res).toMatch(/^null($|\r?\n)/);
describe(`Register script transforms with ts-node`, () => {
test(`CommonJS`, () => {
const res = execSync("ts-node src/index.ts", { cwd: projectRoot }).toString();
expect(res).toMatch(/^null($|\r?\n)/);
});

// See: https://github.com/LeDDGroup/typescript-transform-paths/issues/134
test(`ESM`, () => {
const res = execSync(
`node --no-warnings --loader typescript-transform-paths/esm --es-module-specifier-resolution=node src/index.ts`,
{ cwd: esmProjectRoot }
).toString();
expect(res).toMatch(/^null($|\r?\n)/);
});
});
});
});
1 change: 0 additions & 1 deletion tsconfig.json
Expand Up @@ -6,6 +6,5 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"declaration": true
}
}

0 comments on commit 8185831

Please sign in to comment.