Files

156 lines
4.4 KiB
TypeScript

import path from "node:path";
import fs from "node:fs";
import { createRequire } from "node:module";
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";
import { daikonCommonJS } from "./scripts/daikon-commonjs.ts";
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,
}),
});
}
},
};
}
function bundledPdfAssets(): Plugin {
return {
name: "bundled-pdf-assets",
apply: "build",
generateBundle() {
const root = path.resolve(import.meta.dirname, "node_modules/pdfjs-dist");
const version = JSON.parse(
fs.readFileSync(path.join(root, "package.json"), "utf8")
).version;
for (const folder of ["cmaps", "standard_fonts"]) {
for (const name of fs.readdirSync(path.join(root, folder))) {
this.emitFile({
type: "asset",
fileName: `pdfjs/${version}/${folder}/${name}`,
source: fs.readFileSync(path.join(root, folder, name)),
});
}
}
},
};
}
const plugins = [
daikonCommonJS(),
bundledPdfAssets(),
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: {
// VTK imports XML writers even for volumes. Use the upstream browser build;
// its Node entry requires EventEmitter and node:url at module evaluation.
xmlbuilder2: path.join(
path.dirname(
createRequire(import.meta.url).resolve("xmlbuilder2/package.json")
),
"lib/xmlbuilder2.min.js"
),
// vue: "@vue/compat",
"@/": `${path.resolve(import.meta.dirname, "src")}/`,
},
};
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
if (command === "serve") {
return {
plugins,
resolve,
worker: { plugins: () => [daikonCommonJS()] },
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,
worker: { plugins: () => [daikonCommonJS()] },
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 };
}
},
},
};
}
});