1
1
// @ts -ignore
2
2
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
3
3
import type * as monaco from 'monaco-editor-core'
4
- import * as ts from 'typescript'
5
- import { createJsDelivrFs , createJsDelivrUriResolver , decorateServiceEnvironment } from '@volar/cdn'
4
+ import {
5
+ createJsDelivrFs ,
6
+ createJsDelivrUriResolver ,
7
+ decorateServiceEnvironment ,
8
+ } from '@volar/cdn'
6
9
import { VueCompilerOptions , resolveConfig } from '@vue/language-service'
7
- import { createLanguageService , createLanguageHost , createServiceEnvironment } from '@volar/monaco/worker'
10
+ import {
11
+ createLanguageService ,
12
+ createLanguageHost ,
13
+ createServiceEnvironment ,
14
+ } from '@volar/monaco/worker'
8
15
import type { WorkerHost } from './env'
9
16
10
17
export interface CreateData {
11
- locale : string
12
- tsLocalized : any
13
18
tsconfig : {
14
- compilerOptions ?: ts . CompilerOptions
19
+ compilerOptions ?: import ( 'typescript' ) . CompilerOptions
15
20
vueCompilerOptions ?: Partial < VueCompilerOptions >
16
21
}
17
22
dependencies : { }
18
23
}
19
24
20
- self . onmessage = ( ) => {
25
+ const locale = navigator . language . toLowerCase ( )
26
+
27
+ let ts : typeof import ( 'typescript' )
28
+ let tsLocalized : any
29
+
30
+ self . onmessage = async ( msg ) => {
31
+ if ( msg . data ?. event === 'init' ) {
32
+ ; [ ts , tsLocalized ] = await Promise . all ( [
33
+ importTsFromCdn ( msg . data . tsVersion ) ,
34
+ fetchJson (
35
+ `https://cdn.jsdelivr.net/npm/typescript@${ msg . data . tsVersion } /lib/${ locale } /diagnosticMessages.generated.json`
36
+ ) ,
37
+ ] )
38
+ self . postMessage ( 'inited' )
39
+ return
40
+ }
21
41
worker . initialize (
22
42
(
23
43
ctx : monaco . worker . IWorkerContext < WorkerHost > ,
24
- { tsconfig, dependencies, locale , tsLocalized } : CreateData
44
+ { tsconfig, dependencies } : CreateData
25
45
) => {
26
46
const { options : compilerOptions } = ts . convertCompilerOptionsFromJson (
27
47
tsconfig ?. compilerOptions || { } ,
28
48
''
29
49
)
30
- const env = createServiceEnvironment ( ) ;
31
- const host = createLanguageHost ( ctx . getMirrorModels , env , '/src' , compilerOptions )
50
+ const env = createServiceEnvironment ( )
51
+ const host = createLanguageHost (
52
+ ctx . getMirrorModels ,
53
+ env ,
54
+ '/src' ,
55
+ compilerOptions
56
+ )
32
57
const jsDelivrFs = createJsDelivrFs ( ctx . host . onFetchCdnFile )
33
- const jsDelivrUriResolver = createJsDelivrUriResolver ( '/node_modules' , dependencies )
58
+ const jsDelivrUriResolver = createJsDelivrUriResolver (
59
+ '/node_modules' ,
60
+ dependencies
61
+ )
34
62
35
63
if ( locale ) {
36
64
env . locale = locale
@@ -55,3 +83,24 @@ self.onmessage = () => {
55
83
}
56
84
)
57
85
}
86
+
87
+ async function importTsFromCdn ( tsVersion : string ) {
88
+ const _module = globalThis . module
89
+ ; ( globalThis as any ) . module = { exports : { } }
90
+ const tsUrl = `https://cdn.jsdelivr.net/npm/typescript@${ tsVersion } /lib/typescript.js`
91
+ await import ( /* @vite -ignore */ tsUrl )
92
+ const ts = module . exports
93
+ globalThis . module = _module
94
+ return ts as typeof import ( 'typescript' )
95
+ }
96
+
97
+ async function fetchJson < T > ( url : string ) {
98
+ try {
99
+ const res = await fetch ( url )
100
+ if ( res . status === 200 ) {
101
+ return await res . json ( )
102
+ }
103
+ } catch {
104
+ // ignore
105
+ }
106
+ }
0 commit comments