Skip to content

Commit c7ac119

Browse files
committed
fix(@vue/language-service): reload on script lang change
1 parent 3a7a5f8 commit c7ac119

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/store.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { version, reactive, watchEffect, watch } from 'vue'
22
import * as defaultCompiler from 'vue/compiler-sfc'
33
import { compileFile } from './transform'
4-
import { utoa, atou } from './utils'
4+
import { utoa, atou, hasScriptLangChanged } from './utils'
55
import {
66
SFCScriptCompileOptions,
77
SFCAsyncStyleCompileOptions,
@@ -193,6 +193,18 @@ export class ReplStore implements Store {
193193
)
194194
)
195195

196+
// Temporary workaround for https://github.com/vuejs/repl/issues/321
197+
// which is related to https://github.com/microsoft/TypeScript/issues/57631
198+
// TODO: remove this when the issue is fixed
199+
watch(
200+
() => this.state.activeFile.code,
201+
(newCode, oldCode) => {
202+
if (this.state.activeFile.language !== 'vue') return
203+
if (hasScriptLangChanged(newCode, oldCode))
204+
this.reloadLanguageTools?.()
205+
},
206+
)
207+
196208
watch(
197209
() => [
198210
this.state.files[tsconfigFile]?.code,

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ export function atou(base64: string): string {
3131
// https://base64.guru/developers/javascript/examples/unicode-strings
3232
return decodeURIComponent(escape(binary))
3333
}
34+
35+
// compares the `lang` attribute of the `<script>` tag in .vue files
36+
export function hasScriptLangChanged(newCode: string, oldCode: string) {
37+
const langRegex = /<script[^>]*lang\s*=\s*["']([^"']+)["'][^>]*>/;
38+
39+
const newLang = newCode.match(langRegex)?.[1]
40+
const oldLang = oldCode.match(langRegex)?.[1]
41+
42+
return newLang !== oldLang
43+
}

0 commit comments

Comments
 (0)