From 3b57ebbf3c580b5fb392b5eaf584954358f4afdd Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Thu, 4 Jul 2024 10:13:41 +0800 Subject: [PATCH 1/3] fix --- packages/typescript/lib/quickstart/runTsc.ts | 42 ++++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index 81ecf02a..fe9dd3cc 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -9,9 +9,9 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre export function runTsc( tscPath: string, - extensionsOrOptions: string[] | { - supportedExtensions: string[]; - extensionsToRemove: string[]; + options: string[] | { + extraSupportedExtensions: string[]; + extraExtensionsToRemove: string[]; }, _getLanguagePlugins: typeof getLanguagePlugins ) { @@ -25,20 +25,38 @@ export function runTsc( if (args[0] === tscPath) { let tsc = (readFileSync as any)(...args) as string; - const supportedExtensions = Array.isArray(extensionsOrOptions) ? extensionsOrOptions : extensionsOrOptions.supportedExtensions; - const extensionsToRemove = Array.isArray(extensionsOrOptions) ? [] : extensionsOrOptions.extensionsToRemove; + let extraSupportedExtensions: string[]; + let extraExtensionsToRemove: string[]; + if (Array.isArray(options)) { + extraSupportedExtensions = options; + extraExtensionsToRemove = []; + } + else { + extraSupportedExtensions = options.extraSupportedExtensions; + extraExtensionsToRemove = options.extraExtensionsToRemove; + } + const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); // add allow extensions - if (supportedExtensions.length) { - const extsText = supportedExtensions.map(ext => `"${ext}"`).join(', '); - tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`); - tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`); - tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`); + if (extraSupportedExtensions.length) { + const extsText = extraSupportedExtensions.map(ext => `"${ext}"`).join(', '); + tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); + tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); + tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); + } - if (extensionsToRemove.length) { - const extsText = extensionsToRemove.map(ext => `"${ext}"`).join(', '); + if (extraExtensionsToRemove.length) { + const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', '); tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`); } + if (needPatchExtenstions.length) { + const extsText = needPatchExtenstions.map(ext => `"${ext}"`).join(', '); + tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) { + return [${extsText}].some(ext => path.endsWith(ext)) + ? path + newExtension + : _changeExtension(path, newExtension) + }\n` + s.replace('changeExtension', '_changeExtension')); + } // proxy createProgram tsc = replace(tsc, /function createProgram\(.+\) {/, s => From 2baabe45aa3d16aed8caf3559c312bc60d2a3354 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Thu, 4 Jul 2024 10:54:13 +0800 Subject: [PATCH 2/3] [skip ci] --- packages/typescript/lib/quickstart/runTsc.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index fe9dd3cc..93a9fece 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -43,7 +43,6 @@ export function runTsc( tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); - } if (extraExtensionsToRemove.length) { const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', '); From 630f31118d3986c00cc730eb83cd896709fd547e Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Thu, 4 Jul 2024 10:58:01 +0800 Subject: [PATCH 3/3] [skip ci] --- packages/typescript/lib/quickstart/runTsc.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index 93a9fece..17a2f83d 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -37,17 +37,19 @@ export function runTsc( } const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); - // add allow extensions + // Add allow extensions if (extraSupportedExtensions.length) { const extsText = extraSupportedExtensions.map(ext => `"${ext}"`).join(', '); tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); } + // Use to emit basename.xxx to basename.d.ts instead of basename.xxx.d.ts if (extraExtensionsToRemove.length) { const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', '); 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(', '); tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) {