generate-style-deps.cjs 2.6 KB

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