Skip to content

Commit

Permalink
feat: add esm-register for node>20.6 (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeliex committed Jan 20, 2024
1 parent df125c8 commit 23e511c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ dist
#/target
Cargo.lock

# idea
.idea/

*.node
lib
artifacts
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Run TypeScript with node, without compilation or typechecking:
```bash
npm i -D @swc-node/register
node -r @swc-node/register script.ts
node --loader @swc-node/register/esm script.ts # for esm project
node --import @swc-node/register/esm-register script.ts # for esm project with node>=20.6
node --loader @swc-node/register/esm script.ts # for esm project with node<=20.5, deprecated
```

Set environment variable SWCRC=true when you would like to load .swcrc file
Expand All @@ -26,6 +27,14 @@ Set environment variable SWCRC=true when you would like to load .swcrc file
SWCRC=true node -r @swc-node/register script.ts
```

```typescript
#!/usr/bin/env node --import swc-register-esm

// your code
```
run with shebang, add `TS_NODE_PROJECT=null`(`#!/usr/bin/env TS_NODE_PROJECT=null node --import swc-register-esm`) to use ignore tsconfig.json
## @swc-node/core
Fastest `TypeScript` transformer.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@types/babel__core": "^7.20.1",
"@types/benchmark": "^2.1.2",
"@types/lodash": "^4.14.197",
"@types/node": "^20.5.0",
"@types/node": "^20.11.0",
"@types/sinon": "^10.0.16",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/register/esm-register.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { register } from 'node:module'
import { pathToFileURL } from 'node:url'

register('@swc-node/register/esm', pathToFileURL('./').toString())
3 changes: 3 additions & 0 deletions packages/register/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
},
"./esm": {
"import": "./esm/esm.mjs"
},
"./esm-register": {
"import": "./esm/esm-register.mjs"
}
}
}
2 changes: 1 addition & 1 deletion packages/register/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"outDir": "esm"
},
"include": [],
"files": ["./esm.mts", "register.d.ts", "./read-default-tsconfig.d.ts"]
"files": ["./esm.mts", "./esm-register.mts", "register.d.ts", "./read-default-tsconfig.d.ts"]
}
68 changes: 37 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23e511c

Please sign in to comment.