plugin.js 2.42 KB
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const config_1 = require("../config");
const utils = tslib_1.__importStar(require("../utils"));
function PluginGenerator(config, baseConfig) {
    const getContent = (eggInfo) => {
        const dist = path_1.default.resolve(config.dtsDir, 'plugin.d.ts');
        if (!eggInfo.plugins) {
            return { dist };
        }
        const appPluginNameList = [];
        const importContent = [];
        const framework = config.framework || baseConfig.framework;
        Object.keys(eggInfo.plugins).forEach(name => {
            const pluginInfo = eggInfo.plugins[name];
            if (pluginInfo.package && pluginInfo.from) {
                appPluginNameList.push(name);
                if (pluginInfo.enable) {
                    let pluginPath = pluginInfo.package;
                    if (!pluginPath || config.usePath) {
                        pluginPath = pluginInfo.path.replace(/\\/g, '/');
                    }
                    importContent.push(`import '${pluginPath}';`);
                }
            }
        });
        if (!appPluginNameList.length) {
            return { dist };
        }
        const composeInterface = (list) => {
            return `    ${list
                .map(name => `${utils.isIdentifierName(name) ? name : `'${name}'`}?: EggPluginItem;`)
                .join('\n    ')}`;
        };
        return {
            dist,
            content: `${importContent.join('\n')}\n` +
                `import { EggPluginItem } from '${framework}';\n` +
                `declare module '${framework}' {\n` +
                `  interface ${config.interface} {\n` +
                `${composeInterface(Array.from(new Set(appPluginNameList)))}\n` +
                '  }\n' +
                '}',
        };
    };
    return utils.getEggInfo({
        cwd: baseConfig.cwd,
        customLoader: baseConfig.customLoader,
        cacheIndex: baseConfig.id,
        async: !!config.file,
        callback: getContent,
    });
}
exports.default = PluginGenerator;
PluginGenerator.isPrivate = true;
// only load plugin.ts|plugin.local.ts|plugin.default.ts
PluginGenerator.defaultConfig = {
    pattern: 'plugin*(.local|.default).+(ts|js)',
    interface: config_1.declMapping.plugin,
    /** use path insteadof package while import plugins */
    usePath: false,
};