Comparing version 10.5.0 to 10.6.0
@@ -286,3 +286,3 @@ #!/usr/bin/env node | ||
// Prepend `ts-node` arguments to CLI for child processes. | ||
process.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length)); | ||
process.execArgv.push(__filename, ...process.argv.slice(2, process.argv.length - args._.length)); | ||
process.argv = [process.argv[1]] | ||
@@ -289,0 +289,0 @@ .concat(executeEntrypoint ? [scriptPath] : []) |
@@ -31,2 +31,3 @@ /// <reference types="node" /> | ||
url: string; | ||
format?: NodeLoaderHooksFormat; | ||
}>; | ||
@@ -33,0 +34,0 @@ type LoadHook = (url: string, context: { |
@@ -9,2 +9,3 @@ "use strict"; | ||
const util_1 = require("./util"); | ||
const module_1 = require("module"); | ||
const { createResolve, } = require('../dist-raw/node-esm-resolve-implementation'); | ||
@@ -36,3 +37,2 @@ const { defaultGetFormat } = require('../dist-raw/node-esm-default-get-format'); | ||
: { resolve, getFormat, transformSource, load: undefined }; | ||
return hooksAPI; | ||
function isFileUrlOrNodeStyleSpecifier(parsed) { | ||
@@ -43,2 +43,12 @@ // We only understand file:// URLs, but in node, the specifier can be a node-style `./foo` or `foo` | ||
} | ||
/** | ||
* Named "probably" as a reminder that this is a guess. | ||
* node does not explicitly tell us if we're resolving the entrypoint or not. | ||
*/ | ||
function isProbablyEntrypoint(specifier, parentURL) { | ||
return parentURL === undefined && specifier.startsWith('file://'); | ||
} | ||
// Side-channel between `resolve()` and `load()` hooks | ||
const rememberIsProbablyEntrypoint = new Set(); | ||
const rememberResolvedViaCommonjsFallback = new Set(); | ||
async function resolve(specifier, context, defaultResolve) { | ||
@@ -49,9 +59,40 @@ const defer = async () => { | ||
}; | ||
// See: https://github.com/nodejs/node/discussions/41711 | ||
// nodejs will likely implement a similar fallback. Till then, we can do our users a favor and fallback today. | ||
async function entrypointFallback(cb) { | ||
try { | ||
const resolution = await cb(); | ||
if ((resolution === null || resolution === void 0 ? void 0 : resolution.url) && | ||
isProbablyEntrypoint(specifier, context.parentURL)) | ||
rememberIsProbablyEntrypoint.add(resolution.url); | ||
return resolution; | ||
} | ||
catch (esmResolverError) { | ||
if (!isProbablyEntrypoint(specifier, context.parentURL)) | ||
throw esmResolverError; | ||
try { | ||
let cjsSpecifier = specifier; | ||
// Attempt to convert from ESM file:// to CommonJS path | ||
try { | ||
if (specifier.startsWith('file://')) | ||
cjsSpecifier = (0, url_1.fileURLToPath)(specifier); | ||
} | ||
catch { } | ||
const resolution = (0, url_1.pathToFileURL)((0, module_1.createRequire)(process.cwd()).resolve(cjsSpecifier)).toString(); | ||
rememberIsProbablyEntrypoint.add(resolution); | ||
rememberResolvedViaCommonjsFallback.add(resolution); | ||
return { url: resolution, format: 'commonjs' }; | ||
} | ||
catch (commonjsResolverError) { | ||
throw esmResolverError; | ||
} | ||
} | ||
} | ||
const parsed = (0, url_1.parse)(specifier); | ||
const { pathname, protocol, hostname } = parsed; | ||
if (!isFileUrlOrNodeStyleSpecifier(parsed)) { | ||
return defer(); | ||
return entrypointFallback(defer); | ||
} | ||
if (protocol !== null && protocol !== 'file:') { | ||
return defer(); | ||
return entrypointFallback(defer); | ||
} | ||
@@ -61,6 +102,6 @@ // Malformed file:// URL? We should always see `null` or `''` | ||
// TODO file://./foo sets `hostname` to `'.'`. Perhaps we should special-case this. | ||
return defer(); | ||
return entrypointFallback(defer); | ||
} | ||
// pathname is the path to be resolved | ||
return nodeResolveImplementation.defaultResolve(specifier, context, defaultResolve); | ||
return entrypointFallback(() => nodeResolveImplementation.defaultResolve(specifier, context, defaultResolve)); | ||
} | ||
@@ -93,5 +134,17 @@ // `load` from new loader hook API (See description at the top of this file) | ||
const defer = (overrideUrl = url) => defaultGetFormat(overrideUrl, context, defaultGetFormat); | ||
// See: https://github.com/nodejs/node/discussions/41711 | ||
// nodejs will likely implement a similar fallback. Till then, we can do our users a favor and fallback today. | ||
async function entrypointFallback(cb) { | ||
try { | ||
return await cb(); | ||
} | ||
catch (getFormatError) { | ||
if (!rememberIsProbablyEntrypoint.has(url)) | ||
throw getFormatError; | ||
return { format: 'commonjs' }; | ||
} | ||
} | ||
const parsed = (0, url_1.parse)(url); | ||
if (!isFileUrlOrNodeStyleSpecifier(parsed)) { | ||
return defer(); | ||
return entrypointFallback(defer); | ||
} | ||
@@ -105,6 +158,6 @@ const { pathname } = parsed; | ||
if (ext !== '.js' && !tsNodeService.ignored(nativePath)) { | ||
nodeSays = await defer((0, url_1.format)((0, url_1.pathToFileURL)(nativePath + '.js'))); | ||
nodeSays = await entrypointFallback(() => defer((0, url_1.format)((0, url_1.pathToFileURL)(nativePath + '.js')))); | ||
} | ||
else { | ||
nodeSays = await defer(); | ||
nodeSays = await entrypointFallback(defer); | ||
} | ||
@@ -142,4 +195,5 @@ // For files compiled by ts-node that node believes are either CJS or ESM, check if we should override that classification | ||
} | ||
return hooksAPI; | ||
} | ||
exports.createEsmHooks = createEsmHooks; | ||
//# sourceMappingURL=esm.js.map |
@@ -271,3 +271,3 @@ "use strict"; | ||
} | ||
let customTranspiler = undefined; | ||
let createTranspiler; | ||
if (transpiler) { | ||
@@ -279,7 +279,17 @@ if (!transpileOnly) | ||
const transpilerPath = projectLocalResolveHelper(transpilerName, true); | ||
const transpilerFactory = require(transpilerPath).create; | ||
customTranspiler = transpilerFactory({ | ||
service: { options, config, projectLocalResolveHelper }, | ||
...transpilerOptions, | ||
}); | ||
const transpilerFactory = require(transpilerPath) | ||
.create; | ||
createTranspiler = function (compilerOptions) { | ||
return transpilerFactory({ | ||
service: { | ||
options, | ||
config: { | ||
...config, | ||
options: compilerOptions, | ||
}, | ||
projectLocalResolveHelper, | ||
}, | ||
...transpilerOptions, | ||
}); | ||
}; | ||
} | ||
@@ -647,2 +657,3 @@ /** | ||
compilerOptions.module = overrideModuleType; | ||
let customTranspiler = createTranspiler === null || createTranspiler === void 0 ? void 0 : createTranspiler(compilerOptions); | ||
return (code, fileName) => { | ||
@@ -673,7 +684,10 @@ let result; | ||
: createTranspileOnlyGetOutputFunction(ts.ModuleKind.CommonJS); | ||
// [MUST_UPDATE_FOR_NEW_MODULEKIND] | ||
const getOutputForceESM = config.options.module === ts.ModuleKind.ES2015 || | ||
config.options.module === ts.ModuleKind.ES2020 || | ||
(ts.ModuleKind.ES2020 && config.options.module === ts.ModuleKind.ES2020) || | ||
(ts.ModuleKind.ES2022 && config.options.module === ts.ModuleKind.ES2022) || | ||
config.options.module === ts.ModuleKind.ESNext | ||
? undefined | ||
: createTranspileOnlyGetOutputFunction(ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015); | ||
: // [MUST_UPDATE_FOR_NEW_MODULEKIND] | ||
createTranspileOnlyGetOutputFunction(ts.ModuleKind.ES2022 || ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015); | ||
const getOutputTranspileOnly = createTranspileOnlyGetOutputFunction(); | ||
@@ -680,0 +694,0 @@ // Create a simple TypeScript compiler proxy. |
@@ -77,6 +77,16 @@ "use strict"; | ||
}; | ||
const resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile, redirectedReference, options) => { | ||
const resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile, redirectedReference, options, containingFileMode // new impliedNodeFormat is accepted by compilerHost | ||
) => { | ||
// Note: seems to be called with empty typeDirectiveNames array for all files. | ||
// TODO consider using `ts.loadWithTypeDirectiveCache` | ||
return typeDirectiveNames.map((typeDirectiveName) => { | ||
let { resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(typeDirectiveName, containingFile, config.options, host, redirectedReference); | ||
// Copy-pasted from TS source: | ||
const nameIsString = typeof typeDirectiveName === 'string'; | ||
const mode = nameIsString | ||
? undefined | ||
: ts.getModeForFileReference(typeDirectiveName, containingFileMode); | ||
const strName = nameIsString | ||
? typeDirectiveName | ||
: typeDirectiveName.fileName.toLowerCase(); | ||
let { resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(strName, containingFile, config.options, host, redirectedReference, undefined, mode); | ||
if (typeDirectiveName === 'node' && !resolvedTypeReferenceDirective) { | ||
@@ -83,0 +93,0 @@ // Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function) |
@@ -52,3 +52,3 @@ "use strict"; | ||
const keepClassNames = target >= /* ts.ScriptTarget.ES2016 */ 3; | ||
// swc only supports these 4x module options | ||
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND] | ||
const moduleType = module === ModuleKind.CommonJS | ||
@@ -55,0 +55,0 @@ ? 'commonjs' |
import type * as _ts from 'typescript'; | ||
/** | ||
* Common TypeScript interfaces between versions. | ||
* Common TypeScript interfaces between versions. We endeavour to write ts-node's own code against these types instead | ||
* of against `import "typescript"`, though we are not yet doing this consistently. | ||
* | ||
* Sometimes typescript@next adds an API we need to use. But we build ts-node against typescript@latest. | ||
* In these cases, we must declare that API explicitly here. Our declarations include the newer typescript@next APIs. | ||
* Importantly, these re-declarations are *not* TypeScript internals. They are public APIs that only exist in | ||
* pre-release versions of typescript. | ||
*/ | ||
@@ -27,3 +33,3 @@ export interface TSCommon { | ||
resolveModuleNameFromCache: typeof _ts.resolveModuleNameFromCache; | ||
resolveTypeReferenceDirective: typeof _ts.resolveTypeReferenceDirective; | ||
resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: _ts.CompilerOptions, host: _ts.ModuleResolutionHost, redirectedReference?: _ts.ResolvedProjectReference, cache?: _ts.TypeReferenceDirectiveResolutionCache, resolutionMode?: _ts.SourceFile['impliedNodeFormat']): _ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations; | ||
createIncrementalCompilerHost: typeof _ts.createIncrementalCompilerHost; | ||
@@ -37,1 +43,15 @@ createSourceFile: typeof _ts.createSourceFile; | ||
} | ||
export declare namespace TSCommon { | ||
interface LanguageServiceHost extends _ts.LanguageServiceHost { | ||
resolveTypeReferenceDirectives?(typeDirectiveNames: string[] | _ts.FileReference[], containingFile: string, redirectedReference: _ts.ResolvedProjectReference | undefined, options: _ts.CompilerOptions, containingFileMode?: _ts.SourceFile['impliedNodeFormat'] | undefined): (_ts.ResolvedTypeReferenceDirective | undefined)[]; | ||
} | ||
type ModuleResolutionHost = _ts.ModuleResolutionHost; | ||
type ParsedCommandLine = _ts.ParsedCommandLine; | ||
type ResolvedModule = _ts.ResolvedModule; | ||
type ResolvedTypeReferenceDirective = _ts.ResolvedTypeReferenceDirective; | ||
type CompilerOptions = _ts.CompilerOptions; | ||
type ResolvedProjectReference = _ts.ResolvedProjectReference; | ||
type ResolvedModuleWithFailedLookupLocations = _ts.ResolvedModuleWithFailedLookupLocations; | ||
type FileReference = _ts.FileReference; | ||
type SourceFile = _ts.SourceFile; | ||
} |
{ | ||
"name": "ts-node", | ||
"version": "10.5.0", | ||
"version": "10.6.0", | ||
"description": "TypeScript execution environment and REPL for node.js, with source map support", | ||
@@ -137,4 +137,4 @@ "main": "dist/index.js", | ||
"typedoc": "^0.22.10", | ||
"typescript": "4.5.2", | ||
"typescript-json-schema": "^0.51.0", | ||
"typescript": "4.5.5", | ||
"typescript-json-schema": "^0.53.0", | ||
"util.promisify": "^1.0.1" | ||
@@ -141,0 +141,0 @@ }, |
@@ -29,4 +29,3 @@ { | ||
"description": "JSON object to merge with TypeScript `compilerOptions`.", | ||
"properties": { | ||
}, | ||
"properties": {}, | ||
"type": "object" | ||
@@ -130,4 +129,3 @@ }, | ||
"additionalProperties": true, | ||
"properties": { | ||
}, | ||
"properties": {}, | ||
"type": "object" | ||
@@ -134,0 +132,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
590668
6638
27