diff --git a/packages/playground-v2/scripts/build.mjs b/packages/playground-v2/scripts/build.mjs index 33dae9adbcc5..9d689e585724 100644 --- a/packages/playground-v2/scripts/build.mjs +++ b/packages/playground-v2/scripts/build.mjs @@ -1,6 +1,7 @@ import { context } from "esbuild" +import { existsSync } from "node:fs" import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises" -import { dirname, resolve } from "node:path" +import { dirname, relative, resolve, sep } from "node:path" import { fileURLToPath } from "node:url" import { compatibilityHtml, compatibilityPaths, playgroundPath } from "./routes.mjs" @@ -16,6 +17,11 @@ const serve = process.argv.includes("--serve") const playgroundBase = serve ? "/" : process.env.PLAYGROUND_BASE ?? playgroundPath const editorWorker = fileURLToPath(import.meta.resolve("monaco-editor/editor/editor.worker")) +const monacoEditorVs = resolve(fileURLToPath(import.meta.resolve("monaco-editor/editor/editor.api.js")), "../..") +const monacoEditorCoreVs = resolve( + fileURLToPath(import.meta.resolve("monaco-editor-core/esm/vs/editor/editor.api.js")), + "../.." +) const coiServiceWorker = fileURLToPath(import.meta.resolve("coi-serviceworker/coi-serviceworker.min.js")) const indexHtml = (await readFile(resolve(packageDirectory, "src/index.html"), "utf8")).replaceAll( @@ -30,6 +36,25 @@ await Promise.all([ cp(coiServiceWorker, resolve(outputDirectory, "coi-serviceworker.min.js")), ]) +// monaco-editor ships its own copy of the editor core. Its relative imports into +// vs/ resolve to the matching monaco-editor-core files so the language +// definitions and editor worker share the patched core used by the app. +const monacoEditorCorePath = path => { + if (!path.startsWith(monacoEditorVs + sep)) return + const corePath = resolve(monacoEditorCoreVs, relative(monacoEditorVs, path)) + return existsSync(corePath) ? corePath : undefined +} +const monacoEditorCorePlugin = { + name: "monaco-editor-core", + setup(build) { + build.onResolve({ filter: /^\./ }, args => { + if (!args.importer.startsWith(monacoEditorVs + sep)) return + const corePath = monacoEditorCorePath(resolve(args.resolveDir, args.path)) + if (corePath) return { path: corePath } + }) + }, +} + const buildContext = await context({ absWorkingDir: packageDirectory, bundle: true, @@ -51,6 +76,7 @@ const buildContext = await context({ outdir: outputDirectory, metafile: true, platform: "browser", + plugins: [monacoEditorCorePlugin], sourcemap: true, target: ["es2022"], }) @@ -69,6 +95,12 @@ if (serve) { /node_modules\/(?:@typescript\/)?typescript(?:-wasip1-wasm)?\//.test(path) ) if (compilerInputs.length) throw new Error(`Compiler implementation was bundled: ${compilerInputs.join(", ")}`) + const duplicateMonacoInputs = Object.keys(result.metafile.inputs).filter(path => + monacoEditorCorePath(resolve(packageDirectory, path)) + ) + if (duplicateMonacoInputs.length) { + throw new Error(`monaco-editor core was bundled alongside monaco-editor-core: ${duplicateMonacoInputs.join(", ")}`) + } await buildContext.dispose() await rm(websitePlayDirectory, { force: true, recursive: true }) await cp(outputDirectory, websiteStaticDirectory, { recursive: true }) diff --git a/packages/playground-v2/src/global.d.ts b/packages/playground-v2/src/global.d.ts index e4fb1ae047a1..530394c3c503 100644 --- a/packages/playground-v2/src/global.d.ts +++ b/packages/playground-v2/src/global.d.ts @@ -3,7 +3,6 @@ declare module "*.json" { const value: any export default value } -declare module "monaco-editor/editor/contrib/links/browser/links.js" declare module "monaco-editor-core/esm/vs/base/browser/touch.js" { export const EventType: { Change: string; Tap: string } diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts index f7b31df12421..2069c85371a4 100644 --- a/packages/playground-v2/src/main.ts +++ b/packages/playground-v2/src/main.ts @@ -15,7 +15,6 @@ import { loadLegacyCompilerVersions, publishedNativeCompilerSelection, } from "./compiler-versions" -import "monaco-editor/editor/contrib/links/browser/links.js" import LZString from "lz-string" import "reflect-metadata" import {