Skip to content

Commit 0792067

Browse files
authored
ts-node-esm / --esm to spawn a child process; decouple config loading from create(); fix pluggable dep resolution (#1655)
* WIP * lint-fix * WIP * it works! * Update index.ts * Move `preferTsExts` from `RegisterOptions` to `CreateOptions` * fix * fix * fix tests * fix? * fix * fix? * fix? * fix! * fix * fix!! * fix * fix... * tweak test lib's suite delimiter to match ava's * fix * fix * docs, fixes * fix #1662 and add tests * lint-fix * test cleanup, remove or cleanup version checks, skip failing tsconfig "extends" tests on TS 2.7 * ensure tests are forced to install and use most recent ts-node tarball and cannot accidentally use project-root nor outdated tarball * fix absence of fs method on old node
1 parent f35a120 commit 0792067

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1139
-231
lines changed

ava.config.cjs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const expect = require('expect');
2+
const { createRequire } = require('module');
3+
4+
module.exports = {
5+
files: ['dist/test/**/*.spec.js'],
6+
failWithoutAssertions: false,
7+
environmentVariables: {
8+
ts_node_install_lock: `id-${Math.floor(Math.random() * 10e9)}`,
9+
// Force jest expect() errors to generate colorized strings, makes output more readable.
10+
// Delete the env var within ava processes via `require` option below.
11+
// This avoids passing it to spawned processes under test, which would negatively affect
12+
// their behavior.
13+
FORCE_COLOR: '3',
14+
},
15+
require: ['./src/test/remove-env-var-force-color.js'],
16+
timeout: '300s',
17+
concurrency: 1,
18+
};
19+
20+
{
21+
/*
22+
* Tests *must* install and use our most recent ts-node tarball.
23+
* We must prevent them from accidentally require-ing a different version of
24+
* ts-node, from either node_modules or tests/node_modules
25+
*/
26+
27+
const { existsSync } = require('fs');
28+
const rimraf = require('rimraf');
29+
const { resolve } = require('path');
30+
31+
remove(resolve(__dirname, 'node_modules/ts-node'));
32+
remove(resolve(__dirname, 'tests/node_modules/ts-node'));
33+
34+
// Prove that we did it correctly
35+
expect(() => {createRequire(resolve(__dirname, 'tests/foo.js')).resolve('ts-node')}).toThrow();
36+
37+
function remove(p) {
38+
if(existsSync(p)) rimraf.sync(p, {recursive: true})
39+
}
40+
}

ava.config.js

-14
This file was deleted.

child-loader.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { fileURLToPath } from 'url';
2+
import { createRequire } from 'module';
3+
const require = createRequire(fileURLToPath(import.meta.url));
4+
5+
/** @type {import('./dist/child-loader')} */
6+
const childLoader = require('./dist/child/child-loader');
7+
export const { resolve, load, getFormat, transformSource } = childLoader;

dist-raw/node-primordials.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
22
ArrayFrom: Array.from,
33
ArrayIsArray: Array.isArray,
4-
ArrayPrototypeJoin: (obj, separator) => Array.prototype.join.call(obj, separator),
54
ArrayPrototypeShift: (obj) => Array.prototype.shift.call(obj),
65
ArrayPrototypeForEach: (arr, ...rest) => Array.prototype.forEach.apply(arr, rest),
76
ArrayPrototypeIncludes: (arr, ...rest) => Array.prototype.includes.apply(arr, rest),

package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"./dist/bin-script.js": "./dist/bin-script.js",
1616
"./dist/bin-cwd": "./dist/bin-cwd.js",
1717
"./dist/bin-cwd.js": "./dist/bin-cwd.js",
18+
"./dist/bin-esm": "./dist/bin-esm.js",
19+
"./dist/bin-esm.js": "./dist/bin-esm.js",
1820
"./register": "./register/index.js",
1921
"./register/files": "./register/files.js",
2022
"./register/transpile-only": "./register/transpile-only.js",
@@ -23,6 +25,7 @@
2325
"./esm.mjs": "./esm.mjs",
2426
"./esm/transpile-only": "./esm/transpile-only.mjs",
2527
"./esm/transpile-only.mjs": "./esm/transpile-only.mjs",
28+
"./child-loader.mjs": "./child-loader.mjs",
2629
"./transpilers/swc": "./transpilers/swc.js",
2730
"./transpilers/swc-experimental": "./transpilers/swc-experimental.js",
2831
"./node10/tsconfig.json": "./node10/tsconfig.json",
@@ -33,10 +36,11 @@
3336
"types": "dist/index.d.ts",
3437
"bin": {
3538
"ts-node": "dist/bin.js",
36-
"ts-script": "dist/bin-script-deprecated.js",
37-
"ts-node-script": "dist/bin-script.js",
3839
"ts-node-cwd": "dist/bin-cwd.js",
39-
"ts-node-transpile-only": "dist/bin-transpile.js"
40+
"ts-node-esm": "dist/bin-esm.js",
41+
"ts-node-script": "dist/bin-script.js",
42+
"ts-node-transpile-only": "dist/bin-transpile.js",
43+
"ts-script": "dist/bin-script-deprecated.js"
4044
},
4145
"files": [
4246
"/transpilers/",
@@ -46,6 +50,7 @@
4650
"/register/",
4751
"/esm/",
4852
"/esm.mjs",
53+
"/child-loader.mjs",
4954
"/LICENSE",
5055
"/tsconfig.schema.json",
5156
"/tsconfig.schemastore-schema.json",

src/bin-esm.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import { main } from './bin';
4+
5+
main(undefined, { '--esm': true });

0 commit comments

Comments
 (0)