Skip to content

Commit

Permalink
feat: drop support for outdated Node.js versions (#324)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: only Node.js LTS versions v18 and up are now supported
  • Loading branch information
privatenumber committed Nov 9, 2023
1 parent d132c3f commit acb709c
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 159 deletions.
4 changes: 1 addition & 3 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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
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
10 changes: 1 addition & 9 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport } from '../source-map';
import { transformSync, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { nodeSupportsImport, supportsNodePrefix } from '../utils/node-features';

const isRelativePathPattern = /^\.{1,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
Expand Down Expand Up @@ -66,7 +65,7 @@ const transformer = (

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

if (filePath.endsWith('.cjs') && nodeSupportsImport) {
if (filePath.endsWith('.cjs')) {
const transformed = transformDynamicImport(filePath, code);
if (transformed) {
code = applySourceMap(transformed, filePath);
Expand Down Expand Up @@ -126,15 +125,8 @@ 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);
}

if (
tsconfigPathsMatcher

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';
113 changes: 0 additions & 113 deletions src/esm/loaders-deprecated.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import type {
import type { TransformOptions } from 'esbuild';
import { transform, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import {
supportsNodePrefix,
} from '../utils/node-features';
import {
applySourceMap,
tsconfigPathsMatcher,
Expand Down Expand Up @@ -164,12 +161,6 @@ export const resolve: resolve = async function (
defaultResolve,
recursiveCall,
) {
// Added in v12.20.0
// https://nodejs.org/api/esm.html#esm_node_imports
if (!supportsNodePrefix && specifier.startsWith('node:')) {
specifier = specifier.slice(5);
}

// If directory, can be index.js, index.ts, etc.
if (isDirectoryPattern.test(specifier)) {
return await tryDirectory(specifier, context, defaultResolve);
Expand Down
18 changes: 0 additions & 18 deletions src/utils/node-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ const compareNodeVersion = (version: Version) => (
|| nodeVersion[2] - version[2]
);

export const nodeSupportsImport = (
// v13.2.0 and higher
compareNodeVersion([13, 2, 0]) >= 0

// 12.20.0 ~ 13.0.0
|| (
compareNodeVersion([12, 20, 0]) >= 0
&& compareNodeVersion([13, 0, 0]) < 0
)
);

export const supportsNodePrefix = (
compareNodeVersion([16, 0, 0]) >= 0
|| compareNodeVersion([14, 18, 0]) >= 0
);

export const nodeSupportsDeprecatedLoaders = compareNodeVersion([16, 12, 0]) < 0;

/**
* Node.js loaders are isolated from v20
* https://github.com/nodejs/node/issues/49455#issuecomment-1703812193
Expand Down
7 changes: 1 addition & 6 deletions tests/utils/node-versions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
export const nodeVersions = [
'18',
'20',
...(
(
process.env.CI
&& process.platform !== 'win32'
)
? [
'12.20.0', // CJS named export detection added
'12',
'14',
'16',
'17',
'18',
] as const
: [] as const
),
Expand Down

0 comments on commit acb709c

Please sign in to comment.