wangrong
9 天以前 a611c225b17219e873b60888e03c330fbe0c57a4
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
import uni from "@dcloudio/vite-plugin-uni"; // !此依赖不能安装
import fs from "fs";
import path from "path";
import { defineConfig } from "vite";
import { cool } from "@cool-vue/vite-plugin";
import { proxy } from "./config/proxy";
 
// 微信 WXSS 单行过长会编译失败。图标字体若同时内联 woff2/woff/ttf,会超过限制。
function shortenMpWeixinFonts() {
    return {
        name: "mp-weixin-shorten-fonts",
        enforce: "post" as const,
        closeBundle() {
            const dirs = ["unpackage/dist/dev/mp-weixin", "unpackage/dist/build/mp-weixin"];
            for (const dir of dirs) {
                const file = path.resolve(__dirname, dir, "app.wxss");
                if (!fs.existsSync(file)) continue;
                const css = fs.readFileSync(file, "utf8");
                const next = css.replace(
                    /src:\s*url\("data:font\/woff2;base64,[^"]+"\)\s*format\("woff2"\)\s*,\s*url\("data:font\/woff;base64,[^"]+"\)\s*format\("woff"\)\s*,\s*url\("data:font\/ttf;base64,[^"]+"\)\s*format\("truetype"\)/g,
                    (block) => {
                        const woff2 = block.match(
                            /url\("data:font\/woff2;base64,[^"]+"\)\s*format\("woff2"\)/
                        );
                        return woff2 ? `src: ${woff2[0]}` : block;
                    }
                );
                if (next !== css) fs.writeFileSync(file, next);
            }
        },
    };
}
 
function resolve(dir: string) {
    return path.resolve(__dirname, dir);
}
 
// https://vitejs.dev/config
 
export default defineConfig(() => {
    return {
        plugins: [
            uni(),
            cool({
                type: "app",
                proxy,
            }),
            shortenMpWeixinFonts(),
        ],
        server: {
            port: 9900,
            proxy,
            hmr: {
                overlay: true,
            },
        },
        resolve: {
            alias: {
                "/@": resolve("./"),
                "/$": resolve("./uni_modules/"),
            },
        },
    };
});