generate-style-deps.cjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const tasks = [];
  7. let outputFileEntry = ``;
  8. // import Locale from './packages/locale';\n
  9. config.nav.forEach((item) => {
  10. item.packages.forEach((element) => {
  11. styleMap.set(element.name, {
  12. name: element.name,
  13. children: element.styleDeps
  14. });
  15. // gen entry
  16. const outputMjs = `import ${element.name} from '../_es/${element.name}.js';\nexport { ${element.name} };`;
  17. tasks.push(
  18. fs.outputFile(path.resolve(__dirname, `../dist/packages/${element.name}/index.mjs`), outputMjs, 'utf8', () => {
  19. // console.log('')
  20. })
  21. );
  22. outputFileEntry += `export * from "./packages/${element.name}/index.mjs";\n`;
  23. });
  24. });
  25. tasks.push(
  26. fs.outputFile(path.resolve(__dirname, `../dist/nutui.es.js`), outputFileEntry, 'utf8', () => {
  27. // console.log('')
  28. })
  29. );
  30. styleMap.forEach((value) => {
  31. if (value.children && value.children.length > 0) {
  32. value.children.forEach((item, index) => {
  33. value.children[index] = styleMap.get(item);
  34. });
  35. }
  36. });
  37. const getAllDeps = (styleObj, key) => {
  38. const value = styleObj;
  39. if (value.children?.length === 0) {
  40. return [value.name];
  41. } else {
  42. let deps = [];
  43. value.children?.forEach((item) => {
  44. if (key === item.name) {
  45. console.error('generate-style-deps: 存在循环引用', key);
  46. return [];
  47. }
  48. deps = deps.concat(getAllDeps(item, key));
  49. });
  50. deps.unshift(value.name);
  51. return [...new Set(deps)];
  52. }
  53. };
  54. styleMap.forEach((value, key) => {
  55. const name = key.toLowerCase();
  56. let deps = getAllDeps(value, key);
  57. deps = deps.filter((component) => {
  58. return component !== key;
  59. });
  60. // gen style
  61. const outputStyleCJs = `require('./index.scss');\n${deps
  62. .map((component) => {
  63. return `require('../${component.toLowerCase()}/index.scss');\n`;
  64. })
  65. .join('')}`;
  66. const outputStyleMjs = `import './index.scss';\n${deps
  67. .map((component) => {
  68. return `import '../${component.toLowerCase()}/index.scss';\n`;
  69. })
  70. .join('')}`;
  71. tasks.push(
  72. fs.outputFile(path.resolve(__dirname, `../dist/packages/${name}/style.cjs`), outputStyleCJs, 'utf8', () => {
  73. // console.log('')
  74. })
  75. );
  76. tasks.push(
  77. fs.outputFile(path.resolve(__dirname, `../dist/packages/${name}/style.mjs`), outputStyleMjs, 'utf8', () => {
  78. // console.log('')
  79. })
  80. );
  81. });
  82. Promise.all(tasks);