-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
TypeScriptVueExtension.ts
186 lines (161 loc) · 5.64 KB
/
TypeScriptVueExtension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {
createTypeScriptEmbeddedExtension,
TypeScriptEmbeddedSource,
} from '../TypeScriptEmbeddedExtension';
import fs from 'fs-extra';
import { TypeScriptExtension } from '../TypeScriptExtension';
import { TypeScriptVueExtensionConfiguration } from './TypeScriptVueExtensionConfiguration';
import { VueTemplateCompilerV2 } from './types/vue-template-compiler';
import { VueTemplateCompilerV3 } from './types/vue__compiler-sfc';
interface GenericScriptSFCBlock {
content: string;
attrs: Record<string, string | true | undefined>;
src?: string;
}
function createTypeScriptVueExtension(
configuration: TypeScriptVueExtensionConfiguration
): TypeScriptExtension {
function loadVueTemplateCompiler(): VueTemplateCompilerV2 | VueTemplateCompilerV3 {
return require(configuration.compiler);
}
function isVueTemplateCompilerV2(
compiler: VueTemplateCompilerV2 | VueTemplateCompilerV3
): compiler is VueTemplateCompilerV2 {
return typeof (compiler as VueTemplateCompilerV2).parseComponent === 'function';
}
function isVueTemplateCompilerV3(
compiler: VueTemplateCompilerV2 | VueTemplateCompilerV3
): compiler is VueTemplateCompilerV3 {
return typeof (compiler as VueTemplateCompilerV3).parse === 'function';
}
function getExtensionByLang(
lang: string | true | undefined
): TypeScriptEmbeddedSource['extension'] {
if (lang === true) {
return '.js';
}
switch (lang) {
case 'ts':
return '.ts';
case 'tsx':
return '.tsx';
case 'js':
case 'jsx':
default:
return '.js';
}
}
function createVueNoScriptEmbeddedSource(): TypeScriptEmbeddedSource {
return {
sourceText: 'export default {};\n',
extension: '.js',
};
}
function createVueSrcScriptEmbeddedSource(
src: string,
lang: string | true | undefined
): TypeScriptEmbeddedSource {
// Import path cannot be end with '.ts[x]'
src = src.replace(/\.tsx?$/i, '');
// For now, ignore the error when the src file is not found since it will produce incorrect code location.
// It's not a large problem since it's handled on webpack side.
const text = [
'// @ts-ignore',
`export { default } from '${src}';`,
'// @ts-ignore',
`export * from '${src}';`,
].join('\n');
return {
sourceText: text,
extension: getExtensionByLang(lang),
};
}
function createVueInlineScriptEmbeddedSource(
text: string,
lang: string | true | undefined
): TypeScriptEmbeddedSource {
return {
sourceText: text,
extension: getExtensionByLang(lang),
};
}
function mergeVueScriptsContent(
scriptContent: string | undefined,
scriptSetupContent: string | undefined
): string {
const scriptLines = scriptContent?.split(/\r?\n/) ?? [];
const scriptSetupLines = scriptSetupContent?.split(/\r?\n/) ?? [];
const maxScriptLines = Math.max(scriptLines.length, scriptSetupLines.length);
const mergedScriptLines: string[] = [];
for (let line = 0; line < maxScriptLines; ++line) {
mergedScriptLines.push(scriptLines[line] || scriptSetupLines[line]);
}
return mergedScriptLines.join('\n');
}
function getVueEmbeddedSource(fileName: string): TypeScriptEmbeddedSource | undefined {
if (!fs.existsSync(fileName)) {
return undefined;
}
const compiler = loadVueTemplateCompiler();
const vueSourceText = fs.readFileSync(fileName, { encoding: 'utf-8' });
let script: GenericScriptSFCBlock | undefined;
if (isVueTemplateCompilerV2(compiler)) {
const parsed = compiler.parseComponent(vueSourceText, {
pad: 'line',
});
script = parsed.script;
} else if (isVueTemplateCompilerV3(compiler)) {
const parsed = compiler.parse(vueSourceText, {
pad: 'line',
});
if (parsed.descriptor) {
const parsedScript = parsed.descriptor.script;
const parsedScriptSetup = parsed.descriptor.scriptSetup;
let parsedContent = mergeVueScriptsContent(
parsedScript?.content,
parsedScriptSetup?.content
);
if (parsedScriptSetup) {
// a little bit naive, but should work in 99.9% cases without need for parsing script
const alreadyHasExportDefault = /export\s+default[\s|{]/gm.test(parsedContent);
if (!alreadyHasExportDefault) {
parsedContent += '\nexport default {};';
}
// add script setup lines at the end
parsedContent +=
"\n// @ts-ignore\nimport { defineProps, defineEmits, defineExpose, withDefaults } from '@vue/runtime-core';";
}
if (parsedScript || parsedScriptSetup) {
// map newer version of SFCScriptBlock to the generic one
script = {
content: parsedContent,
attrs: {
lang: parsedScript?.lang || parsedScriptSetup?.lang,
},
};
}
}
} else {
throw new Error(
'Unsupported vue template compiler. Compiler should provide `parse` or `parseComponent` function.'
);
}
if (!script) {
// No <script> block
return createVueNoScriptEmbeddedSource();
} else if (script.attrs.src) {
// <script src="file.ts" /> block
if (typeof script.attrs.src === 'string') {
return createVueSrcScriptEmbeddedSource(script.attrs.src, script.attrs.lang);
}
} else {
// <script lang="ts"></script> block
return createVueInlineScriptEmbeddedSource(script.content, script.attrs.lang);
}
}
return createTypeScriptEmbeddedExtension({
embeddedExtensions: ['.vue'],
getEmbeddedSource: getVueEmbeddedSource,
});
}
export { createTypeScriptVueExtension };