/
index.ts
80 lines (69 loc) · 2.33 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { relative, resolve } from 'path'
import type { ExtensionContext } from 'vscode'
import { StatusBarAlignment, window, workspace } from 'vscode'
import { sourceObjectFields, sourcePluginFactory } from 'unconfig/presets'
import presetUno from '@unocss/preset-uno'
import { version } from '../package.json'
import { resolveOptions as resolveNuxtOptions } from '../../nuxt/src/options'
import { createContext } from './integration'
import { log } from './log'
import { registerAnnonations } from './annonation'
import { registerAutoComplete } from './autocomplete'
export async function activate(ext: ExtensionContext) {
const projectPath = workspace.workspaceFolders?.[0].uri.fsPath
if (!projectPath)
return
const config = workspace.getConfiguration('unocss')
const root = config.get<string>('root')
const cwd = root ? resolve(projectPath, root) : projectPath
log.appendLine(`UnoCSS for VS Code v${version} ${process.cwd()}`)
const context = createContext(
cwd,
{
presets: [
presetUno(),
],
},
[
sourcePluginFactory({
files: [
'vite.config',
'svelte.config',
'astro.config',
'iles.config',
],
targetModule: 'unocss/vite',
parameters: [{ command: 'serve', mode: 'development' }],
}),
sourceObjectFields({
files: 'nuxt.config',
fields: 'unocss',
}),
],
(result) => {
if (result.sources.some(s => s.includes('nuxt.config')))
resolveNuxtOptions(result.config)
},
)
context.updateRoot(cwd)
let sources: string[] = []
try {
sources = (await context.ready).sources
}
catch (e) {
log.appendLine(`[error] ${String(e)}`)
log.appendLine('[error] Failed to start extension, exiting')
return
}
if (!sources.length) {
log.appendLine('[warn] No config files found, disabled')
log.appendLine('[warn] Make sure you have `unocss.config.js` in your workspace root, or change `unocss.root` in your workspace settings')
return
}
log.appendLine(`Configuration loaded from\n${sources.map(s => ` - ${relative(cwd, s)}`).join('\n')}`)
const status = window.createStatusBarItem(StatusBarAlignment.Right, 200)
status.text = 'UnoCSS'
registerAutoComplete(context, ext)
registerAnnonations(cwd, context, status, ext)
}
export function deactivate() {}