UNPKG

rune-sdk

Version:

Build a multiplayer game played by millions! Your game runs inside the Rune app with 10 million installs across [iOS](https://apps.apple.com/app/rune-games-and-voice-chat/id1450358364) and [Android](https://play.google.com/store/apps/details?id=ai.rune.ti

59 lines (58 loc) 2.39 kB
import { Worker } from "okie"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); //Vite does not allow to disable minification for a specific file. //This plugin in turn disables minifation on all files, and then runs it on all files except logic.js //Implemented using https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/terser.ts as inspiration //And adapted according to https://rollupjs.org/plugin-development/#output-generation-hooks chart, so that this plugin runs after esbuild is done. export function terserPlugin(minifyLogic) { //Make sure the path is treated as absolute file import to fix windows issue: //https://github.com/nodejs/node/issues/31710 const terserPath = "file:" + require.resolve("terser"); const makeWorker = () => new Worker(async (params, options) => { const terser = await import(params.terserPath); return terser.minify(params.code, options); }, { max: undefined, }); let worker; let shouldMinify = true; return { name: "vite:rune-plugin:minify", apply: "build", //Disable minification for all files config: (config) => { //In case user manually disables minification, we'll skip running it for all files too. if (config.build?.minify === false) { shouldMinify = false; } return { ...config, build: { minify: false, }, }; }, async generateBundle(outputOptions, bundle) { if (!worker) { worker = makeWorker(); } await Promise.all(Object.keys(bundle).map(async (name) => { if (!shouldMinify || (name === "logic.js" && !minifyLogic)) { return; } const chunk = bundle[name]; if ("code" in chunk) { const res = await worker.run({ code: chunk.code, terserPath }, { module: outputOptions.format.startsWith("es"), toplevel: outputOptions.format === "cjs", }); chunk.code = res.code || ""; } })); }, closeBundle() { worker?.stop(); }, }; }