gulpfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var gulp = require('gulp')
  2. var $ = require('gulp-load-plugins')()
  3. var path = require('path')
  4. var del = require('del')
  5. var distPath = path.resolve('./dist')
  6. var version = '' // 版本号
  7. var versionPath = '' // 版本号路径
  8. var env = ''; // 运行环境
  9. // 创建版本号(年月日时分)
  10. (function () {
  11. var d = new Date()
  12. var yy = d.getFullYear().toString().slice(2)
  13. var MM = d.getMonth() + 1 >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1)
  14. var DD = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate()
  15. var h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours()
  16. var mm = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes()
  17. version = yy + MM + DD + h + mm
  18. versionPath = distPath + '/' + version
  19. })()
  20. // 编译
  21. gulp.task('build', $.shell.task(['node build/build.js']))
  22. // 创建版本号目录
  23. gulp.task('create:versionCatalog', gulp.series('build', function () {
  24. return gulp.src(`${distPath}/static/**/*`)
  25. .pipe(gulp.dest(`${versionPath}/static/`))
  26. }))
  27. // 替换${versionPath}/static/js/manifest.js window.SITE_CONFIG.cdnUrl占位变量
  28. gulp.task('replace:cdnUrl', gulp.series('create:versionCatalog', function () {
  29. return gulp.src(`${versionPath}/static/js/manifest.js`)
  30. .pipe($.replace(new RegExp(`"${require('./config').build.assetsPublicPath}"`, 'g'), 'window.SITE_CONFIG.cdnUrl + "/"'))
  31. .pipe(gulp.dest(`${versionPath}/static/js/`))
  32. }))
  33. // 替换${versionPath}/static/config/index-${env}.js window.SITE_CONFIG['version']配置变量
  34. gulp.task('replace:version', gulp.series('create:versionCatalog', function () {
  35. return gulp.src(`${versionPath}/static/config/index-${env}.js`)
  36. .pipe($.replace(/window.SITE_CONFIG\['version'\] = '.*'/g, `window.SITE_CONFIG['version'] = '${version}'`))
  37. .pipe(gulp.dest(`${versionPath}/static/config/`))
  38. }))
  39. // 合并${versionPath}/static/config/[index-${env}, init].js 至 ${distPath}/config/index.js
  40. gulp.task('concat:config', gulp.series('replace:version', function () {
  41. return gulp.src([`${versionPath}/static/config/index-${env}.js`, `${versionPath}/static/config/init.js`])
  42. .pipe($.concat('index.js'))
  43. .pipe(gulp.dest(`${distPath}/config/`))
  44. }))
  45. // 清空文件历史
  46. gulp.task('clean', function () {
  47. console.log('--clean--')
  48. // del([`${distPath}/static`, `${versionPath}/static/config`]);
  49. // return del([versionPath])
  50. return del([`${distPath}`])
  51. })
  52. gulp.task('build-end', function () {
  53. console.log('--builed-end--')
  54. // del([`${distPath}/static`, `${versionPath}/static/config`]);
  55. return del([`${distPath}/static`, `${versionPath}/static/config`]);
  56. })
  57. env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod'
  58. gulp.task('default',
  59. gulp.series('clean',
  60. gulp.parallel('create:versionCatalog', 'replace:cdnUrl', 'replace:version', 'concat:config'),
  61. 'build','build-end')
  62. )
  63. /* gulp.task('default', gulp.series('clean', function (done) {
  64. // 获取环境配置
  65. env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod'
  66. // 开始打包编译
  67. // 开始打包编译
  68. gulp.task('default', gulp.series('build','create:versionCatalog', 'replace:cdnUrl', 'replace:version', 'concat:config', function (done) {
  69. // 清除, 编译 / 处理项目中产生的文件
  70. del([`${distPath}/static`, `${versionPath}/static/config`])
  71. done();
  72. }))
  73. })); */
  74. /*
  75. gulp.task('default', gulp.series('clean', function (done) {
  76. // 获取环境配置
  77. env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod'
  78. // 开始打包编译
  79. // 开始打包编译
  80. gulp.task('default', gulp.series('build','create:versionCatalog', 'replace:cdnUrl', 'replace:version', 'concat:config', function (done) {
  81. // 清除, 编译 / 处理项目中产生的文件
  82. del([`${distPath}/static`, `${versionPath}/static/config`])
  83. done();
  84. }))
  85. }));
  86. */