generate-unplugin-deps.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const config = require('../src/config.json');
  2. const packageConfig = require('../package.json');
  3. const path = require('path');
  4. const fs = require('fs-extra');
  5. // 获取依赖关系
  6. const styleMap = new Map();
  7. const tasks = [];
  8. let outputFileEntry = ``;
  9. let components = [];
  10. // import Locale from './packages/locale';\n
  11. config.nav.forEach((item) => {
  12. item.packages.forEach((element) => {
  13. styleMap.set(element.name, {
  14. name: element.name,
  15. children: element.styleDeps
  16. });
  17. // gen entry
  18. if (element.exclude != true) {
  19. const outputMjs = `import _${element.name} from '../_es/${element.name}.js';
  20. const treeshaking = (t) => t;
  21. const ${element.name} = treeshaking(_${element.name});
  22. export { ${element.name} };`;
  23. tasks.push(
  24. fs.outputFile(path.resolve(__dirname, `../dist/packages/${element.name}/index.mjs`), outputMjs, 'utf8', () => {
  25. // console.log('')
  26. })
  27. );
  28. let folderName = element.name.toLowerCase();
  29. outputFileEntry += `export * from "./packages/${folderName}/index.mjs";\n`;
  30. components.push(element.name);
  31. }
  32. });
  33. });
  34. outputFileEntry += components.map(name => `import { ${name} } from "./packages/${name}/index.mjs";`).join('\n');
  35. outputFileEntry += `function install(app) {
  36. const packages = [${components.join(',')}];
  37. packages.forEach((item) => {
  38. if (item.install) {
  39. app.use(item);
  40. } else if (item.name) {
  41. app.component(item.name, item);
  42. }
  43. });
  44. }
  45. const version = '${packageConfig.version}';
  46. var stdin_default = {
  47. install,
  48. version
  49. };
  50. export {
  51. stdin_default as default,
  52. install,
  53. version
  54. };`;
  55. tasks.push(
  56. fs.outputFile(path.resolve(__dirname, `../dist/nutui.es.js`), outputFileEntry, 'utf8', () => {
  57. // console.log('')
  58. })
  59. );
  60. styleMap.forEach((value) => {
  61. if (value.children && value.children.length > 0) {
  62. value.children.forEach((item, index) => {
  63. value.children[index] = styleMap.get(item);
  64. });
  65. }
  66. });
  67. const getAllDeps = (styleObj, key) => {
  68. const value = styleObj;
  69. if (value.children?.length === 0) {
  70. return [value.name];
  71. } else {
  72. let deps = [];
  73. value.children?.forEach((item) => {
  74. if (key === item.name) {
  75. console.error('generate-style-deps: 存在循环引用', key);
  76. return [];
  77. }
  78. deps = deps.concat(getAllDeps(item, key));
  79. });
  80. deps.unshift(value.name);
  81. return [...new Set(deps)];
  82. }
  83. };
  84. styleMap.forEach((value, key) => {
  85. const name = key.toLowerCase();
  86. let deps = getAllDeps(value, key);
  87. deps = deps.filter((component) => {
  88. return component !== key;
  89. });
  90. // gen style
  91. const outputStyleCJs = `require('./index.scss');\n${deps
  92. .map((component) => {
  93. return `require('../${component.toLowerCase()}/index.scss');\n`;
  94. })
  95. .join('')}`;
  96. const outputStyleMjs = `import './index.scss';\n${deps
  97. .map((component) => {
  98. return `import '../${component.toLowerCase()}/index.scss';\n`;
  99. })
  100. .join('')}`;
  101. tasks.push(
  102. fs.outputFile(path.resolve(__dirname, `../dist/packages/${name}/style.cjs`), outputStyleCJs, 'utf8', () => {
  103. // console.log('')
  104. })
  105. );
  106. tasks.push(
  107. fs.outputFile(path.resolve(__dirname, `../dist/packages/${name}/style.mjs`), outputStyleMjs, 'utf8', () => {
  108. // console.log('')
  109. })
  110. );
  111. });
  112. Promise.all(tasks);