Skip to content

Commit

Permalink
refactor: refactor only support esm type:module
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyingchun committed May 29, 2024
1 parent 3c9b87c commit a4d3784
Show file tree
Hide file tree
Showing 28 changed files with 313 additions and 227 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-pots-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperse/ts-node-paths": patch
---

refactor only support `esm` type:module
8 changes: 4 additions & 4 deletions .github/workflows/ci-integrity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ jobs:
run: |
yarn lint
- name: Unit tests
run: |
yarn test
- name: Build
run: |
yarn build
- name: Unit tests
run: |
yarn test
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
}
43 changes: 19 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,36 +119,34 @@ This package reads the `tsconfig.json` file (and is capable to find values if th
}
```

### ENV

Path to tsconfig file. Environment: `TS_NODE_PATHS_PROJECT`

```ts
runTsScript(
program,
{
env: {
TS_NODE_PATHS_PROJECT: 'tsconfig.json',
},
},
...args
);
```

The fields listed in the example of above are all required in order to the correct working of the package.

### ...with **ESM** `type:module` projects

- scripts
- scripts (node 20+)

```json
// node 20+ using --import
{
"serve": "yarn node --import=@armit/path-alias/register ./config/dev-server.ts"
"serve": "yarn node --import=@hyperse/ts-node-paths/register ./config/dev-server.ts"
}
```

- Execute the source code with **ts-node:**

node \
--loader @hyperse/ts-node-paths/esm \
./src/index.ts

```
```

- Execute the transpiled code:
```bash
node \
--loader @hyperse/ts-node-paths/esm \
./dist/index.js
```

## Utilities

This package includes `dotnet` package, so if you want, create a `.env` file in your current working directory.
Expand Down Expand Up @@ -224,13 +222,10 @@ Optionally receives as second parameter an object with this options:
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.base.json",
"ts-node": {
"files": true
},
"compilerOptions": {
// If the code contains import 'events' and it coincidentally matches the paths baseUrl /src/events directory,
// it may cause the built-in events module to be incorrectly resolved as a relative module of the project.
// FIXME: recommmand config baseUrl:'./' Instead of use `./src`
// NOTE: recommmand config baseUrl:'./' Instead of use `./src`
// Avoid run into issue of "builtin module `events` wrong resolved as `./src/events`"
"baseUrl": "./",
"allowJs": false,
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
},
"type": "module",
"exports": {
"./cjs": {
"require": "./dist/register/index.cjs"
},
"./esm": {
"import": "./dist/loader/index.mjs"
"import": "./dist/loader/esm.js"
},
"./register": {
"import": "./dist/loader/register.mjs"
"import": "./dist/loader/register.js"
},
".": {
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -64,10 +61,12 @@
}
},
"dependencies": {
"colorette": "^2.0.20",
"dotenv": "^16.4.5",
"get-tsconfig": "^4.7.5",
"module-alias": "^2.2.3",
"pkg-up": "^5.0.0",
"ts-node": "11.0.0-beta.1",
"tsconfig-paths": "^4.2.0"
},
"devDependencies": {
Expand All @@ -82,12 +81,12 @@
"commitizen": "4.3.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "^9.3.0",
"execa": "^9.1.0",
"husky": "9.0.11",
"is-ci": "3.0.1",
"lint-staged": "15.2.5",
"npm-run-all": "^4.1.5",
"shell-quote": "1.8.1",
"ts-node": "11.0.0-beta.1",
"tsup": "^8.0.2",
"tsx": "^4.11.0",
"typescript": "^5.4.5",
Expand All @@ -101,4 +100,4 @@
"access": "public"
},
"packageManager": "yarn@4.2.2"
}
}
21 changes: 5 additions & 16 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,25 @@ async function buildAll() {
clean: true,
outExtension: undefined,
},
'src/register/index.ts': {
format: ['cjs'],
entry: 'register/index',
dts: false,
clean: false,
outExtension() {
return {
js: `.cjs`,
};
},
},
'src/loader/index.mts': {
'src/loader/esm.ts': {
format: ['esm'],
entry: 'loader/index',
entry: 'loader/esm',
dts: false,
clean: false,
outExtension() {
return {
js: `.mjs`,
js: `.js`,
};
},
},
'src/loader/register.mts': {
'src/loader/register.ts': {
format: ['esm'],
entry: 'loader/register',
dts: false,
clean: false,
outExtension() {
return {
js: `.mjs`,
js: `.js`,
};
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { HYPERSE_TS_NODE, HYPERSE_TS_NODE_PATHS } from './constants.js';
import { pathAlias } from './path-alias.js';
import { leftReplacer } from './tool/left-replacer.js';
import { leftReplacer } from './tool/leftReplacer.js';

/**
* Checks if this library is already used in runtime.
Expand Down
24 changes: 15 additions & 9 deletions src/loader/index.mts → src/loader/esm.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import type { LoadFunction, ResolveFn } from 'ts-node/esm';
// Use the "load" function of ts-node only if applied
import { load as loadTs, resolve as resolveTs } from 'ts-node/esm';
import {
load as loadTs,
LoadFn,
resolve as resolveTs,
ResolveFn,
} from 'ts-node/esm';
import { pathToFileURL } from 'url';
import { pathAlias } from '../path-alias.js';
import { createMatchPath } from './create-match-path.js';
import { replace } from './replace.mjs';
import { createMatchPath } from '../tool/createMatchPath.js';
import { replace } from '../tool/replace.js';
import '../types/ts-node.d.js';

pathAlias.showInConsole();

const matchPath = createMatchPath(pathAlias.opts.baseUrl, pathAlias.opts.paths);

export function load(
export async function load(
url: string,
context: { conditions: string[]; format: string },
defaultLoad: LoadFunction
defaultLoad: LoadFn
): Promise<unknown> {
const isTsNode = pathAlias.checkTsNode(url);
if (isTsNode) {
Expand All @@ -25,7 +28,11 @@ export function load(
}

// Use the ts-node mechanism only if applied
export const resolve: ResolveFn = (specifier, context, defaultResolve) => {
export const resolve: ResolveFn = async (
specifier,
context,
defaultResolve
) => {
const isTsNode = pathAlias.checkTsNode(specifier, context);
if (isTsNode) {
// USE TS-NODE TO HANDLE THE INCOMING MODULES
Expand Down Expand Up @@ -64,7 +71,6 @@ export const resolve: ResolveFn = (specifier, context, defaultResolve) => {
}
}
}

return resolveTs(specifier, context, defaultResolve);
} else {
// Use node directly, skipping ts-node
Expand Down
19 changes: 0 additions & 19 deletions src/loader/register.mts

This file was deleted.

19 changes: 19 additions & 0 deletions src/loader/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { register } from 'node:module';
import { pathToFileURL } from 'node:url';
import { pathAlias } from '../path-alias.js';
import { showWarns } from '../tool/showWarns.js';

process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});

if (process.env.TS_NODE_PROJECT) {
showWarns('USING `TS_NODE_PATHS_PROJECT` instead ');
delete process.env.TS_NODE_PROJECT;
}

process.env.TS_NODE_SKIP_PROJECT = 'true';
process.env.TS_NODE_COMPILER_OPTIONS = JSON.stringify(pathAlias.opts);

register('@hyperse/ts-node-paths/esm', pathToFileURL('./'));
Loading

0 comments on commit a4d3784

Please sign in to comment.