wangzhibo
昨天 5e756b5a69bcd957f8eafc5f99a4f6413691d6dd
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
import type { Plugin } from "vite";
import { createEps } from "./eps";
import { parseJson } from "./utils";
import { updatePlugin } from "./plugin";
import { updateProxy } from "./proxy";
import { createFile } from "./file";
import { config } from "./config";
import { createTag } from "./tag";
 
export function base(): Plugin {
    return {
        name: "vite-cool-base",
        enforce: "pre",
        configureServer(server) {
            server.middlewares.use(async (req, res, next) => {
                function done(data: any) {
                    res.writeHead(200, { "Content-Type": "text/html;charset=UTF-8" });
                    res.end(JSON.stringify(data));
                }
 
                if (req.originalUrl?.includes("__cool")) {
                    const body = await parseJson(req);
 
                    switch (req.url) {
                        // 创建文件
                        case "/__cool_createFile":
                            await createFile(body);
                            break;
 
                        // 创建 eps 文件
                        case "/__cool_eps":
                            await createEps();
                            break;
 
                        // 更新插件
                        case "/__cool_updatePlugin":
                            await updatePlugin(body);
                            break;
 
                        // 设置代理
                        case "/__cool_updateProxy":
                            await updateProxy(body);
                            break;
 
                        default:
                            return done({
                                code: 1001,
                                message: "Unknown request",
                            });
                    }
 
                    done({
                        code: 1000,
                    });
                } else {
                    next();
                }
            });
        },
        transform(code, id) {
            if (config.nameTag) {
                return createTag(code, id);
            }
 
            return code;
        },
    };
}