gulpfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var gulp = require('gulp')
  2. var $ = require('gulp-load-plugins')()
  3. var fs = require('fs')
  4. var path = require('path')
  5. var del = require('del')
  6. //var colors = require('colors')
  7. var child_process = require('child_process')
  8. var theme = {}
  9. var themeList = require('./src/element-ui/config.js').filter(item => !item.hasBuild)
  10. var styleFileDir = './src/assets/scss'
  11. var styleFileDirTemp = `${styleFileDir}-temp`
  12. var themeFileDir = './public/element-theme'
  13. var et = require('element-theme')
  14. var etOptions = require('./package.json')['element-theme']
  15. var themeFileName = etOptions.config.replace(/.*\/(.+\.scss)/, '$1')
  16. /**
  17. * 构建生成主题
  18. */
  19. gulp.task('themes', () => {
  20. if (themeList.length <= 0) { return del(styleFileDirTemp) }
  21. // 删除临时文件,保证本次操作正常执行
  22. //del(styleFileDirTemp)
  23. console.log('del(styleFileDirTemp)');
  24. // 拷贝一份scss样式文件夹,作为构建的临时处理文件夹
  25. //child_process.spawnSync('cp', ['-r', styleFileDir, styleFileDirTemp])
  26. console.log('copy styleFileDirTemp');
  27. // 拷贝element组件scss变量样式文件至临时处理文件夹中,并修改相应配置信息
  28. //child_process.spawnSync('cp', ['-r', etOptions.config, styleFileDirTemp])
  29. etOptions.config = `${styleFileDirTemp}/${themeFileName}`
  30. // 开始构建生成
  31. fnCreate(themeList)
  32. function fnCreate (themeList) {
  33. if (themeList.length >= 1) {
  34. // 保存当前构建生成的主题对象
  35. theme = themeList[0]
  36. console.log('\n')
  37. console.log('-------------------- 待构建,主题 -------------------------')
  38. console.log(themeList)
  39. console.log('\n')
  40. console.log('-------------------- 构建中,主题 -------------------------')
  41. console.log(theme)
  42. console.log('\n')
  43. // 修改.scss临时文件中的$--color-primary主题变量值
  44. var data = fs.readFileSync(etOptions.config, 'utf8')
  45. var result = data.replace(/\$--color-primary:(.*) !default;/, `$--color-primary:${theme.color} !default;`)
  46. fs.writeFileSync(path.resolve(etOptions.config), result)
  47. // 修改aui.scss临时文件中引入element组件主题变量文件路径
  48. var data = fs.readFileSync(`${styleFileDirTemp}/aui.scss`, 'utf8')
  49. var result = data.replace(new RegExp(`(@import \")(.*\/)(${themeFileName}\";)`), '$1./$3')
  50. fs.writeFileSync(path.resolve(`${styleFileDirTemp}/aui.scss`), result)
  51. // 调用element-theme插件,生成element组件主题
  52. etOptions.out = `${themeFileDir}/${theme.name}`
  53. et.run(etOptions, () => {
  54. // 生成后,构建同主题色aui.css项目主题
  55. gulp.start(['styles'], () => {
  56. // 递归下一步
  57. themeList.splice(0, 1)
  58. fnCreate(themeList)
  59. })
  60. })
  61. } else {
  62. // 删除临时文件
  63. del(styleFileDirTemp)
  64. console.log('\n')
  65. console.log('-------------------- 构建完毕,删除临时文件 -------------------------')
  66. console.log(styleFileDirTemp)
  67. console.log('\n')
  68. // 删除主题不需要的部分文件
  69. var files = [
  70. `${themeFileDir}/**/*.css`,
  71. `!${themeFileDir}/**/index.css`,
  72. `!${themeFileDir}/**/aui.css`,
  73. `!${themeFileDir}/**/fonts`
  74. ]
  75. del(files)
  76. console.log('-------------------- 构建完毕,删除主题独立组件文件 -------------------------')
  77. console.log(files)
  78. console.log('\n')
  79. }
  80. }
  81. })
  82. gulp.task('styles', () => {
  83. return gulp.src([`${styleFileDirTemp}/aui.scss`])
  84. .pipe($.sass().on('error', $.sass.logError))
  85. .pipe($.autoprefixer({
  86. browsers: etOptions.browsers,
  87. cascade: false
  88. }))
  89. .pipe($.cleanCss())
  90. .pipe($.rename('aui.css'))
  91. .pipe(gulp.dest(`${themeFileDir}/${theme.name}`))
  92. })