plugin.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const path_1 = tslib_1.__importDefault(require("path"));
  5. const config_1 = require("../config");
  6. const utils = tslib_1.__importStar(require("../utils"));
  7. function PluginGenerator(config, baseConfig) {
  8. const getContent = (eggInfo) => {
  9. const dist = path_1.default.resolve(config.dtsDir, 'plugin.d.ts');
  10. if (!eggInfo.plugins) {
  11. return { dist };
  12. }
  13. const appPluginNameList = [];
  14. const importContent = [];
  15. const framework = config.framework || baseConfig.framework;
  16. Object.keys(eggInfo.plugins).forEach(name => {
  17. const pluginInfo = eggInfo.plugins[name];
  18. if (pluginInfo.package && pluginInfo.from) {
  19. appPluginNameList.push(name);
  20. if (pluginInfo.enable) {
  21. let pluginPath = pluginInfo.package;
  22. if (!pluginPath || config.usePath) {
  23. pluginPath = pluginInfo.path.replace(/\\/g, '/');
  24. }
  25. importContent.push(`import '${pluginPath}';`);
  26. }
  27. }
  28. });
  29. if (!appPluginNameList.length) {
  30. return { dist };
  31. }
  32. const composeInterface = (list) => {
  33. return ` ${list
  34. .map(name => `${utils.isIdentifierName(name) ? name : `'${name}'`}?: EggPluginItem;`)
  35. .join('\n ')}`;
  36. };
  37. return {
  38. dist,
  39. content: `${importContent.join('\n')}\n` +
  40. `import { EggPluginItem } from '${framework}';\n` +
  41. `declare module '${framework}' {\n` +
  42. ` interface ${config.interface} {\n` +
  43. `${composeInterface(Array.from(new Set(appPluginNameList)))}\n` +
  44. ' }\n' +
  45. '}',
  46. };
  47. };
  48. return utils.getEggInfo({
  49. cwd: baseConfig.cwd,
  50. customLoader: baseConfig.customLoader,
  51. cacheIndex: baseConfig.id,
  52. async: !!config.file,
  53. callback: getContent,
  54. });
  55. }
  56. exports.default = PluginGenerator;
  57. PluginGenerator.isPrivate = true;
  58. // only load plugin.ts|plugin.local.ts|plugin.default.ts
  59. PluginGenerator.defaultConfig = {
  60. pattern: 'plugin*(.local|.default).+(ts|js)',
  61. interface: config_1.declMapping.plugin,
  62. /** use path insteadof package while import plugins */
  63. usePath: false,
  64. };