custom.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const config_1 = require("../config");
  5. const utils = tslib_1.__importStar(require("../utils"));
  6. const path_1 = tslib_1.__importDefault(require("path"));
  7. const customWatcherName = 'custom';
  8. const customSpecRef = `${customWatcherName}_spec_ref`;
  9. const DeclareMapping = utils.pickFields(config_1.declMapping, ['ctx', 'app']);
  10. function CustomGenerator(config, baseConfig, tsHelper) {
  11. const createCustomLoader = (eggInfo) => {
  12. const eggConfig = eggInfo.config || {};
  13. const newCustomWatcherList = [];
  14. if (eggConfig.customLoader) {
  15. Object.keys(eggConfig.customLoader).forEach(key => {
  16. const loaderConfig = eggConfig.customLoader[key];
  17. if (!loaderConfig || !loaderConfig.directory) {
  18. return;
  19. }
  20. loaderConfig.inject = loaderConfig.inject || 'app';
  21. if (!DeclareMapping[loaderConfig.inject] || loaderConfig.tsd === false)
  22. return;
  23. // custom d.ts name
  24. const name = `${customWatcherName}-${key}`;
  25. newCustomWatcherList.push(name);
  26. // create a custom watcher
  27. tsHelper.registerWatcher(name, {
  28. ref: customSpecRef,
  29. distName: `${name}.d.ts`,
  30. directory: loaderConfig.directory,
  31. pattern: loaderConfig.match,
  32. ignore: loaderConfig.ignore,
  33. caseStyle: loaderConfig.caseStyle || 'lower',
  34. interface: loaderConfig.interface || config_1.declMapping[key],
  35. declareTo: `${DeclareMapping[loaderConfig.inject]}.${key}`,
  36. generator: 'auto',
  37. execAtInit: true,
  38. });
  39. });
  40. }
  41. // collect watcher which is need to remove.
  42. const removeList = tsHelper.watcherList.filter(w => (w.ref === customSpecRef && !newCustomWatcherList.includes(w.name)));
  43. // remove watcher and old d.ts
  44. tsHelper.destroyWatcher.apply(tsHelper, removeList.map(w => w.name));
  45. return removeList.map(w => ({
  46. dist: path_1.default.resolve(w.dtsDir, `${w.name}.d.ts`),
  47. }));
  48. };
  49. // reload egg info by file
  50. return utils.getEggInfo({
  51. cwd: baseConfig.cwd,
  52. customLoader: baseConfig.customLoader,
  53. cacheIndex: baseConfig.id,
  54. async: !!config.file,
  55. callback: createCustomLoader,
  56. });
  57. }
  58. exports.default = CustomGenerator;
  59. CustomGenerator.isPrivate = true;
  60. CustomGenerator.defaultConfig = {
  61. directory: 'config',
  62. execAtInit: true,
  63. pattern: [
  64. 'config*(.local|.default).+(ts|js)',
  65. 'plugin*(.local|.default).+(ts|js)',
  66. ],
  67. };