11// @ts -ignore
22import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
33import 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'
69import { 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'
815import type { WorkerHost } from './env'
916
1017export interface CreateData {
11- locale : string
12- tsLocalized : any
1318 tsconfig : {
14- compilerOptions ?: ts . CompilerOptions
19+ compilerOptions ?: import ( 'typescript' ) . CompilerOptions
1520 vueCompilerOptions ?: Partial < VueCompilerOptions >
1621 }
1722 dependencies : { }
1823}
1924
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+ }
2141 worker . initialize (
2242 (
2343 ctx : monaco . worker . IWorkerContext < WorkerHost > ,
24- { tsconfig, dependencies, locale , tsLocalized } : CreateData
44+ { tsconfig, dependencies } : CreateData
2545 ) => {
2646 const { options : compilerOptions } = ts . convertCompilerOptionsFromJson (
2747 tsconfig ?. compilerOptions || { } ,
2848 ''
2949 )
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+ )
3257 const jsDelivrFs = createJsDelivrFs ( ctx . host . onFetchCdnFile )
33- const jsDelivrUriResolver = createJsDelivrUriResolver ( '/node_modules' , dependencies )
58+ const jsDelivrUriResolver = createJsDelivrUriResolver (
59+ '/node_modules' ,
60+ dependencies
61+ )
3462
3563 if ( locale ) {
3664 env . locale = locale
@@ -55,3 +83,24 @@ self.onmessage = () => {
5583 }
5684 )
5785}
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