uni.plugin.js 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const uni_shared_1 = require("@dcloudio/uni-shared");
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const validateFunction_1 = require("./validateFunction");
process.env.UNI_CLOUD_PROVIDER = JSON.stringify([]);
const uniCloudSpaces = [];
initUniCloudEnv();
fxy060608's avatar
fxy060608 已提交
9 10 11 12
/**
 * @type {import('vite').Plugin}
 */
const UniCloudPlugin = {
fxy060608's avatar
fxy060608 已提交
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
    name: 'vite:uni-cloud',
    config(config) {
        const silent = config.build && config.build.ssr ? true : false;
        if (silent) {
            return;
        }
        const len = uniCloudSpaces.length;
        if (!len) {
            return;
        }
        if (len === 1) {
            console.log(`本项目的uniCloud使用的默认服务空间spaceId为:${uniCloudSpaces[0].id}`);
        }
        if (process.env.UNI_PLATFORM === 'h5' &&
            !process.env.UNI_SUB_PLATFORM &&
            process.env.NODE_ENV === 'production') {
            console.warn('发布H5,需要在uniCloud后台操作,绑定安全域名,否则会因为跨域问题而无法访问。教程参考:https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5');
        }
        return {};
    },
    configureServer(server) {
        if (server.httpServer) {
            server.httpServer.on('listening', () => {
                process.nextTick(() => {
                    initUniCloudWarningOnce();
                });
            });
        }
        else {
            initUniCloudWarningOnce();
        }
    },
    closeBundle() {
        if (process.env.UNI_PLATFORM === 'h5' && !process.env.UNI_SSR_CLIENT) {
            console.log();
            console.log('欢迎将H5站部署到uniCloud前端网页托管平台,高速、免费、安全、省心,详见:');
            console.log('https://uniapp.dcloud.io/uniCloud/hosting');
        }
    },
};
fxy060608's avatar
fxy060608 已提交
53
const initUniCloudWarningOnce = (0, uni_shared_1.once)(() => {
fxy060608's avatar
fxy060608 已提交
54 55 56
    uniCloudSpaces.length &&
        console.warn('当前项目使用了uniCloud,为避免云函数调用跨域问题,建议在HBuilderX内置浏览器里调试,如使用外部浏览器需处理跨域,详见:https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5');
});
57
function initUniCloudEnv() {
fxy060608's avatar
fxy060608 已提交
58 59
    if (!process.env.UNI_CLOUD_SPACES) {
        return;
fxy060608's avatar
fxy060608 已提交
60
    }
fxy060608's avatar
fxy060608 已提交
61 62 63 64
    try {
        const spaces = JSON.parse(process.env.UNI_CLOUD_SPACES);
        if (!Array.isArray(spaces)) {
            return;
65
        }
fxy060608's avatar
fxy060608 已提交
66 67 68 69
        spaces.forEach((s) => uniCloudSpaces.push(s));
        process.env.UNI_CLOUD_PROVIDER = JSON.stringify(uniCloudSpaces.map((space) => {
            if (space.clientSecret) {
                return {
fxy060608's avatar
fxy060608 已提交
70
                    provider: space.provider || 'aliyun',
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78
                    spaceName: space.name,
                    spaceId: space.id,
                    clientSecret: space.clientSecret,
                    endpoint: space.apiEndpoint,
                };
            }
            else {
                return {
fxy060608's avatar
fxy060608 已提交
79
                    provider: space.provider || 'tencent',
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86
                    spaceName: space.name,
                    spaceId: space.id,
                };
            }
        }));
    }
    catch (e) { }
fxy060608's avatar
fxy060608 已提交
87
}
88
exports.default = [
fxy060608's avatar
fxy060608 已提交
89
    UniCloudPlugin,
fxy060608's avatar
fxy060608 已提交
90
    (0, uni_cli_shared_1.uniViteInjectPlugin)({
fxy060608's avatar
fxy060608 已提交
91 92 93
        exclude: [...uni_cli_shared_1.COMMON_EXCLUDE],
        uniCloud: ['@dcloudio/uni-cloud', 'default'],
    }),
fxy060608's avatar
fxy060608 已提交
94
    (0, validateFunction_1.uniValidateFunctionPlugin)(),
fxy060608's avatar
fxy060608 已提交
95
];