mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 11:44:40 +00:00
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
// vite-plugin-inline-worker-dev.ts
|
|
import { Plugin } from "vite";
|
|
import fs from "fs/promises";
|
|
import { build, transform } from "esbuild";
|
|
|
|
export default function InlineWorkerDevPlugin(): Plugin {
|
|
return {
|
|
name: "vite:inline-worker-dev",
|
|
async load(id) {
|
|
if (id.includes("?inlineWorker")) {
|
|
const [cleanPath] = id.split("?");
|
|
console.log("cleanPath", cleanPath);
|
|
const code = await fs.readFile(cleanPath, "utf-8");
|
|
const result = await build({
|
|
entryPoints: [cleanPath],
|
|
bundle: true,
|
|
write: false,
|
|
platform: "browser",
|
|
format: "iife",
|
|
target: "esnext",
|
|
});
|
|
|
|
const workerCode = result.outputFiles[0].text;
|
|
|
|
const workerBlobCode = `
|
|
const code = ${JSON.stringify(workerCode)};
|
|
export default function InlineWorker() {
|
|
const blob = new Blob([code], { type: 'application/javascript' });
|
|
return new Worker(URL.createObjectURL(blob), { type: 'module' });
|
|
}
|
|
`;
|
|
return workerBlobCode;
|
|
}
|
|
return null;
|
|
},
|
|
};
|
|
}
|