/
index.ts
59 lines (48 loc) · 1.38 KB
/
index.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
import type { VueLanguagePlugin } from '@vue/language-core';
import * as pug from 'volar-service-pug/lib/languageService';
import { SourceMap } from '@volar/source-map';
const plugin: VueLanguagePlugin = ({ modules }) => {
return {
name: require('../package.json').name,
version: 2,
compileSFCTemplate(lang, template, options) {
if (lang === 'pug') {
const pugFile = pug?.baseParse(template);
const map = new SourceMap(pugFile.mappings);
if (pugFile) {
const compiler = modules['@vue/compiler-dom'];
const completed = compiler.compile(pugFile.htmlCode, {
...options,
comments: true,
onWarn(warning) {
options?.onWarn?.(createProxyObject(warning));
},
onError(error) {
options?.onError?.(createProxyObject(error));
},
});
return createProxyObject(completed);
function createProxyObject(target: any): any {
return new Proxy(target, {
get(target, prop) {
if (prop === 'offset') {
const htmlOffset = target.offset;
for (const mapped of map.getSourceOffsets(htmlOffset)) {
return mapped[0];
}
return -1;
}
const value = target[prop];
if (typeof value === 'object') {
return createProxyObject(target[prop]);
}
return value;
}
});
}
}
}
},
};
};
export = plugin;