Skip to content

Commit

Permalink
优化构建
Browse files Browse the repository at this point in the history
  • Loading branch information
master1lan committed Feb 14, 2023
1 parent f58c8a7 commit ffcb6fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 23 additions & 0 deletions getbuildtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function getBulidTime() {
const buildTimeName = `build takes time`;
return {
name: "get-bulid-time",
enforce: "pre||post", // 插件执行的顺序,pre表示在alias之后,在vite的核心插件之前,post在核心插件之后
// pre 首批被执行的插件,会在@rollup/plugin-alias插件执行之后执行。
// normal(默认值) 第二批配执行的插件,会在vite的build阶段之前被执行,可以根据配置判断是否需要处理当前文件的代码。
// post 会在vite的build阶段之后被执行,进行代码构建方面的工作(minimize、代码分析...)。
apply: "build", //值可以是 build 或 serve 亦可以是一个函数,指明它们仅在 build 或 serve 模式时调用;如果不增加这个属性就会在run dev的时候也会输出buildStart中的信息
buildStart() {
console.time(buildTimeName);
},
buildEnd() {
// console.log('在构建阶段结束后被调用,此处构建结束只是代表所有模块转义完成;')
// console.timeEnd('time')
},
closeBundle() {
// 在服务器关闭时被调用
console.timeEnd(buildTimeName);
},
};
}
export default getBulidTime;
6 changes: 5 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { visualizer } from "rollup-plugin-visualizer";
import viteCompression from "vite-plugin-compression";
// polyfill
import legacy from "@vitejs/plugin-legacy";
//get build time
import getBulidTime from "./getbuildtime";

const ProdinjectScript = `<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DTCJR7156E"></script>
<script>
Expand Down Expand Up @@ -63,9 +66,10 @@ export default defineConfig(({ mode }) => {
},
},
}),
getBulidTime(),
],
build: {
// target: "es2015",
reportCompressedSize: false,
minify: "esbuild",
rollupOptions: {
output: {
Expand Down

0 comments on commit ffcb6fa

Please sign in to comment.