File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,28 +60,6 @@ const App: React.FC = () => {
6060 EventsEmit ( 'app-ready' )
6161 } , [ ] ) ;
6262
63- // 为了让 monaco 从本地而不是 CDN 加载
64- const monacoInit = async ( ) => {
65- const loader = await import ( "@monaco-editor/loader" ) ;
66- const monaco = await import ( "monaco-editor" )
67- const editorWorker = await import ( "monaco-editor/esm/vs/editor/editor.worker?worker" )
68- const jsonWorker = await import ( "monaco-editor/esm/vs/language/json/json.worker?worker" )
69-
70- self . MonacoEnvironment = {
71- getWorker ( _ , label ) {
72- if ( label === "json" ) {
73- return new jsonWorker . default ( )
74- }
75- return new editorWorker . default ( )
76- }
77- }
78- loader . default . config ( {
79- monaco,
80- } ) ;
81- }
82-
83- monacoInit ( ) . then ( ( ) => {
84- } )
8563
8664
8765 return (
Original file line number Diff line number Diff line change 1+ import './utils/monacoSetup'
12import React from 'react'
23import { createRoot } from 'react-dom/client'
34import './style.css'
Original file line number Diff line number Diff line change 1+ // Ensure Monaco loads locally instead of from a CDN
2+ // This module should be imported before any component that uses @monaco -editor/react
3+
4+ import loader from '@monaco-editor/loader'
5+ import * as monaco from 'monaco-editor'
6+
7+ // Import workers so Vite bundles them locally
8+ // Note: The "?worker" query tells Vite to create worker bundles
9+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10+ // @ts -ignore
11+ import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
12+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13+ // @ts -ignore
14+ import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
15+
16+ // Provide MonacoEnvironment to use locally bundled workers
17+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18+ ( self as any ) . MonacoEnvironment = {
19+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20+ getWorker ( _moduleId : string , label : string ) {
21+ if ( label === 'json' ) {
22+ return new ( JsonWorker as unknown as { new ( ) : Worker } ) ( )
23+ }
24+ return new ( EditorWorker as unknown as { new ( ) : Worker } ) ( )
25+ } ,
26+ }
27+
28+ // Configure the loader to use the already imported local monaco instance
29+ loader . config ( { monaco } )
You can’t perform that action at this time.
0 commit comments