From 587e51d65f8a169aeee290ba00a1682874cf62e3 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 17 Aug 2024 19:51:15 +0800 Subject: [PATCH] Update runTsc.ts --- packages/typescript/lib/quickstart/runTsc.ts | 94 ++++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index b18c3272..886994cc 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -7,6 +7,47 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre setup?(language: Language): void, } = () => []; +export function runTsc( + tscPath: string, + options: string[] | { + extraSupportedExtensions: string[]; + extraExtensionsToRemove: string[]; + }, + _getLanguagePlugins: typeof getLanguagePlugins +) { + getLanguagePlugins = _getLanguagePlugins; + + 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; + } + + return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove); + } + return (readFileSync as any)(...args); + }; + + try { + require(tscPath); + } finally { + (fs as any).readFileSync = readFileSync; + delete require.cache[tscPath]; + } +} + /** * Replaces the code of typescript to add support for additional extensions and language plugins. * @@ -17,15 +58,14 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre * @param getLanguagePluginsFile - The file to get language plugins from. * @returns The modified typescript code. */ -export function replaceTscContent( +export function transformTscContent( tsc: string, proxyApiPath: string, extraSupportedExtensions: string[], extraExtensionsToRemove: string[], - getLanguagePluginsFile = __filename, + getLanguagePluginsFile = __filename ) { - - const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); + const neededPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); // Add allow extensions if (extraSupportedExtensions.length) { @@ -40,8 +80,8 @@ export function replaceTscContent( tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`); } // Support for basename.xxx to basename.xxx.d.ts - if (needPatchExtenstions.length) { - const extsText = needPatchExtenstions.map(ext => `"${ext}"`).join(', '); + if (neededPatchExtenstions.length) { + const extsText = neededPatchExtenstions.map(ext => `"${ext}"`).join(', '); tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) { return [${extsText}].some(ext => path.endsWith(ext)) ? path + newExtension @@ -64,48 +104,6 @@ export function replaceTscContent( return tsc; } -export function runTsc( - tscPath: string, - options: string[] | { - extraSupportedExtensions: string[]; - extraExtensionsToRemove: string[]; - }, - _getLanguagePlugins: typeof getLanguagePlugins -) { - - getLanguagePlugins = _getLanguagePlugins; - - 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; - } - - return replaceTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove); - } - return (readFileSync as any)(...args); - }; - - try { - require(tscPath); - } finally { - (fs as any).readFileSync = readFileSync; - delete require.cache[tscPath]; - } -} - function replace(text: string, ...[search, replace]: Parameters) { const before = text; text = text.replace(search, replace);