plugin.js
2.42 KB
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
"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,
};