Last active
August 28, 2021 13:51
-
-
Save felladrin/032137f206ea7b3a2afd9b7bf7c248c5 to your computer and use it in GitHub Desktop.
Roadroller Vite Plugin (https://lifthrasiir.github.io/roadroller + https://vitejs.dev) for Js13kGames games (https://js13kgames.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from "vite"; | |
import { minifyHtml } from "vite-plugin-html"; | |
import { viteSingleFile } from "vite-plugin-singlefile"; | |
import { Packer, InputType, InputAction } from "roadroller"; | |
export default defineConfig({ | |
plugins: [ | |
{ | |
name: "roadroller", | |
async transformIndexHtml(html, context) { | |
if (!context || !context.bundle) return html; | |
let transformedScripts = []; | |
for (const asset of Object.values(context.bundle)) { | |
if (asset.type !== "chunk") continue; | |
html = html.replace(new RegExp(`<script type="module"[^>]*?src="/${asset.fileName}"[^>]*?></script>`), ""); | |
const packer = new Packer( | |
[ | |
{ | |
data: asset.code, | |
type: "js" as InputType.JS, | |
action: "eval" as InputAction.Eval, | |
}, | |
], | |
{} | |
); | |
await packer.optimize(); | |
const { firstLine, secondLine } = packer.makeDecoder(); | |
transformedScripts.push(`<script>${firstLine}${secondLine}</script>`); | |
} | |
return html.replace(/<\/body>/, `${transformedScripts.join("")}</body>`); | |
}, | |
}, | |
viteSingleFile(), | |
minifyHtml() | |
], | |
build: { | |
rollupOptions: { | |
output: { | |
manualChunks: {} // This sets Vite to output a single .js chunk, instead of the default 'index.js + vendor.js'. | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment