Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next major release queue #368

Merged
merged 9 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
blank_issues_enabled: false
contact_links:
- name: 💬 Help / Questions / Discussions
url: https://github.com/esbuild-kit/tsx/discussions
url: https://github.com/privatenumber/tsx/discussions
about: Use GitHub Discussions for anything else

- name: 🚀 Priority Support
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ on:
push:
branches: [develop]
pull_request:
branches: [master, develop, next]
jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
timeout-minutes: 15
os: [ubuntu-latest, windows-latest]
timeout-minutes: 5

steps:
- name: Checkout
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
- Blazing fast on-demand TypeScript & ESM compilation
- Works in both [CommonJS and ESM packages](https://nodejs.org/api/packages.html#type)
- Supports next-gen TypeScript extensions (`.cts` & `.mts`)
- Supports `node:` import prefixes
- Hides experimental feature warnings
- TypeScript REPL
- Resolves `tsconfig.json` [`paths`](https://www.typescriptlang.org/tsconfig#paths)
- Tested on Linux & Windows with Node.js v12~20

> **💡 Protip: Looking to bundle your TypeScript project?**
>
Expand Down Expand Up @@ -45,7 +43,7 @@ How does it compare to [ts-node](https://github.com/TypeStrong/ts-node)? Checkou
tsx strives to:
1. Enhance Node.js with TypeScript compatibility
2. Improve ESM <-> CJS interoperability
3. Support the latest major version of Node.js v12 and up _(likely to change in the future)_
3. Support the [LTS versions of Node.js](https://endoflife.date/nodejs)

## Install

Expand Down Expand Up @@ -106,10 +104,10 @@ To set a custom path, use the `--tsconfig` flag:
tsx --tsconfig ./path/to/tsconfig.custom.json ./file.ts
```

Alternatively, use the `ESBK_TSCONFIG_PATH` environment variable:
Alternatively, use the `TSX_TSCONFIG_PATH` environment variable:

```sh
ESBK_TSCONFIG_PATH=./path/to/tsconfig.custom.json tsx ./file.ts
TSX_TSCONFIG_PATH=./path/to/tsconfig.custom.json tsx ./file.ts
```

### Watch mode
Expand Down Expand Up @@ -149,6 +147,12 @@ Set the `--no-cache` flag to disable the cache:
tsx --no-cache ./file.ts
```

Alternatively, use the `TSX_DISABLE_CACHE` environment variable:

```sh
TSX_DISABLE_CACHE=1 tsx ./file.ts
```

### Node.js Loader

`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.
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"typescript"
],
"license": "MIT",
"repository": "esbuild-kit/tsx",
"repository": "privatenumber/tsx",
"author": {
"name": "Hiroki Osame",
"email": "[email protected]"
Expand Down Expand Up @@ -47,6 +47,9 @@
"lint-staged": {
"*.{js,ts,mjs,mts,cjs,cts,json}": "pnpm lint"
},
"engines": {
"node": ">=18.0.0"
},
"dependencies": {
"esbuild": "~0.18.20",
"get-tsconfig": "^4.7.2",
Expand Down Expand Up @@ -76,7 +79,7 @@
"kolorist": "^1.8.0",
"lint-staged": "^14.0.1",
"magic-string": "^0.30.3",
"manten": "^1.1.0",
"manten": "^1.2.0",
"node-pty": "^1.0.0",
"outdent": "^0.8.0",
"pkgroll": "^1.11.1",
Expand Down
46 changes: 23 additions & 23 deletions pnpm-lock.yaml

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

90 changes: 52 additions & 38 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import {
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport } from '../source-map';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { transformSync, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { nodeSupportsImport, supportsNodePrefix } from '../utils/node-features';
import { isESM } from '../utils/esm-pattern';

const isRelativePathPattern = /^\.{1,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;

const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
process.env.TSX_TSCONFIG_PATH
? {
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
path: path.resolve(process.env.TSX_TSCONFIG_PATH),
config: parseTsconfig(process.env.TSX_TSCONFIG_PATH),
}
: getTsconfig()
);
Expand All @@ -34,44 +34,57 @@ const applySourceMap = installSourceMapSupport();
const extensions = Module._extensions;
const defaultLoader = extensions['.js'];

const transformExtensions = [
'.js',
'.cjs',
const typescriptExtensions = [
'.cts',
'.mjs',
'.mts',
'.ts',
'.tsx',
'.jsx',
];

const transformExtensions = [
'.js',
'.cjs',
'.mjs',
];

const transformer = (
module: Module,
filePath: string,
) => {
const shouldTransformFile = transformExtensions.some(extension => filePath.endsWith(extension));
if (!shouldTransformFile) {
return defaultLoader(module, filePath);
}

/**
* For tracking dependencies in watch mode
*/
// For tracking dependencies in watch mode
if (process.send) {
process.send({
type: 'dependency',
path: filePath,
});
}

const transformTs = typescriptExtensions.some(extension => filePath.endsWith(extension));
const transformJs = transformExtensions.some(extension => filePath.endsWith(extension));
if (!transformTs && !transformJs) {
return defaultLoader(module, filePath);
}

let code = fs.readFileSync(filePath, 'utf8');

if (filePath.endsWith('.cjs') && nodeSupportsImport) {
// Strip source maps if originally disabled
if (shouldStripSourceMap) {
code = stripSourceMap(code);
}

if (filePath.endsWith('.cjs')) {
// Contains native ESM check
const transformed = transformDynamicImport(filePath, code);
if (transformed) {
code = applySourceMap(transformed, filePath);
}
} else {
} else if (
transformTs

// CommonJS file but uses ESM import/export
|| isESM(code)
) {
const transformed = transformSync(
code,
filePath,
Expand Down Expand Up @@ -126,13 +139,12 @@ Object.defineProperty(extensions, '.mjs', {
enumerable: false,
});

// Add support for "node:" protocol
const defaultResolveFilename = Module._resolveFilename.bind(Module);
Module._resolveFilename = (request, parent, isMain, options) => {
// Added in v12.20.0
// https://nodejs.org/api/esm.html#esm_node_imports
if (!supportsNodePrefix && request.startsWith('node:')) {
request = request.slice(5);
// Strip query string
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
request = request.slice(0, queryIndex);
}

if (
Expand Down Expand Up @@ -191,20 +203,22 @@ const resolveTsFilename = (
&& isTsFilePatten.test(parent.filename)
&& tsPath
) {
try {
return defaultResolveFilename(
tsPath[0],
parent,
isMain,
options,
);
} catch (error) {
const { code } = error as NodeError;
if (
code !== 'MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
for (const tryTsPath of tsPath) {
try {
return defaultResolveFilename(
tryTsPath,
parent,
isMain,
options,
);
} catch (error) {
const { code } = error as NodeError;
if (
code !== 'MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/esm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ if (
}

export * from './loaders.js';
export * from './loaders-deprecated.js';
Loading
Loading