Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/playground-v2/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -51,6 +76,7 @@ const buildContext = await context({
outdir: outputDirectory,
metafile: true,
platform: "browser",
plugins: [monacoEditorCorePlugin],
sourcemap: true,
target: ["es2022"],
})
Expand All @@ -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 })
Expand Down
1 change: 0 additions & 1 deletion packages/playground-v2/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion packages/playground-v2/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading