gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview 自动构建程序
  3. * @author Jin Yufeng
  4. */
  5. // 载入 gulp 插件
  6. const gulp = require('gulp')
  7. const htmlmin = require('gulp-htmlmin')
  8. const babel = require('gulp-babel')
  9. const uglify = require('gulp-uglify')
  10. const cleanCss = require('gulp-clean-css')
  11. const clean = require('gulp-clean')
  12. const gulpif = require('gulp-if')
  13. const plumber = require('gulp-plumber')
  14. const size = require('gulp-size')
  15. // 载入构建工具
  16. const config = require('./tools/config')
  17. const converter = require('./tools/converter')
  18. const ifdef = require('./tools/ifdef')
  19. const minifier = require('./tools/minifier')
  20. const plugin = require('./tools/plugin')
  21. // 载入环境信息
  22. const isDev = process.argv.includes('--dev')
  23. let platform = process.argv[3]
  24. if (!platform) {
  25. throw Error('缺少平台信息')
  26. }
  27. platform = platform.substr(2)
  28. /**
  29. * @description 清理文件夹
  30. */
  31. gulp.task('clean', () => {
  32. return gulp.src(`${isDev ? 'dev' : 'dist'}/${platform === 'all' ? '' : platform + '/'}*`, {
  33. read: false,
  34. allowEmpty: true
  35. })
  36. .pipe(clean())
  37. })
  38. /**
  39. * @description 生成原生组件包(含插件)
  40. * @returns {NodeJS.ReadWriteStream}
  41. */
  42. function packComp () {
  43. return gulp.src(['plugins/**/*', 'src/**/*'], {
  44. nodir: true
  45. })
  46. // 公共处理
  47. .pipe(plumber()) // 错误处理
  48. .pipe(plugin.build(platform)) // 构建插件
  49. .pipe(ifdef(platform)) // 条件编译
  50. // wxml 处理
  51. .pipe(gulpif(file => file.extname === '.wxml', minifier.wxs())) // 压缩内联 wxs
  52. .pipe(gulpif(file => file.extname === '.wxml', htmlmin(config.htmlmin))) // 压缩 wxml
  53. .pipe(gulpif(file => file.extname === '.html', htmlmin(Object.assign({}, config.htmlmin, { // 压缩 html
  54. minifyCSS: true
  55. }))))
  56. // js 处理
  57. .pipe(gulpif(file => file.extname === '.js' && !file.stem.includes('.min') && (platform !== 'uni-app' || file.relative.includes('static')), babel(config.babel))) // es6 转 es5
  58. .pipe(gulpif(file => file.extname === '.js' && !file.stem.includes('.min') && !isDev && (platform !== 'uni-app' || file.relative.includes('static')), uglify(config.uglify))) // 压缩 js
  59. // wxss(css)处理
  60. .pipe(gulpif(file => file.extname.includes('ss'), cleanCss(config.cleanCss))) // 压缩 wxss
  61. .pipe(plugin.importCss()) // 引入插件中的 css 文件
  62. // json 处理
  63. .pipe(gulpif(file => file.extname === '.json', minifier.json())) // 压缩 json
  64. // 公共处理
  65. .pipe(converter(platform)) // 将微信端的代码转换到各个平台
  66. .pipe(gulpif(!isDev, size({
  67. title: `${platform} 包生成完毕`
  68. })))
  69. .pipe(gulp.dest(file => {
  70. return `${isDev ? 'dev' : 'dist'}/${platform}/${(platform === 'uni-app' && !file.relative.includes('components') && !file.relative.includes('static')) || (platform !== 'uni-app' && isDev) ? 'components/mp-html/' : ''}`
  71. }))
  72. }
  73. gulp.task('build', gulp.series('clean', packComp))
  74. /**
  75. * @description 生成原生示例项目
  76. * @returns {NodeJS.ReadWriteStream}
  77. */
  78. function packDemo () {
  79. return gulp.src(['tools/demo/**/*', 'test/content.js'], {
  80. nodir: true
  81. })
  82. .pipe(ifdef(platform))
  83. .pipe(gulpif(platform !== 'uni-app', converter(platform)))
  84. .pipe(gulp.dest(`dev/${platform}/`))
  85. }
  86. gulp.task('dev', gulp.series('clean', gulp.parallel(packComp, packDemo)))
  87. /**
  88. * @description 监听文件变化
  89. */
  90. gulp.task('watch-demo', () => {
  91. gulp.watch(['tools/demo/**/*', 'test/content.js']).on('all', (type, file) => {
  92. console.log(type + ':' + file)
  93. packDemo()
  94. })
  95. })
  96. gulp.task('watch-comp', () => {
  97. gulp.watch(['src/**/*', 'src/common/**/*', 'plugins/**/*']).on('all', (type, file) => {
  98. console.log(type + ':' + file)
  99. packComp()
  100. })
  101. })
  102. gulp.task('watch', gulp.parallel('watch-demo', 'watch-comp'))