|
1 | | -import type * as _monaco from 'monaco-editor'; |
2 | | -type Monaco = typeof _monaco; |
3 | | -import { version as monacoVersion } from 'monaco-editor/package.json'; |
4 | | - |
| 1 | +import * as monaco from 'monaco-editor'; |
5 | 2 | import schemaStylelintrc from '../schema/stylelintrc.json'; |
6 | 3 |
|
| 4 | +type Monaco = typeof monaco; |
| 5 | + |
7 | 6 | let monacoPromise: Promise<Monaco> | null = null; |
8 | 7 |
|
9 | 8 | /** Load the Monaco editor object. */ |
10 | 9 | export function loadMonaco(): Promise<Monaco> { |
11 | 10 | return ( |
12 | 11 | monacoPromise || |
13 | 12 | (monacoPromise = (async () => { |
14 | | - const rawMonaco: Monaco | { m: Monaco } = |
15 | | - (await loadMonacoFromEsmCdn()) || (await loadModuleFromMonaco('vs/editor/editor.main')); |
16 | | - |
17 | | - let monaco: Monaco; |
18 | | - |
19 | | - if ('m' in rawMonaco) { |
20 | | - |
21 | | - monaco = rawMonaco.m || rawMonaco; |
22 | | - } else { |
23 | | - monaco = rawMonaco; |
24 | | - } |
25 | | - |
26 | 13 | monaco.languages.css.cssDefaults.setOptions({ |
27 | 14 | validate: false, //Turn off CSS built-in validation. |
28 | 15 | }); |
@@ -52,77 +39,6 @@ export function loadMonaco(): Promise<Monaco> { |
52 | 39 | ); |
53 | 40 | } |
54 | 41 |
|
55 | | -async function loadMonacoFromEsmCdn(): Promise<Monaco | null> { |
56 | | - try { |
57 | | - const result = await import(`https://cdn.jsdelivr.net/npm/monaco-editor@${monacoVersion}/+esm`); |
58 | | - |
59 | | - const link = document.createElement('link'); |
60 | | - |
61 | | - link.rel = 'stylesheet'; |
62 | | - link.href = `https://cdn.jsdelivr.net/npm/monaco-editor@${monacoVersion}/min/vs/editor/editor.main.css`; |
63 | | - document.head.append(link); |
64 | | - |
65 | | - return result as Monaco; |
66 | | - } catch (e: unknown) { |
67 | | - console.warn('Failed to load Monaco editor from ESM CDN.', e); |
68 | | - |
69 | | - return null; |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -async function loadModuleFromMonaco<T>(moduleName: string): Promise<T> { |
74 | | - await setupMonaco(); |
75 | | - |
76 | | - return new Promise((resolve) => { |
77 | | - if (typeof window !== 'undefined') { |
78 | | - // @ts-expect-error -- global Monaco's require |
79 | | - window.require([moduleName], (r: T) => { |
80 | | - resolve(r); |
81 | | - }); |
82 | | - } |
83 | | - }); |
84 | | -} |
85 | | - |
86 | | -async function setupMonaco(): Promise<void> { |
87 | | - if (typeof window !== 'undefined') { |
88 | | - const monacoScript = |
89 | | - Array.from(document.head.querySelectorAll('script')).find( |
90 | | - (script) => script.src && script.src.includes('monaco') && script.src.includes('vs/loader'), |
91 | | - ) || |
92 | | - // If the script tag that loads the Monaco editor is not found, insert the script tag. |
93 | | - (await appendMonacoEditorScript()); |
94 | | - |
95 | | - // @ts-expect-error -- global Monaco's require |
96 | | - window.require.config({ paths: { vs: monacoScript.src.replace(/\/vs\/.*$/u, '/vs') } }); |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -/** Appends a script tag that loads the Monaco editor. */ |
101 | | -async function appendMonacoEditorScript(): Promise<HTMLScriptElement> { |
102 | | - const script = document.createElement('script'); |
103 | | - |
104 | | - return new Promise((resolve) => { |
105 | | - script.src = `https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.53.0/min/vs/loader.min.js`; |
106 | | - script.onload = () => { |
107 | | - script.onload = null; |
108 | | - |
109 | | - watch(); |
110 | | - |
111 | | - function watch() { |
112 | | - // @ts-expect-error -- global Monaco's require |
113 | | - if (window.require) { |
114 | | - resolve(script); |
115 | | - |
116 | | - return; |
117 | | - } |
118 | | - |
119 | | - setTimeout(watch, 200); |
120 | | - } |
121 | | - }; |
122 | | - document.head.append(script); |
123 | | - }); |
124 | | -} |
125 | | - |
126 | 42 | function setupEnhancedLanguages(monaco: Monaco) { |
127 | 43 | monaco.languages.register({ id: 'astro' }); |
128 | 44 | monaco.languages.registerTokensProviderFactory('astro', { |
|
0 commit comments