-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
plugins.ts
73 lines (70 loc) · 2.49 KB
/
plugins.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
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as useHtmlFilePlugin from './plugins/file-html';
import * as useMdFilePlugin from './plugins/file-md';
import * as useVueFilePlugin from './plugins/file-vue';
import * as useVueSfcCustomBlocks from './plugins/vue-sfc-customblocks';
import * as useVueSfcScriptsFormat from './plugins/vue-sfc-scripts';
import * as useVueSfcStyles from './plugins/vue-sfc-styles';
import * as useVueSfcTemplate from './plugins/vue-sfc-template';
import * as useHtmlPlugin from './plugins/vue-template-html';
import useVueTsx from './plugins/vue-tsx';
import { VueLanguagePlugin } from './types';
import { VueCompilerOptions } from './types';
import { resolveVueCompilerOptions } from './utils/ts';
import * as CompilerDOM from '@vue/compiler-dom';
import * as CompilerVue2 from './utils/vue2TemplateCompiler';
export function getDefaultVueLanguagePlugins(
ts: typeof import('typescript/lib/tsserverlibrary'),
compilerOptions: ts.CompilerOptions,
_vueCompilerOptions: VueCompilerOptions,
) {
const _plugins: VueLanguagePlugin[] = [
useVueFilePlugin,
useMdFilePlugin,
useHtmlFilePlugin,
useHtmlPlugin,
useVueSfcStyles,
useVueSfcCustomBlocks,
useVueSfcScriptsFormat,
useVueSfcTemplate,
useVueTsx,
];
const pluginPaths = new Map<number, string>();
const vueCompilerOptions = resolveVueCompilerOptions(_vueCompilerOptions);
if (typeof require?.resolve === 'function') {
for (const pluginPath of vueCompilerOptions.plugins) {
try {
const importPath = require.resolve(pluginPath);
const plugin = require(importPath);
pluginPaths.set(_plugins.length, pluginPath);
_plugins.push(plugin);
}
catch (error) {
console.log('Load plugin failed', pluginPath, error);
}
}
}
else {
console.log('vueCompilerOptions.plugins is not available in Web.');
}
const pluginCtx: Parameters<VueLanguagePlugin>[0] = {
modules: {
'@vue/compiler-dom': vueCompilerOptions.target < 3 ? CompilerVue2 : CompilerDOM,
typescript: ts,
},
compilerOptions,
vueCompilerOptions,
};
const plugins = _plugins.map(plugin => plugin(pluginCtx)).sort((a, b) => {
const aOrder = a.order ?? 0;
const bOrder = b.order ?? 0;
return aOrder - bOrder;
});
return plugins.filter((plugin, i) => {
const valid = plugin.version >= 1 && plugin.version < 2;
if (!valid) {
console.warn(`Plugin ${JSON.stringify(pluginPaths.get(i) ?? plugin.name)} API version incompatible, expected 1.x but got ${JSON.stringify(plugin.version)}`);
}
return valid;
});
}