Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
166 lines (157 loc) · 5.96 KB
/
Copy pathvite.config.js
File metadata and controls
166 lines (157 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// noinspection JSCheckFunctionSignatures
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";
import {resolve} from "path";
import viteImagemin from "vite-plugin-imagemin";
import {viteSingleFile} from "vite-plugin-singlefile";
import {createHtmlPlugin} from "vite-plugin-html";
import fs, {readFileSync, writeFileSync} from "fs";
import {JSDOM} from "jsdom";
import vitePrerender from "./plugins/vite-prerender.js";
import prerenderConfig from "./prerender.config.js";
const timestamp = new Date().getTime();
const inline_icon = () => {
// noinspection JSUnusedGlobalSymbols
return {
name: "inline-svg",
apply: "build",
enforce: "post",
closeBundle() {
//注意Linux上会失效 需要单独一个网页头图片
console.log("内联svg");
const indexPath = resolve(__dirname, "dist", "index.html");
const html = readFileSync(indexPath, "utf-8");
const dom = new JSDOM(html);
const document = dom.window.document;
// Remove all meta tags with name="description"
const metaTags = document.querySelectorAll("meta[name=\"description\"]");
metaTags.forEach(tag => {
tag.parentNode.removeChild(tag);
});
const links = document.querySelectorAll("link[href$='.svg']");
links.forEach((link) => {
let href = link.getAttribute("href");
if (href.startsWith("/")) {
href = href.substring(1);
} else if (href.startsWith("./")) {
href = href.substring(2);
}
const svgPath = resolve(__dirname, "dist", href);
const svgContent = readFileSync(svgPath, "utf-8");
const dataUrl = `data:image/svg+xml,${encodeURIComponent(svgContent)}`;
// Set the data URL as the href attribute of the link element
link.setAttribute("href", dataUrl);
// Delete the SVG file
fs.unlinkSync(svgPath);
});
const updatedHtml = dom.serialize();
writeFileSync(indexPath, updatedHtml);
console.log("内联svg完成");
},
};
};
// 共用插件列表
const plugin_array = [viteImagemin({
svgo: {
plugins: [{
name: "removeViewBox",
}, {
name: "removeEmptyAttrs",
active: false,
}],
},
}), vue(), AutoImport({
resolvers: [ElementPlusResolver()],
}), Components({
extensions: ["vue"],
resolvers: [ElementPlusResolver(({
importStyle: "sass",
}))],
})];
// https://vitejs.dev/config/
// noinspection JSUnusedGlobalSymbols
export default ({mode}) => {
if (mode === "local_use") {
//打包成本地可以直接运行的html
return {
server: {
port: 3001,
},
base: "./",
plugins: [...plugin_array,
viteSingleFile(),
createHtmlPlugin({
minify: {
collapseWhitespace: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}), inline_icon(),
],
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
build: {
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
rollupOptions: {
output: {
// 入口文件名
entryFileNames: `[name].js`, // 块文件名
chunkFileNames: `chunk-[name].js`, // 资源文件名 css 图片等等
assetFileNames: `[name]-[hash].${timestamp}.[ext]`,
},
},
},
esbuild: {
pure: ["console.log", "console.debug"]
,
drop: ["debugger"],
},
}; } else { //正常为网站构建
return {
server: {
port: 3001,
},
plugins: [
...plugin_array, // 添加预渲染插件(仅在生产构建时启用)
process.env.ENABLE_PRERENDER === 'true' && vitePrerender({
pages: prerenderConfig.pages,
playwrightOptions: prerenderConfig.playwrightOptions,
renderOptions: prerenderConfig.renderOptions,
injectScript: prerenderConfig.injectScript
})
].filter(Boolean),
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
build: {
rollupOptions: {
output: {
// 入口文件名
entryFileNames: `assets/[name].${timestamp}.js`, // 块文件名
chunkFileNames: `assets/[name]-[hash].${timestamp}.js`, // 资源文件名 css 图片等等
assetFileNames: `assets/[name]-[hash].${timestamp}.[ext]`,
},
},
},
esbuild: {
pure: ["console.log", "console.debug"]
,
drop: ["debugger"],
},
};
}
}