generate-style-deps.cjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const config = require('../src/config.json');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. // 获取依赖关系
  5. const styleMap = new Map();
  6. config.nav.forEach((item) => {
  7. item.packages.forEach((element) => {
  8. styleMap.set(element.name, {
  9. name: element.name,
  10. children: element.styleDeps
  11. });
  12. });
  13. });
  14. styleMap.forEach((value) => {
  15. if (value.chilren?.length !== 0) {
  16. value.children.forEach((item, index) => {
  17. value.children[index] = styleMap.get(item);
  18. });
  19. }
  20. });
  21. const getAllDeps = (styleObj, key) => {
  22. const value = styleObj;
  23. if (value.children?.length === 0) {
  24. return [value.name];
  25. } else {
  26. let deps = [];
  27. value.children?.forEach((item) => {
  28. if (key === item.name) {
  29. console.error('generate-style-deps: 存在循环引用', key);
  30. return [];
  31. }
  32. deps = deps.concat(getAllDeps(item, key));
  33. });
  34. deps.unshift(value.name);
  35. return [...new Set(deps)];
  36. }
  37. };
  38. const tasks = [];
  39. styleMap.forEach((value, key) => {
  40. const name = key.toLowerCase();
  41. let deps = getAllDeps(value, key);
  42. deps = deps.filter((component) => {
  43. return component !== key;
  44. });
  45. const output = `require('./index.scss');\n${deps
  46. .map((component) => {
  47. return `require('../${component.toLowerCase()}/index.scss');\n`;
  48. })
  49. .join('')}`;
  50. tasks.push(
  51. fs.outputFile(path.resolve(__dirname, `../dist/packages/${name}/index.js`), output, 'utf8', () => {
  52. // console.log('')
  53. })
  54. );
  55. });
  56. Promise.all(tasks);