bjs/vite.config.ts

82 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import process from "node:process";
import { fileURLToPath, URL } from "node:url";
import topLevelAwait from "vite-plugin-top-level-await";
// https://vite.dev/config/
export default defineConfig({
plugins: [
{
name: "inject-at-top",
apply: "build",
generateBundle(_, bundle) {
Object.values(bundle).forEach((chunk) => {
if (chunk.fileName.endsWith(".js")) {
// 在文件头部插入代码
var chunkAny = chunk as any;
chunkAny.code = `if(globalThis != null){
if(globalThis.global != null){
if(typeof window === "undefined"){
window = globalThis.window
}
if(typeof document === "undefined"){
document = globalThis.windocument
}
if(typeof URL === "undefined"){
URL = window.URL
}
if(typeof SVGElement === "undefined"){
SVGElement = window.SVGElement
}
};
navigator = window.navigator;
const HTMLAnchorElement = globalThis.HTMLAnchorElement;
}
\n${chunkAny.code}`;
}
});
},
},
vue(),
topLevelAwait(),
{
name: "fix-recast",
transform(code, id) {
if (id.includes("recast-detour.js")) {
return code.replace(`this["Recast"]`, 'window["Recast"]');
}
},
},
],
base: process.env.NODE_ENV === "production" ? "/car2/" : "/",
// resolve: {
// alias: {
// "@": fileURLToPath(new URL("./src", import.meta.url)),
// },
// },
build: {
target: ["edge90", "chrome90", "firefox90", "safari15"],
// lib 和 rollupOptions 在编译网站,不编译库的时候注释掉
lib: {
entry: "src/main.ts", // 入口文件路径
name: "XPrint", // 全局变量名UMD 格式时生效)
fileName: (format) => `xprint.${format}.js`, // 输出文件名模板
// formats: ['es', 'umd', 'system'] // 输出格式ES 模块、UMD 等)
// formats: ['es'] // 输出格式ES 模块)
formats: ["es", "umd"], // 输出格式ES 模块、UMD 等)
},
rollupOptions: {
output: {
inlineDynamicImports: true, // 内联动态导入(禁用代码分割)
manualChunks: undefined, // 禁用手动分块
},
},
minify: "esbuild", // 使用 esbuild 压缩
// terserOptions: { // 可选:自定义压缩参数
// compress: { drop_console: true }
// }
},
optimizeDeps: { exclude: ["recast-navigation"] },
});