prepub.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env node
  2. /* eslint-disable */
  3. 'use strict';
  4. const fs = require('fs');
  5. const path = require('path');
  6. const packageInfo = require('../package.json');
  7. const defaultVars = require('./default-vars');
  8. const darkVars = require('./dark-vars');
  9. function generateVersionFile() {
  10. if (fs.existsSync(path.join(__dirname, '../lib'))) {
  11. // Build package.json version to lib/version/index.js
  12. // prevent json-loader needing in user-side
  13. const versionFilePath = path.join(process.cwd(), 'lib', 'version', 'index.js');
  14. const versionFileContent = fs.readFileSync(versionFilePath).toString();
  15. fs.writeFileSync(
  16. versionFilePath,
  17. versionFileContent.replace(
  18. `require('../../package.json')`,
  19. `{ version: '${packageInfo.version}' }`,
  20. ),
  21. );
  22. console.log('Wrote version into lib/version/index.js');
  23. }
  24. }
  25. function generateThemeFileContent(theme) {
  26. return `const { ${theme}ThemeSingle } = require('./theme');\nconst defaultTheme = require('./default-theme');\n
  27. module.exports = {
  28. ...defaultTheme,
  29. ...${theme}ThemeSingle
  30. }`;
  31. }
  32. // We need compile additional content for antd user
  33. function finalizeCompile() {
  34. if (fs.existsSync(path.join(__dirname, '../lib'))) {
  35. // Build a entry less file to dist/antd.less
  36. const componentsPath = path.join(process.cwd(), 'components');
  37. let componentsLessContent = '';
  38. // Build components in one file: lib/style/components.less
  39. fs.readdir(componentsPath, (err, files) => {
  40. files.forEach(file => {
  41. if (fs.existsSync(path.join(componentsPath, file, 'style', 'index.less'))) {
  42. componentsLessContent += `@import "../${path.join(file, 'style', 'index.less')}";\n`;
  43. }
  44. });
  45. fs.writeFileSync(
  46. path.join(process.cwd(), 'lib', 'style', 'components.less'),
  47. componentsLessContent,
  48. );
  49. });
  50. }
  51. }
  52. function buildThemeFile(theme, vars) {
  53. // Build less entry file: dist/antd.${theme}.less
  54. if (theme !== 'default') {
  55. fs.writeFileSync(
  56. path.join(process.cwd(), 'dist', `antd.${theme}.less`),
  57. `@import "../lib/style/${theme}.less";\n@import "../lib/style/components.less";`,
  58. );
  59. // eslint-disable-next-line no-console
  60. console.log(`Built a entry less file to dist/antd.${theme}.less`);
  61. } else {
  62. fs.writeFileSync(
  63. path.join(process.cwd(), 'dist', `default-theme.js`),
  64. `module.exports = ${JSON.stringify(vars, null, 2)};\n`,
  65. );
  66. return;
  67. }
  68. // Build ${theme}.js: dist/${theme}-theme.js, for less-loader
  69. fs.writeFileSync(
  70. path.join(process.cwd(), 'dist', `theme.js`),
  71. `const ${theme}ThemeSingle = ${JSON.stringify(vars, null, 2)};\n`,
  72. {
  73. flag: 'a',
  74. },
  75. );
  76. fs.writeFileSync(
  77. path.join(process.cwd(), 'dist', `${theme}-theme.js`),
  78. generateThemeFileContent(theme),
  79. );
  80. // eslint-disable-next-line no-console
  81. console.log(`Built a ${theme} theme js file to dist/${theme}-theme.js`);
  82. }
  83. function finalizeDist() {
  84. if (fs.existsSync(path.join(__dirname, '../dist'))) {
  85. // Build less entry file: dist/antd.less
  86. fs.writeFileSync(
  87. path.join(process.cwd(), 'dist', 'antd.less'),
  88. '@import "../lib/style/index.less";\n@import "../lib/style/components.less";',
  89. );
  90. // eslint-disable-next-line no-console
  91. fs.writeFileSync(
  92. path.join(process.cwd(), 'dist', 'theme.js'),
  93. `const defaultTheme = require('./default-theme.js');\n`,
  94. );
  95. // eslint-disable-next-line no-console
  96. console.log('Built a entry less file to dist/antd.less');
  97. buildThemeFile('default', defaultVars);
  98. buildThemeFile('dark', darkVars);
  99. // buildThemeFile('compact', compactVars);
  100. fs.writeFileSync(
  101. path.join(process.cwd(), 'dist', `theme.js`),
  102. `
  103. function getThemeVariables(options = {}) {
  104. let themeVar = {
  105. 'hack': \`true;@import "\${require.resolve('ant-design-vue/lib/style/color/colorPalette.less')}";\`,
  106. ...defaultTheme
  107. };
  108. if(options.dark) {
  109. themeVar = {
  110. ...themeVar,
  111. ...darkThemeSingle
  112. }
  113. }
  114. if(options.compact){
  115. themeVar = {
  116. ...themeVar
  117. }
  118. }
  119. return themeVar;
  120. }
  121. module.exports = {
  122. darkThemeSingle,
  123. getThemeVariables
  124. }`,
  125. {
  126. flag: 'a',
  127. },
  128. );
  129. }
  130. }
  131. generateVersionFile();
  132. finalizeCompile();
  133. finalizeDist();