Skip to content

Commit 84ebcef

Browse files
committed
fix(playground): use bundled min.js + jsDelivr CDN + blob workers
1 parent 1abc946 commit 84ebcef

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

docs/playground.html

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,36 @@ <h1>Playground</h1>
231231
<link
232232
rel="stylesheet"
233233
id="monaco-css"
234-
href="https://unpkg.com/@node-projects/monaco-editor-esm@latest/min/vs/editor/editor.main.css"
234+
href="https://cdn.jsdelivr.net/npm/@node-projects/monaco-editor-esm@latest/min/vs/editor/editor.main.css"
235235
/>
236236

237237
<script type="module">
238238
// ─── CDN base ────────────────────────────────────────────────────────────────
239-
const CDN = 'https://unpkg.com/@node-projects/monaco-editor-esm@latest';
240-
const MONACO_MAIN = `${CDN}/esm/vs/editor/editor.main.js`;
239+
const CDN = 'https://cdn.jsdelivr.net/npm/@node-projects/monaco-editor-esm@latest';
240+
const MONACO_MAIN = `${CDN}/esm/vs/editor/editor.main.min.js`;
241+
242+
// ─── Workers: use a same-origin blob that imports the cross-origin worker ────
243+
// Chrome/Firefox both allow module workers from blob: URLs that import() other
244+
// origins, as long as the remote script is served with CORS headers (jsDelivr ✓).
245+
function makeWorker(label) {
246+
const workerMap = {
247+
json: 'json.worker.js',
248+
css: 'css.worker.js',
249+
scss: 'css.worker.js',
250+
less: 'css.worker.js',
251+
html: 'html.worker.js',
252+
handlebars: 'html.worker.js',
253+
razor: 'html.worker.js',
254+
typescript: 'ts.worker.js',
255+
javascript: 'ts.worker.js',
256+
};
257+
const file = workerMap[label] ?? 'editor.worker.js';
258+
const url = `${CDN}/esm/vs/editor/${file}`;
259+
const blob = new Blob([`import "${url}";`], { type: 'text/javascript' });
260+
return new Worker(URL.createObjectURL(blob), { type: 'module' });
261+
}
262+
263+
self.MonacoEnvironment = { getWorker: (_id, label) => makeWorker(label) };
241264

242265
// ─── Default setup code ──────────────────────────────────────────────────────
243266
const DEFAULT_CODE = `\
@@ -263,7 +286,14 @@ <h1>Playground</h1>
263286
`;
264287

265288
// ─── Load Monaco once ────────────────────────────────────────────────────────
266-
const monaco = await import(MONACO_MAIN);
289+
let monaco;
290+
try {
291+
monaco = await import(MONACO_MAIN);
292+
} catch (err) {
293+
document.getElementById('error-bar').style.display = 'block';
294+
document.getElementById('error-bar').textContent = 'Failed to load Monaco: ' + err;
295+
throw err;
296+
}
267297

268298
// ─── Setup editor (left pane) ─────────────────────────────────────────────
269299
const setupEditor = monaco.editor.create(

0 commit comments

Comments
 (0)