Skip to content

fix: reload language tools on script lang change #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'vue'
import * as defaultCompiler from 'vue/compiler-sfc'
import { compileFile } from './transform'
import { atou, utoa } from './utils'
import { atou, hasScriptLangChanged, utoa } from './utils'
import type {
SFCAsyncStyleCompileOptions,
SFCScriptCompileOptions,
Expand Down Expand Up @@ -68,6 +68,18 @@ export function useStore(
compileFile(store, activeFile.value).then((errs) => (errors.value = errs))
})

// Temporary workaround for https://github.com/vuejs/repl/issues/321
// which is related to https://github.com/microsoft/TypeScript/issues/57631
// TODO: remove this when the issue is fixed
watch(
() => activeFile.value.code,
(newCode, oldCode) => {
if (activeFile.value.language !== 'vue') return
if (hasScriptLangChanged(newCode, oldCode))
reloadLanguageTools.value?.()
},
)

watch(
() => [
files.value[tsconfigFile]?.code,
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ export function atou(base64: string): string {
// https://base64.guru/developers/javascript/examples/unicode-strings
return decodeURIComponent(escape(binary))
}

// compares the `lang` attribute of the `<script>` tag in .vue files
export function hasScriptLangChanged(newCode: string, oldCode: string) {
const langRegex = /<script[^>]*lang\s*=\s*["']([^"']+)["'][^>]*>/;

const newLang = newCode.match(langRegex)?.[1]
const oldLang = oldCode.match(langRegex)?.[1]

return newLang !== oldLang
}