microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/playground/build.js
126lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //@ts-check |
| 5 | |
| 6 | import { copyFileSync, mkdirSync, cpSync } from "node:fs"; |
| 7 | import { dirname, join, resolve } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | |
| 10 | import { build, context } from "esbuild"; |
| 11 | |
| 12 | import { copyKatex } from "../vscode/build.mjs"; |
| 13 | |
| 14 | const thisDir = dirname(fileURLToPath(import.meta.url)); |
| 15 | const libsDir = join(thisDir, "..", "..", "node_modules"); |
| 16 | |
| 17 | // Use minified libraries |
| 18 | const isRelease = process.argv.includes("--release"); |
| 19 | const outdir = join(thisDir, "public/libs"); |
| 20 | |
| 21 | function getTimeStr() { |
| 22 | const now = new Date(); |
| 23 | |
| 24 | const hh = now.getHours().toString().padStart(2, "0"); |
| 25 | const mm = now.getMinutes().toString().padStart(2, "0"); |
| 26 | const ss = now.getSeconds().toString().padStart(2, "0"); |
| 27 | const mil = now.getMilliseconds().toString().padStart(3, "0"); |
| 28 | |
| 29 | return `${hh}:${mm}:${ss}.${mil}`; |
| 30 | } |
| 31 | |
| 32 | /** @type {import("esbuild").BuildOptions} */ |
| 33 | const buildOptions = { |
| 34 | entryPoints: [ |
| 35 | join(thisDir, "src/main.tsx"), |
| 36 | join(thisDir, "src/compiler-worker.ts"), |
| 37 | join(thisDir, "src/language-service-worker.ts"), |
| 38 | join(thisDir, "src/kataViewer.tsx"), |
| 39 | ], |
| 40 | outdir, |
| 41 | bundle: true, |
| 42 | platform: "browser", |
| 43 | target: ["es2020", "chrome64", "edge79", "firefox62", "safari11.1"], |
| 44 | define: { "import.meta.url": "document.URL" }, |
| 45 | sourcemap: "linked", |
| 46 | minify: isRelease ? true : false, |
| 47 | }; |
| 48 | |
| 49 | // Copy the relevant external libraries from node_modules into the static site files |
| 50 | function copyLibs() { |
| 51 | let monacoBase = join( |
| 52 | libsDir, |
| 53 | `monaco-editor/${isRelease ? "min" : "dev"}/vs`, |
| 54 | ); |
| 55 | let monacoDest = join(thisDir, `public/libs/monaco/vs`); |
| 56 | |
| 57 | console.log("Copying the Monaco files over from: " + monacoBase); |
| 58 | mkdirSync(monacoDest, { recursive: true }); |
| 59 | cpSync(monacoBase, monacoDest, { recursive: true }); |
| 60 | |
| 61 | copyKatex(join(thisDir, "public/libs/katex")); |
| 62 | |
| 63 | copyWasmToPlayground(); |
| 64 | } |
| 65 | |
| 66 | export function copyWasmToPlayground() { |
| 67 | let qsharpWasm = join(thisDir, "..", "npm/qsharp/lib/web/qsc_wasm_bg.wasm"); |
| 68 | let qsharpDest = join(thisDir, `public/libs/qsharp`); |
| 69 | |
| 70 | console.log("Copying the wasm file to playground from: " + qsharpWasm); |
| 71 | mkdirSync(qsharpDest, { recursive: true }); |
| 72 | copyFileSync(qsharpWasm, join(qsharpDest, "qsc_wasm_bg.wasm")); |
| 73 | } |
| 74 | |
| 75 | function buildBundle() { |
| 76 | console.log("Running esbuild"); |
| 77 | |
| 78 | build(buildOptions).then(() => console.log(`Built bundles to ${outdir}`)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param {boolean} serve |
| 83 | */ |
| 84 | export async function buildPlayground(serve) { |
| 85 | // Serve the site or build it? |
| 86 | if (serve) { |
| 87 | // Plugin to log start/end of build events (mostly to help VS Code problem matcher) |
| 88 | /** @type {import("esbuild").Plugin} */ |
| 89 | const buildPlugin = { |
| 90 | name: "Build Events", |
| 91 | setup(build) { |
| 92 | build.onStart(() => |
| 93 | console.log("Playground build started @ " + getTimeStr()), |
| 94 | ); |
| 95 | build.onEnd(() => |
| 96 | console.log("Playground build complete @ " + getTimeStr()), |
| 97 | ); |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | let ctx = await context({ |
| 102 | ...buildOptions, |
| 103 | plugins: [buildPlugin], |
| 104 | color: false, |
| 105 | }); |
| 106 | const servedir = join(thisDir, "public"); |
| 107 | |
| 108 | // See https://esbuild.github.io/api/#serve |
| 109 | console.log( |
| 110 | "Starting the playground on http://localhost:5555 (copy this URL to a browser to use the playground)", |
| 111 | ); |
| 112 | await ctx.serve({ |
| 113 | port: 5555, |
| 114 | servedir, |
| 115 | }); |
| 116 | } else { |
| 117 | copyLibs(); |
| 118 | buildBundle(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | const thisFilePath = resolve(fileURLToPath(import.meta.url)); |
| 123 | if (thisFilePath === resolve(process.argv[1])) { |
| 124 | const serve = process.argv.includes("--serve"); |
| 125 | buildPlayground(serve); |
| 126 | } |
| 127 | |