uni_modules.js 4.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
"use strict";
fxy060608's avatar
fxy060608 已提交
2
var __importDefault = (this && this.__importDefault) || function (mod) {
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8 9 10 11 12 13
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDefines = exports.parseExports = exports.genUniModulesExports = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const merge_1 = require("merge");
function genUniModulesExports() {
    const uniModulesDir = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'uni_modules');
    if (!fs_extra_1.default.existsSync(uniModulesDir)) {
        return '';
fxy060608's avatar
fxy060608 已提交
14
    }
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    const importCodes = [];
    const assignCodes = [];
    fs_extra_1.default.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
        var _a, _b;
        // 必须以 uni- 开头
        if (!uniModuleDir.startsWith('uni-')) {
            return;
        }
        const pkgPath = path_1.default.resolve(uniModulesDir, uniModuleDir, 'package.json');
        if (!fs_extra_1.default.existsSync(pkgPath)) {
            return;
        }
        const exports = (_b = (_a = JSON.parse(fs_extra_1.default.readFileSync(pkgPath, 'utf8'))) === null || _a === void 0 ? void 0 : _a.uni_modules) === null || _b === void 0 ? void 0 : _b.exports;
        if (exports) {
            const [exportsImportCodes, exportsAssignCodes] = parseExports(process.env.UNI_PLATFORM === 'h5' ? 'web' : process.env.UNI_PLATFORM, `@/uni_modules/${uniModuleDir}`, exports);
            importCodes.push(...exportsImportCodes);
            assignCodes.push(...exportsAssignCodes);
        }
    });
    if (!importCodes.length) {
        return '';
fxy060608's avatar
fxy060608 已提交
36
    }
fxy060608's avatar
fxy060608 已提交
37 38
    return `${importCodes.join('\n')}
${assignCodes.join('\n')}`;
fxy060608's avatar
fxy060608 已提交
39
}
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45 46 47 48 49 50 51
exports.genUniModulesExports = genUniModulesExports;
function parseExports(platform, source, exports = {}) {
    const rootDefines = {};
    Object.keys(exports).forEach((name) => {
        if (name.startsWith('uni')) {
            rootDefines[name] = exports[name];
        }
    });
    const platformDefines = exports[platform];
    // 该平台不支持
    if (platformDefines === false) {
        return [[], []];
fxy060608's avatar
fxy060608 已提交
52
    }
fxy060608's avatar
fxy060608 已提交
53
    return parseDefines(source, (0, merge_1.recursive)(true, rootDefines, platformDefines));
fxy060608's avatar
fxy060608 已提交
54
}
fxy060608's avatar
fxy060608 已提交
55 56 57 58 59 60 61 62 63 64
exports.parseExports = parseExports;
function parseDefines(source, defines = {}) {
    const importCodes = [];
    const assignCodes = [];
    Object.keys(defines).forEach((name) => {
        const [defineImportCodes, defineAssignCodes] = parseDefine(source, name, defines[name]);
        importCodes.push(...defineImportCodes);
        assignCodes.push(...defineAssignCodes);
    });
    return [importCodes, assignCodes];
fxy060608's avatar
fxy060608 已提交
65
}
fxy060608's avatar
fxy060608 已提交
66
exports.parseDefines = parseDefines;
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/**
 *  uni:'getBatteryInfo'
 * import getBatteryInfo from '..'
 *
 * uni:['getBatteryInfo']
 * import { getBatteryInfo } from '..'
 *
 * uni:['openLocation','chooseLocation']
 * import { openLocation, chooseLocation } from '..'
 *
 * uni:{
 *  onUserCaptureScreen: "onCaptureScreen"
 *  offUserCaptureScreen: "offCaptureScreen"
 * }
 *
 * uni.getBatteryInfo = getBatteryInfo
 * @param source
 * @param globalObject
 * @param define
 * @returns
 */
fxy060608's avatar
fxy060608 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
function parseDefine(source, globalObject, define) {
    const importCodes = [];
    const assignCodes = [];
    if (typeof define === 'string') {
        importCodes.push(`import ${define} from '${source}'`);
        assignCodes.push(`${globalObject}.${define} = ${define}`);
    }
    else if (Array.isArray(define)) {
        importCodes.push(`import { ${define.join(', ')} } from '${source}'`);
        define.forEach((d) => {
            assignCodes.push(`${globalObject}.${d} = ${d}`);
        });
    }
    else {
        const keys = Object.keys(define);
        const specifiers = [];
        keys.forEach((d) => {
            if (d !== define[d]) {
                specifiers.push(`${define[d]} as ${d}`);
            }
            else {
                specifiers.push(d);
            }
            assignCodes.push(`${globalObject}.${d} = ${d}`);
        });
        importCodes.push(`import { ${specifiers.join(', ')} } from '${source}'`);
    }
    return [importCodes, assignCodes];
fxy060608's avatar
fxy060608 已提交
116
}