Skip to content

Commit 3481236

Browse files
committed
Improve monaco local loading priority to avoid edge timing issues
1 parent 9d5de2a commit 3481236

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

frontend/src/App.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff 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 (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './utils/monacoSetup'
12
import React from 'react'
23
import {createRoot} from 'react-dom/client'
34
import './style.css'

frontend/src/utils/monacoSetup.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 })

0 commit comments

Comments
 (0)