From e1cc687a7d8062fc02fb036241722edf1e21f415 Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Sun, 27 Nov 2022 15:29:10 +0800 Subject: [PATCH] feat: support TS v5 for svelte-tsc --- examples/svelte-tsc/bin/svelte-tsc.js | 63 ++++++++++--------------- examples/svelte-tsc/src/proxy.ts | 3 ++ vue-language-tools/vue-tsc/src/proxy.ts | 6 +-- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/examples/svelte-tsc/bin/svelte-tsc.js b/examples/svelte-tsc/bin/svelte-tsc.js index 2842df05b..87b58f962 100755 --- a/examples/svelte-tsc/bin/svelte-tsc.js +++ b/examples/svelte-tsc/bin/svelte-tsc.js @@ -6,43 +6,32 @@ const tscPath = require.resolve('typescript/lib/tsc'); const proxyPath = require.resolve('../out/proxy'); fs.readFileSync = (...args) => { - if (args[0] === tscPath) { - let tsc = readFileSync(...args); - - // add *.vue files to allow extensions - tsc = tsc.replace( - `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"]];`, - `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"], [".svelte"]];`, - ); - tsc = tsc.replace( - `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"]];`, - `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".svelte"]];`, - ); - tsc = tsc.replace( - `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"]];`, - `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"], [".svelte"]];`, - ); - - // proxy createProgram apis - tsc = tsc.replace( - `function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) {`, - `function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) { return require(${JSON.stringify(proxyPath)}).createProgramProxy(...arguments);`, - ); - - // proxy tracing - tsc = tsc.replace( - `ts.startTracing = tracingEnabled.startTracing;`, - `ts.startTracing = require(${JSON.stringify(proxyPath)}).loadTsLib().startTracing;`, - ); - - tsc = tsc.replace( - `ts.dumpTracingLegend = tracingEnabled.dumpLegend;`, - `ts.dumpTracingLegend = require(${JSON.stringify(proxyPath)}).loadTsLib().dumpTracingLegend;`, - ); - - return tsc; - } - return readFileSync(...args); + if (args[0] === tscPath) { + let tsc = readFileSync(...args); + + // add *.svelte files to allow extensions + tryReplace(/supportedTSExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])'); + tryReplace(/supportedJSExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])'); + tryReplace(/allSupportedExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])'); + + // proxy startTracing, dumpTracingLegend + tryReplace(/ = tracingEnabled\./g, ` = require(${JSON.stringify(proxyPath)}).loadTsLib().`); + + // proxy createProgram apis + tryReplace(/function createProgram\(.+\) {/, s => s + ` return require(${JSON.stringify(proxyPath)}).createProgramProxy(...arguments);`); + + return tsc; + + function tryReplace(search, replace) { + const before = tsc; + tsc = tsc.replace(search, replace); + const after = tsc; + if (after === before) { + throw 'Search string not found: ' + JSON.stringify(search.toString()); + } + } + } + return readFileSync(...args); }; require(tscPath); diff --git a/examples/svelte-tsc/src/proxy.ts b/examples/svelte-tsc/src/proxy.ts index 613e0c26e..78919b1a4 100644 --- a/examples/svelte-tsc/src/proxy.ts +++ b/examples/svelte-tsc/src/proxy.ts @@ -13,6 +13,9 @@ export function createProgramProxy( if (!options.options.noEmit && !options.options.emitDeclarationOnly) return doThrow('js emit is not supported'); + if (!options.options.noEmit && options.options.noEmitOnError) + return doThrow('noEmitOnError is not supported'); + if (!options.host) return doThrow('!options.host'); diff --git a/vue-language-tools/vue-tsc/src/proxy.ts b/vue-language-tools/vue-tsc/src/proxy.ts index 0f1d60f5a..aeda092e3 100644 --- a/vue-language-tools/vue-tsc/src/proxy.ts +++ b/vue-language-tools/vue-tsc/src/proxy.ts @@ -66,7 +66,7 @@ export function createProgramProxy( const vueTsLs = vueTs.createLanguageService(vueLsHost); program = vueTsLs.getProgram(); - program.__VLS_ctx = ctx; + program.__vue = ctx; function getVueCompilerOptions(): vue.VueCompilerOptions { const tsConfig = ctx.options.options.configFilePath; @@ -109,8 +109,8 @@ export function createProgramProxy( } } else { - program.__VLS_ctx.options = options; - program.__VLS_ctx.projectVersion++; + program.__vue.options = options; + program.__vue.projectVersion++; } for (const rootName of options.rootNames) {