jialh
2025-01-07 bb59b053247ef82969b64979260e2478bd732e1f
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniViteCopyPlugin = void 0;
const watcher_1 = require("../../watcher");
const messages_1 = require("../../messages");
const logs_1 = require("../../logs");
function uniViteCopyPlugin({ targets, verbose, }) {
    let resolvedConfig;
    let initialized = false;
    return {
        name: 'uni:copy',
        apply: 'build',
        configResolved(config) {
            resolvedConfig = config;
        },
        writeBundle() {
            if (initialized) {
                return;
            }
            if (resolvedConfig.build.ssr) {
                return;
            }
            initialized = true;
            return new Promise((resolve) => {
                Promise.all(targets.map(({ watchOptions, ...target }) => {
                    return new Promise((resolve) => {
                        new watcher_1.FileWatcher({
                            verbose,
                            ...target,
                        }).watch({
                            cwd: process.env.UNI_INPUT_DIR,
                            ...watchOptions,
                        }, (watcher) => {
                            if (process.env.NODE_ENV !== 'development') {
                                // 生产模式下,延迟 close,否则会影响 chokidar 初始化的 add 等事件
                                setTimeout(() => {
                                    watcher.close().then(() => resolve(void 0));
                                }, 2000);
                            }
                            else {
                                resolve(void 0);
                            }
                        }, () => {
                            // TODO 目前初始化编译时,也会不停地触发此函数。
                            (0, logs_1.output)('log', messages_1.M['dev.watching.end']);
                        });
                    });
                })).then(() => resolve());
            });
        },
    };
}
exports.uniViteCopyPlugin = uniViteCopyPlugin;