-
Notifications
You must be signed in to change notification settings - Fork 182
/
vue.worker.ts
109 lines (102 loc) · 3.15 KB
/
vue.worker.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
// @ts-expect-error
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
import type * as monaco from 'monaco-editor-core'
import {
type LanguageServiceEnvironment,
createTypeScriptWorkerLanguageService,
} from '@volar/monaco/worker'
import { createNpmFileSystem } from '@volar/jsdelivr'
import {
type VueCompilerOptions,
getFullLanguageServicePlugins,
createVueLanguagePlugin,
resolveVueCompilerOptions,
} from '@vue/language-service'
import type { WorkerHost, WorkerMessage } from './env'
import { URI } from 'vscode-uri'
export interface CreateData {
tsconfig: {
compilerOptions?: import('typescript').CompilerOptions
vueCompilerOptions?: Partial<VueCompilerOptions>
}
dependencies: Record<string, string>
}
let ts: typeof import('typescript')
let locale: string | undefined
self.onmessage = async (msg: MessageEvent<WorkerMessage>) => {
if (msg.data?.event === 'init') {
locale = msg.data.tsLocale
ts = await importTsFromCdn(msg.data.tsVersion)
self.postMessage('inited')
return
}
worker.initialize(
(
ctx: monaco.worker.IWorkerContext<WorkerHost>,
{ tsconfig, dependencies }: CreateData,
) => {
const asFileName = (uri: URI) => uri.path
const asUri = (fileName: string): URI => URI.file(fileName)
const env: LanguageServiceEnvironment = {
workspaceFolders: [URI.file('/')],
locale,
fs: createNpmFileSystem(
(uri) => {
if (uri.scheme === 'file') {
if (uri.path === '/node_modules') {
return ''
} else if (uri.path.startsWith('/node_modules/')) {
return uri.path.slice('/node_modules/'.length)
}
}
},
(pkgName) => dependencies[pkgName],
(path, content) => {
ctx.host.onFetchCdnFile(
asUri('/node_modules/' + path).toString(),
content,
)
},
),
}
const { options: compilerOptions } = ts.convertCompilerOptionsFromJson(
tsconfig?.compilerOptions || {},
'',
)
const vueCompilerOptions = resolveVueCompilerOptions(
tsconfig.vueCompilerOptions || {},
)
return createTypeScriptWorkerLanguageService({
typescript: ts,
compilerOptions,
workerContext: ctx,
env,
uriConverter: {
asFileName,
asUri,
},
languagePlugins: [
createVueLanguagePlugin(
ts,
compilerOptions,
vueCompilerOptions,
asFileName,
),
],
languageServicePlugins: getFullLanguageServicePlugins(ts),
setup({ project }) {
project.vue = { compilerOptions: vueCompilerOptions }
},
})
},
)
}
async function importTsFromCdn(tsVersion: string) {
const _module = globalThis.module
;(globalThis as any).module = { exports: {} }
const tsUrl = `https://cdn.jsdelivr.net/npm/typescript@${tsVersion}/lib/typescript.js`
await import(/* @vite-ignore */ tsUrl)
const ts = globalThis.module.exports
globalThis.module = _module
return ts as typeof import('typescript')
}