import path from "node:path"; import fs from "node:fs"; import { gzipSync } from "node:zlib"; import { defineConfig, type Plugin } from "vite"; import vue from "@vitejs/plugin-vue"; import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"; import legacy from "@vitejs/plugin-legacy"; import { compression } from "vite-plugin-compression2"; function bundledAceAssets(): Plugin { return { name: "bundled-ace-assets", apply: "build", generateBundle() { const root = path.resolve(import.meta.dirname, "node_modules/ace-builds"); const version = JSON.parse( fs.readFileSync(path.join(root, "package.json"), "utf8") ).version; const source = path.join(root, "src-min-noconflict"); const scripts = fs .readdirSync(source) .filter( (name) => /^(mode-|theme-|ext-searchbox)/.test(name) && name.endsWith(".js") ); scripts.push( ...fs .readdirSync(path.join(source, "snippets")) .filter((name) => name.endsWith(".js")) .map((name) => "snippets/" + name) ); for (const name of scripts) { this.emitFile({ type: "asset", fileName: `ace/${version}/${name}.gz`, source: gzipSync(fs.readFileSync(path.join(source, name)), { level: 9, }), }); } }, }; } const plugins = [ bundledAceAssets(), vue(), VueI18nPlugin({ include: [path.resolve(import.meta.dirname, "./src/i18n/**/*.json")], }), legacy({ // defaults already drop IE support targets: ["Chrome >= 70", "Firefox >= 78", "Edge >= 79", "Safari >= 12"], }), compression({ include: /\.js$/, deleteOriginalAssets: false }), ]; const resolve = { alias: { // vue: "@vue/compat", "@/": `${path.resolve(import.meta.dirname, "src")}/`, }, }; // https://vitejs.dev/config/ export default defineConfig(({ command }) => { if (command === "serve") { return { plugins, resolve, server: { proxy: { "/api/command": { target: "ws://127.0.0.1:8080", ws: true, }, "/api": "http://127.0.0.1:8080", }, }, }; } else { // command === 'build' return { plugins, resolve, base: "", build: { rollupOptions: { input: { index: path.resolve(import.meta.dirname, "./public/index.html"), }, output: { manualChunks: (id) => { // bundle dayjs files in a single chunk // this avoids having small files for each locale if (id.includes("dayjs/")) { return "dayjs"; // bundle i18n in a separate chunk } else if (id.includes("i18n/")) { return "i18n"; } }, }, }, }, experimental: { renderBuiltUrl(filename, { hostType }) { if (hostType === "js") { return { runtime: `window.__prependStaticUrl("${filename}")` }; } else if (hostType === "html") { return `[{[ .StaticURL ]}]/${filename}`; } else { return { relative: true }; } }, }, }; } });