import * as rollup from "@rollup/browser"; import { init } from "./files"; import { nodeResolve } from "../node-resolve"; import commonjs from "../commonjs"; import swc from "../swc"; import replace from "../replace"; import json from "../json"; import { nfs } from "./fs-ref"; import sbom from "../sbom"; import sass from "../sass"; import { ExampleFS } from "./example-fs"; import { debounce } from "lodash"; import seismicPlugin from "../seismic"; import uiPage from "../ui-page"; import path from "path"; import url from "../url"; import exampleFsLoader from "./example-fs-loader"; const dec = new TextDecoder(); nfs.promises.readFile("/node_modules/lodash-es/package.json").then((d) => { console.log(dec.decode(d)); }); export type RunOptions = { rootDir?: string; }; const runDefaults = { rootDir: "/", }; let bundle: rollup.RollupBuild | undefined; let watcher: rollup.RollupWatcher; init(nfs).then(async () => { const content = await nfs.promises.readFile("/c.js", "utf8"); self.postMessage({ content }); }); const rollUpFS = new ExampleFS(nfs); let running = false; const run = async ({ rootDir = runDefaults.rootDir, }: RunOptions = runDefaults) => { running = true; const outputConfig: rollup.OutputOptions = { format: "esm", interop: "compat", }; globalThis.__rollupFs = rollUpFS; const plugins = [ uiPage({ rootDir, updateSrcPath(_: string, emittedFileName: string): string { const ext = path.extname(emittedFileName); const outName = emittedFileName.substring( 0, emittedFileName.length - ext.length, ); return `/uxasset/externals/${outName}.jsdbx`; }, entryPath(entry, { fullPath }) { const parsed = path.parse(entry); return { [`my-app/${parsed.name}`]: fullPath, }; }, }), seismicPlugin(), replace({ "process.env.NODE_ENV": "'production'", preventAssignment: true, }), url({ limit: 1024000 }), json(), sass(), nodeResolve({ rootDir, browser: true }), swc({ swc: { jsc: { parser: { jsx: true, syntax: "ecmascript", }, }, }, }), commonjs({ rootDir, dynamicRequireRoot: "/", dynamicRequireTargets: ["/dynamic/*.js"], }), sbom({ outFormats: ["json"], includeWellKnown: false }), exampleFsLoader({ fs: rollUpFS }), ]; if (!bundle) { bundle = await rollup.rollup({ input: "./*.html", output: outputConfig, plugins, }); } else if (!bundle.closed) { await bundle.close(); bundle = await rollup.rollup({ input: "./*.html", output: outputConfig, plugins, }); } // if (!watcher) { // watcher = rollup.watch({ // input: './index.js', // plugins, // output: outputConfig, // watch: watchOptions // }) // } // watcher.on('event', (e) => { // // @ts-ignore // if (e.result) { // // @ts-ignore // e.result.close(); // } // }); const { output } = await bundle.generate(outputConfig); running = false; return output; }; globalThis.addEventListener("message", async (e) => { const rebuild = async () => { try { if (running) return; const start = globalThis.performance.now(); const output = await run(); const end = globalThis.performance.now(); const time = `${end - start}ms`; console.log(time); self.postMessage({ time, output }); } catch (e) { console.error(e); running = false; bundle = undefined; } }; if (e.data?.build) { await rebuild(); } const updateFile = debounce( async () => { await nfs.promises.writeFile(e.data?.filePath, e.data?.fileContent); await rebuild(); }, 500, { trailing: false, leading: true }, ); if (e.data?.updateFile) { await updateFile(); } if (e.data?.readFile) { const content = await nfs.promises.readFile(e.data?.filePath, "utf8"); self.postMessage({ content }); } });