Skip to content

Commit 745436d

Browse files
Fix: fail to compile *.vue with some langs
Fixed these problems. - Compilation errors are raised when tsx or jsx in vue SFC. - Code in script tag is ignored when script tag has no lang attribute. (Should be treated as JavaScript)
1 parent dbfa736 commit 745436d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/VueProgram.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class VueProgram {
6666
const host = ts.createCompilerHost(programConfig.options);
6767
const realGetSourceFile = host.getSourceFile;
6868

69+
const getScriptKind = (lang: string) => {
70+
if (lang === "ts") {
71+
return ts.ScriptKind.TS;
72+
} else if (lang === "tsx") {
73+
return ts.ScriptKind.TSX;
74+
} else if (lang === "jsx") {
75+
return ts.ScriptKind.JSX;
76+
} else {
77+
// when lang is "js" or no lang specified
78+
return ts.ScriptKind.JS;
79+
}
80+
}
81+
6982
// We need a host that can parse Vue SFCs (single file components).
7083
host.getSourceFile = (filePath, languageVersion, onError) => {
7184
// first check if watcher is watching file - if not - check it's mtime
@@ -91,8 +104,21 @@ class VueProgram {
91104

92105
// get typescript contents from Vue file
93106
if (source && VueProgram.isVue(filePath)) {
94-
const parsed = vueParser.parse(source.text, 'script', { lang: ['ts', 'tsx', 'js', 'jsx'] });
95-
source = ts.createSourceFile(filePath, parsed, languageVersion, true);
107+
let parsed: string;
108+
let kind: ts.ScriptKind;
109+
for (const lang of ['ts', 'tsx', 'js', 'jsx']) {
110+
parsed = vueParser.parse(source.text, 'script', { lang: [lang], emptyExport: false });
111+
if (parsed) {
112+
kind = getScriptKind(lang);
113+
break;
114+
}
115+
}
116+
if (!parsed) {
117+
// when script tag has no lang, or no script tag given
118+
parsed = vueParser.parse(source.text, 'script');
119+
kind = ts.ScriptKind.JS;
120+
}
121+
source = ts.createSourceFile(filePath, parsed, languageVersion, true, kind);
96122
}
97123

98124
return source;

0 commit comments

Comments
 (0)