Skip to content

Commit

Permalink
feat: esm & cjs loaders (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 24, 2023
1 parent f8837b7 commit e46366d
Show file tree
Hide file tree
Showing 30 changed files with 1,561 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
timeout-minutes: 10
timeout-minutes: 15

steps:
- name: Checkout
Expand Down
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,33 @@ tsx --no-cache ./file.ts

`tsx` is a standalone binary designed to be used in place of `node`, but sometimes you'll want to use `node` directly. For example, when adding TypeScript & ESM support to npm-installed binaries.

To use `tsx` as a Node.js loader, simply pass it in to the [`--loader`](https://nodejs.org/api/esm.html#loaders) flag.

> Note: The loader is limited to adding support for loading TypeScript/ESM files. CLI features such as _watch mode_ or suppressing "experimental feature" warnings will not be available.
To use `tsx` as a Node.js loader, pass it in to the [`--loader`](https://nodejs.org/api/esm.html#loaders) flag. This will add TypeScript & ESM support for both ESM and CommonJS contexts.

```sh
# As a CLI flag
node --loader tsx ./file.ts
```

# As an environment variable
Or as an environment variable:
```sh
NODE_OPTIONS='--loader tsx' node ./file.ts
```

> Tip: In rare circumstances, you might be limited to using the [`-r, --require`](https://nodejs.org/api/cli.html#-r---require-module) flag.
>
> You can use [`@esbuild-kit/cjs-loader`](https://github.com/esbuild-kit/cjs-loader), but transformations will only be applied to `require()` (not `import`).
> **Note:** The loader is limited to adding support for loading TypeScript/ESM files. CLI features such as _watch mode_ or suppressing "experimental feature" warnings will not be available.
#### ESM only loader

If you only need to add TypeScript support in a Module context, you can use the ESM loader:

```sh
node --loader tsx/esm ./file.ts
```

#### CommonJS only loader
If you only need to add TypeScript & ESM support in a CommonJS context, you can use the CJS loader:

```sh
node --require tsx/cjs ./file.ts
```

### Hashbang

Expand All @@ -194,14 +205,6 @@ $ ./file.ts hello
argv: [ 'hello' ]
```

## Dependencies

#### [@esbuild-kit/esm-loader](https://github.com/esbuild-kit/esm-loader)
Node.js Loader to transform TypeScript to ESM.

#### [@esbuild-kit/cjs-loader](https://github.com/esbuild-kit/cjs-loader)
Node.js `require()` hook to transform TypeScript & ESM to CommonJS.

<br>

<p align="center">
Expand Down
26 changes: 17 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@
],
"exports": {
"./package.json": "./package.json",
".": "./dist/loader.js",
"./cli": "./dist/cli.js",
".": "./dist/loader.mjs",
"./cjs": "./dist/cjs/index.cjs",
"./esm": "./dist/esm/index.mjs",
"./cli": "./dist/cli.mjs",
"./source-map": "./dist/source-map.cjs",
"./suppress-warnings": "./dist/suppress-warnings.cjs",
"./preflight": "./dist/preflight.cjs",
"./repl": "./dist/repl.js"
"./repl": "./dist/repl.mjs"
},
"bin": "./dist/cli.js",
"bin": "./dist/cli.mjs",
"scripts": {
"prepare": "pnpm simple-git-hooks",
"build": "pkgroll --target=node12.19 --minify",
"lint": "eslint --cache .",
"type-check": "tsc --noEmit",
"test": "pnpm build && node ./dist/cli.js tests/index.ts",
"test": "pnpm build && node ./dist/cli.mjs tests/index.ts",
"prepack": "pnpm build && clean-pkg-json"
},
"simple-git-hooks": {
Expand All @@ -45,37 +48,42 @@
"*.{js,ts,mjs,mts,cjs,cts,json}": "pnpm lint"
},
"dependencies": {
"@esbuild-kit/cjs-loader": "^2.4.4",
"@esbuild-kit/core-utils": "^3.3.2",
"@esbuild-kit/esm-loader": "^2.6.5"
"esbuild": "~0.18.20",
"get-tsconfig": "^4.7.2",
"source-map-support": "^0.5.21"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
},
"devDependencies": {
"@ampproject/remapping": "^2.2.1",
"@pvtnbr/eslint-config": "^0.37.0",
"@types/cross-spawn": "^6.0.3",
"@types/node": "^20.6.0",
"@types/react": "^18.2.21",
"@types/semver": "^7.5.1",
"@types/source-map-support": "^0.5.7",
"cachedir": "^2.4.0",
"chokidar": "^3.5.3",
"clean-pkg-json": "^1.2.0",
"cleye": "^1.3.2",
"cross-spawn": "^7.0.3",
"es-module-lexer": "^1.3.1",
"eslint": "^8.49.0",
"execa": "^8.0.1",
"fs-fixture": "^1.2.0",
"get-node": "^14.2.1",
"kolorist": "^1.8.0",
"lint-staged": "^14.0.1",
"magic-string": "^0.30.3",
"manten": "^1.1.0",
"node-pty": "^1.0.0",
"outdent": "^0.8.0",
"pkgroll": "^1.11.0",
"pkgroll": "^1.11.1",
"semver": "^7.5.4",
"simple-git-hooks": "^2.9.0",
"strip-ansi": "^7.1.0",
"type-fest": "^4.3.1",
"type-flag": "^3.0.0",
"typescript": "^5.2.2"
},
Expand Down

0 comments on commit e46366d

Please sign in to comment.