From a89fbcdfb359cb0ccc884483af023657bafbb63a Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Wed, 13 Dec 2023 22:43:42 +0800 Subject: [PATCH] feat(tsc): support `vueCompilerOptions.extensions` option (#3800) --- packages/tsc/src/index.ts | 36 +++++++++++++++++++------ test-workspace/tsc/petite-vue/main.html | 3 ++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/tsc/src/index.ts b/packages/tsc/src/index.ts index 8a6c5c1a64..68790e4771 100644 --- a/packages/tsc/src/index.ts +++ b/packages/tsc/src/index.ts @@ -1,16 +1,36 @@ -import * as vue from '@vue/language-core'; import { runTsc } from '@volar/typescript/lib/starters/runTsc'; +import * as vue from '@vue/language-core'; -const windowsPathReg = /\\/g; +let runExtensions = ['.vue']; -runTsc(require.resolve('typescript/lib/tsc'), ['.vue'], (ts, options) => { +const windowsPathReg = /\\/g; +const extensionsChangedException = new Error('extensions changed'); +const main = () => runTsc(require.resolve('typescript/lib/tsc'), runExtensions, (ts, options) => { const { configFilePath } = options.options; const vueOptions = typeof configFilePath === 'string' ? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions : {}; - return vue.createLanguages( - ts, - options.options, - vueOptions, - ); + const extensions = vueOptions.extensions ?? ['.vue']; + if ( + runExtensions.length === extensions.length + && runExtensions.every(ext => extensions.includes(ext)) + ) { + return vue.createLanguages( + ts, + options.options, + vueOptions, + ); + } + else { + runExtensions = extensions; + throw extensionsChangedException; + } }); + +try { + main(); +} catch (err) { + if (err === extensionsChangedException) { + main(); + } +} diff --git a/test-workspace/tsc/petite-vue/main.html b/test-workspace/tsc/petite-vue/main.html index e379d02710..e52731ffb0 100644 --- a/test-workspace/tsc/petite-vue/main.html +++ b/test-workspace/tsc/petite-vue/main.html @@ -1,5 +1,6 @@