From fd3017a4f78e511ba8b855c2b34cc5354fe21c0a Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Wed, 9 Nov 2022 00:13:37 +0800 Subject: [PATCH] feat(vue-tsc): support for TS v5 #2095 --- vue-language-tools/vue-tsc/bin/vue-tsc.js | 52 +++++++++-------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/vue-language-tools/vue-tsc/bin/vue-tsc.js b/vue-language-tools/vue-tsc/bin/vue-tsc.js index 9e444bf32..edc52f1ef 100755 --- a/vue-language-tools/vue-tsc/bin/vue-tsc.js +++ b/vue-language-tools/vue-tsc/bin/vue-tsc.js @@ -9,41 +9,27 @@ 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"], [".vue", ".md", ".html"]];`, - `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"], [".vue"]];`, - ); - tsc = tsc.replace( - `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"]];`, - // `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".vue", ".md", ".html"]];`, - `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".vue"]];`, - ); - 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"], [".vue", ".md", ".html"]];`, - `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"], [".vue"]];`, - ); - - // 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;`, - ); + // add *.vue files to allow extensions + tryReplace(/supportedTSExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])'); + tryReplace(/supportedJSExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])'); + tryReplace(/allSupportedExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])'); + + // 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); };