|
| 1 | +import { Monaco } from "@monaco-editor/react"; |
| 2 | +import { nanoid } from "nanoid"; |
| 3 | +import { build, initialize, Loader } from "esbuild-wasm"; |
| 4 | +import { fetchPlugin } from "./fetch.plugin"; |
| 5 | +import { unpkgPathPlugin } from "./unpkg-path.plugin"; |
| 6 | + |
| 7 | +declare const window: { |
| 8 | + monaco: Monaco; |
| 9 | +}; |
| 10 | + |
| 11 | +let serviceLoaded: boolean | null = null; |
| 12 | + |
| 13 | +const bundler = async (rawCode: string, lang: Loader) => { |
| 14 | + if (!serviceLoaded) { |
| 15 | + await initialize({ |
| 16 | + wasmURL: "https://unpkg.com/esbuild-wasm@0.14.34/esbuild.wasm", |
| 17 | + worker: true, |
| 18 | + }); |
| 19 | + console.log("esbuild-wasm initialized"); |
| 20 | + serviceLoaded = true; |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + const result = await build({ |
| 25 | + entryPoints: ["index.js"], |
| 26 | + bundle: true, |
| 27 | + write: false, |
| 28 | + metafile: true, |
| 29 | + legalComments: "none", |
| 30 | + plugins: [unpkgPathPlugin(), fetchPlugin(rawCode, lang)], |
| 31 | + define: { |
| 32 | + "process.env.NODE_ENV": `"production"`, |
| 33 | + global: "window", |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + const imports = result.metafile?.inputs["a:index.js"].imports |
| 38 | + .map((el) => el.path.replace("a:https://unpkg.com/", "")) |
| 39 | + .filter((e) => !e.includes("/")); |
| 40 | + |
| 41 | + loadTypes(imports); |
| 42 | + |
| 43 | + // console.log("esbuild result: ", result); |
| 44 | + |
| 45 | + return { code: result.outputFiles[0].text, err: null }; |
| 46 | + } catch (error: any) { |
| 47 | + console.error("esbuild error: ", error); |
| 48 | + return { |
| 49 | + code: null, |
| 50 | + err: { method: "error", data: [error.message], id: nanoid() }, |
| 51 | + }; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +export const normalizeCss = (data: string) => { |
| 56 | + /** |
| 57 | + * Function to remove any new lines, quotes from imported css packages. |
| 58 | + */ |
| 59 | + const escaped = data |
| 60 | + .replace(/\n/g, "") |
| 61 | + .replace(/"/g, '\\"') |
| 62 | + .replace(/'/g, "\\'"); |
| 63 | + return `const style = document.createElement('style') |
| 64 | + style.innerText = '${escaped}'; |
| 65 | + document.head.appendChild(style)`; |
| 66 | +}; |
| 67 | + |
| 68 | +export default bundler; |
| 69 | + |
| 70 | +let typesWorker; |
| 71 | + |
| 72 | +const loadTypes = (types) => { |
| 73 | + const disposables: any = []; |
| 74 | + const monaco = window && window.monaco; |
| 75 | + |
| 76 | + const dependencies = types.map((e) => ({ name: e, version: "latest" })) || []; |
| 77 | + |
| 78 | + if (!typesWorker) { |
| 79 | + typesWorker = new Worker( |
| 80 | + new URL("./workers/fetch-types.worker.js", import.meta.url) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + dependencies.forEach((dep) => { |
| 85 | + typesWorker.postMessage({ |
| 86 | + name: dep.name, |
| 87 | + version: dep.version, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + typesWorker.addEventListener("message", (event) => { |
| 92 | + // name, |
| 93 | + // version, |
| 94 | + // typings: result, |
| 95 | + const key = `node_modules/${event.data.name}/index.d.ts`; |
| 96 | + const source = event.data.typings[key]; |
| 97 | + |
| 98 | + // const path = `${MONACO_LIB_PREFIX}${event.data.name}`; |
| 99 | + const libUri = `file:///node_modules/@types/${event.data.name}/index.d.ts`; |
| 100 | + |
| 101 | + disposables.push( |
| 102 | + monaco.languages.typescript.javascriptDefaults.addExtraLib(source, libUri) |
| 103 | + ); |
| 104 | + disposables.push( |
| 105 | + monaco.languages.typescript.typescriptDefaults.addExtraLib(source, libUri) |
| 106 | + ); |
| 107 | + |
| 108 | + // When resolving definitions and references, the editor will try to use created models. |
| 109 | + // Creating a model for the library allows "peek definition/references" commands to work with the library. |
| 110 | + }); |
| 111 | + |
| 112 | + return { |
| 113 | + dispose() { |
| 114 | + disposables.forEach((d) => d.dispose()); |
| 115 | + if (typesWorker) { |
| 116 | + typesWorker.terminate(); |
| 117 | + } |
| 118 | + }, |
| 119 | + }; |
| 120 | +}; |
0 commit comments