diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index dfb99703..370907a3 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as path from 'path'; import type * as ts from 'typescript'; import type { Language, LanguagePlugin } from '@volar/language-core'; @@ -18,25 +19,37 @@ export function runTsc( ) { getLanguagePlugins = _getLanguagePlugins; + let extraSupportedExtensions: string[]; + let extraExtensionsToRemove: string[]; + + if (Array.isArray(options)) { + extraSupportedExtensions = options; + extraExtensionsToRemove = []; + } + else { + extraSupportedExtensions = options.extraSupportedExtensions; + extraExtensionsToRemove = options.extraExtensionsToRemove; + } + const proxyApiPath = require.resolve('../node/proxyCreateProgram'); const readFileSync = fs.readFileSync; (fs as any).readFileSync = (...args: any[]) => { if (args[0] === tscPath) { let tsc = (readFileSync as any)(...args) as string; - - let extraSupportedExtensions: string[]; - let extraExtensionsToRemove: string[]; - if (Array.isArray(options)) { - extraSupportedExtensions = options; - extraExtensionsToRemove = []; - } - else { - extraSupportedExtensions = options.extraSupportedExtensions; - extraExtensionsToRemove = options.extraExtensionsToRemove; + try { + return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject); + } catch { + // Support the tsc shim used in Typescript v5.7 and up + const requireRegex = /module\.exports\s*=\s*require\((?:"|')(?\.\/\w+\.js)(?:"|')\)/; + const requirePath = requireRegex.exec(tsc)?.groups?.path; + if (requirePath) { + tsc = readFileSync(path.join(path.dirname(tscPath), requirePath), 'utf8'); + return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject); + } else { + throw new Error('Failed to locate tsc module path from shim'); + } } - - return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject); } return (readFileSync as any)(...args); }; @@ -112,7 +125,7 @@ function replace(text: string, ...[search, replace]: Parameters