Skip to content

Commit

Permalink
V11 breaking changes (#1760)
Browse files Browse the repository at this point in the history
* remove node12 stuff

* bump minimum supported TS version to 3.8 to match DT

* install source-map-support in register, not create

* Remove ts.createIncrementalProgram fallback to ts.createEmitAndSemanticDiagnosticsBuilderProgram

* Make enableExperimentalEsmLoaderInterop() on by default; remove conditional code

* Remove deprecated `ts-script` bin

* make bin.ts `main()` export @internal

* change `nodeEval` function signature to accept `context: Context` instead of `: any`

* fix version check

* Add node18 @tsconfig/bases; remove node10 and node12; change dependencies to be wildcard; will respect whatever version you want to use

* More stuff for bumping minimum supported node12 -> node14

* remove test workaround that avoided old bug in node nightly

* fix test failing due to `lib` change in @tsconfig/bases node18

* merge fixes

* fix
  • Loading branch information
cspotcode authored Jul 14, 2022
1 parent 0e0da59 commit 97f9afd
Show file tree
Hide file tree
Showing 29 changed files with 604 additions and 850 deletions.
51 changes: 16 additions & 35 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,91 +51,72 @@ jobs:
matrix:
os: [ubuntu, windows]
# Don't forget to add all new flavors to this list!
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
include:
# Node 12.15
- flavor: 1
node: 12.15
nodeFlag: 12_15
typescript: latest
typescriptFlag: latest
# Node 12.16
# Earliest version that supports getFormat, etc hooks: https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#12.16.0
- flavor: 2
node: 12.16
nodeFlag: 12_16
typescript: latest
typescriptFlag: latest
# Node 12
- flavor: 3
node: 12
nodeFlag: 12
typescript: latest
typescriptFlag: latest
# Node 14.13.0
# To test ESM builtin module resolution immediately before a node behavioral change: https://github.com/TypeStrong/ts-node/issues/1130
- flavor: 4
- flavor: 1
node: 14.13.0
nodeFlag: 14_13_0
typescript: latest
typescriptFlag: latest
# Node 14
- flavor: 5
- flavor: 2
node: 14
nodeFlag: 14
typescript: latest
typescriptFlag: latest
- flavor: 6
- flavor: 3
node: 14
nodeFlag: 14
typescript: 2.7
typescriptFlag: 2_7
- flavor: 7
typescript: 4.0
typescriptFlag: 4_0
- flavor: 4
node: 14
nodeFlag: 14
typescript: next
typescriptFlag: next
# Node 16
# Node 16.11.1
# Earliest version that supports old ESM Loader Hooks API: https://github.com/TypeStrong/ts-node/pull/1522
- flavor: 8
- flavor: 5
node: 16.11.1
nodeFlag: 16_11_1
typescript: latest
typescriptFlag: latest
- flavor: 9
- flavor: 6
node: 16
nodeFlag: 16
typescript: latest
typescriptFlag: latest
downgradeNpm: true
- flavor: 10
- flavor: 7
node: 16
nodeFlag: 16
typescript: 2.7
typescriptFlag: 2_7
typescript: 4.0
typescriptFlag: 4_0
downgradeNpm: true
- flavor: 11
- flavor: 8
node: 16
nodeFlag: 16
typescript: next
typescriptFlag: next
downgradeNpm: true
# Node 18
- flavor: 12
- flavor: 9
node: 18
nodeFlag: 18
typescript: latest
typescriptFlag: latest
downgradeNpm: true
- flavor: 13
- flavor: 10
node: 18
nodeFlag: 18
typescript: next
typescriptFlag: next
downgradeNpm: true
# Node nightly
- flavor: 14
- flavor: 11
node: nightly
nodeFlag: nightly
typescript: latest
Expand Down
4 changes: 1 addition & 3 deletions ava.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ module.exports = {
NODE_PATH: ''
},
require: ['./src/test/remove-env-var-force-color.js'],
nodeArguments: semver.gte(process.version, '14.0.0')
? ['--loader', './src/test/test-loader.mjs', '--no-warnings']
: [],
nodeArguments: ['--loader', './src/test/test-loader.mjs', '--no-warnings'],
timeout: '300s',
concurrency: 1,
};
Expand Down
2 changes: 1 addition & 1 deletion dist-raw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ in a factory function, we will not indent the function body, to avoid whitespace

One obvious problem with this approach: the code has been pulled from one version of node, whereas users of ts-node
run multiple versions of node.
Users running node 12 may see that ts-node behaves like node 14, for example.
Users running node 14 may see that ts-node behaves like node 18, for example.

## `raw` directory

Expand Down
3 changes: 1 addition & 2 deletions dist-raw/node-internal-modules-cjs-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) {

ObjectDefineProperty(object, name, {
get: () => {
// Node 12 hack; remove when we drop node12 support
const lib = (dummyModule.require || require)(name);
const lib = dummyModule.require(name);

// Disable the current getter/setter and set up a new
// non-enumerable property.
Expand Down
16 changes: 8 additions & 8 deletions dist-raw/node-internal-modules-cjs-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,11 @@ const Module_resolveFilename = function _resolveFilename(request, parent, isMain
paths = Module._resolveLookupPaths(request, parent);
}

// if (parent?.filename) {
// node 12 hack
if (parent != null && parent.filename) {
if (parent?.filename) {
if (request[0] === '#') {
const pkg = readPackageScope(parent.filename) || {};

// if (pkg.data?.imports != null) {
// node 12 hack
if (pkg.data != null && pkg.data.imports != null) {
if (pkg.data?.imports != null) {
try {
return finalizeEsmResolution(
packageImportsResolve(request, pathToFileURL(parent.filename),
Expand Down Expand Up @@ -559,11 +555,15 @@ return {
/**
* copied from Module._extensions['.js']
* https://github.com/nodejs/node/blob/v15.3.0/lib/internal/modules/cjs/loader.js#L1113-L1120
* Assert that script can be loaded as CommonJS when we attempt to require it.
* If it should be loaded as ESM, throw ERR_REQUIRE_ESM like node does.
*
* @param {import('../src/index').Service} service
* @param {NodeJS.Module} module
* @param {string} filename
*/
function assertScriptCanLoadAsCJSImpl(service, module, filename) {
function assertScriptCanLoadAsCJS(service, module, filename) {
const pkg = readPackageScope(filename);

// ts-node modification: allow our configuration to override
Expand All @@ -588,6 +588,6 @@ function assertScriptCanLoadAsCJSImpl(service, module, filename) {

module.exports = {
createCjsLoader,
assertScriptCanLoadAsCJSImpl,
assertScriptCanLoadAsCJS,
readPackageScope
};
2 changes: 0 additions & 2 deletions dist-raw/node-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ function parseArgv(argv) {
'--preserve-symlinks-main': Boolean,
'--input-type': String,
'--experimental-specifier-resolution': String,
// Legacy alias for node versions prior to 12.16
'--es-module-specifier-resolution': '--experimental-specifier-resolution',
'--experimental-policy': String,
'--conditions': [String],
'--pending-deprecation': Boolean,
Expand Down
3 changes: 3 additions & 0 deletions node18/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node18/tsconfig.json"
}
11 changes: 9 additions & 2 deletions package-lock.json

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

20 changes: 8 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@
"./child-loader.mjs": "./child-loader.mjs",
"./transpilers/swc": "./transpilers/swc.js",
"./transpilers/swc-experimental": "./transpilers/swc-experimental.js",
"./node10/tsconfig.json": "./node10/tsconfig.json",
"./node12/tsconfig.json": "./node12/tsconfig.json",
"./node14/tsconfig.json": "./node14/tsconfig.json",
"./node16/tsconfig.json": "./node16/tsconfig.json"
"./node16/tsconfig.json": "./node16/tsconfig.json",
"./node18/tsconfig.json": "./node18/tsconfig.json"
},
"types": "dist/index.d.ts",
"bin": {
"ts-node": "dist/bin.js",
"ts-node-cwd": "dist/bin-cwd.js",
"ts-node-esm": "dist/bin-esm.js",
"ts-node-script": "dist/bin-script.js",
"ts-node-transpile-only": "dist/bin-transpile.js",
"ts-script": "dist/bin-script-deprecated.js"
"ts-node-transpile-only": "dist/bin-transpile.js"
},
"files": [
"/transpilers/",
Expand All @@ -55,10 +53,9 @@
"/LICENSE",
"/tsconfig.schema.json",
"/tsconfig.schemastore-schema.json",
"/node10/",
"/node12/",
"/node14/",
"/node16/"
"/node16/",
"/node18/"
],
"scripts": {
"lint": "dprint check",
Expand Down Expand Up @@ -159,10 +156,9 @@
},
"dependencies": {
"@cspotcode/source-map-support": "^0.8.0",
"@tsconfig/node10": "^1.0.7",
"@tsconfig/node12": "^1.0.7",
"@tsconfig/node14": "^1.0.0",
"@tsconfig/node16": "^1.0.2",
"@tsconfig/node14": "*",
"@tsconfig/node16": "*",
"@tsconfig/node18": "*",
"acorn": "^8.4.1",
"acorn-walk": "^8.1.1",
"arg": "^4.1.0",
Expand Down
10 changes: 0 additions & 10 deletions src/bin-script-deprecated.ts

This file was deleted.

22 changes: 2 additions & 20 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import { findAndReadConfig } from './configuration';
*
* The functions are intentionally given uncreative names and left in the same order as the original code, to make a
* smaller git diff.
*
* @internal
*/
export function main(
argv: string[] = process.argv.slice(2),
Expand Down Expand Up @@ -97,19 +99,6 @@ export function bootstrap(state: BootstrapState) {

function parseArgv(argv: string[], entrypointArgs: Record<string, any>) {
arg ??= require('arg');
// HACK: technically, this function is not marked @internal so it's possible
// that libraries in the wild are doing `require('ts-node/dist/bin').main({'--transpile-only': true})`
// We can mark this function @internal in next major release.
// For now, rewrite args to avoid a breaking change.
entrypointArgs = { ...entrypointArgs };
for (const key of Object.keys(entrypointArgs)) {
entrypointArgs[
key.replace(
/([a-z])-([a-z])/g,
(_$0, $1, $2: string) => `${$1}${$2.toUpperCase()}`
)
] = entrypointArgs[key];
}

const args = {
...entrypointArgs,
Expand Down Expand Up @@ -742,13 +731,6 @@ let guaranteedNonexistentDirectorySuffix = 0;
* https://stackoverflow.com/questions/59865584/how-to-invalidate-cached-require-resolve-results
*/
function requireResolveNonCached(absoluteModuleSpecifier: string) {
// node <= 12.1.x fallback: The trick below triggers a node bug on old versions.
// On these old versions, pollute the require cache instead. This is a deliberate
// ts-node limitation that will *rarely* manifest, and will not matter once node 12
// is end-of-life'd on 2022-04-30
const isSupportedNodeVersion = versionGteLt(process.versions.node, '12.2.0');
if (!isSupportedNodeVersion) return require.resolve(absoluteModuleSpecifier);

const { dir, base } = parsePath(absoluteModuleSpecifier);
const relativeModuleSpecifier = `./${base}`;

Expand Down
6 changes: 0 additions & 6 deletions src/child/spawn-child.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { BootstrapState } from '../bin';
import { spawn } from 'child_process';
import { pathToFileURL } from 'url';
import { versionGteLt } from '../util';
import { argPrefix, compress } from './argv-payload';

/**
Expand All @@ -11,11 +10,6 @@ import { argPrefix, compress } from './argv-payload';
* the child process.
*/
export function callInChild(state: BootstrapState) {
if (!versionGteLt(process.versions.node, '12.17.0')) {
throw new Error(
'`ts-node-esm` and `ts-node --esm` require node version 12.17.0 or newer.'
);
}
const child = spawn(
process.execPath,
[
Expand Down
2 changes: 0 additions & 2 deletions src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ export function registerAndCreateEsmHooks(opts?: RegisterOptions) {
}

export function createEsmHooks(tsNodeService: Service) {
tsNodeService.enableExperimentalEsmLoaderInterop();

// Custom implementation that considers additional file extensions and automatically adds file extensions
const nodeResolveImplementation = tsNodeService.getNodeEsmResolver();
const nodeGetFormatImplementation = tsNodeService.getNodeEsmGetFormat();
Expand Down
Loading

0 comments on commit 97f9afd

Please sign in to comment.