gulpfile.js 3.7 KB

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