后端

gulpfile.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. version = (d.getFullYear().toString().slice(2))
  13. + ((d.getMonth() + 1) >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1))
  14. + (d.getDate() >= 10 ? d.getDate() : '0' + d.getDate());
  15. versionPath = distPath + '/' + version;
  16. })();
  17. // 编译
  18. gulp.task('build', $.shell.task([ 'node build/build.js' ]));
  19. // 创建版本号目录
  20. gulp.task('create:versionPath', ['build'], function () {
  21. return gulp.src(`${distPath}/static/**/*`)
  22. .pipe(gulp.dest(`${versionPath}/static/`))
  23. });
  24. // 替换${versionPath}/static/js/manifest.js window.SITE_CONFIG.cdnUrl占位变量
  25. gulp.task('replace:cdnUrl', ['create:versionPath'], function () {
  26. return gulp.src(`${versionPath}/static/js/manifest.js`)
  27. .pipe($.replace(/"\.\/"/g, 'window.SITE_CONFIG.cdnUrl + "/"'))
  28. .pipe(gulp.dest(`${versionPath}/static/js/`))
  29. });
  30. // 替换${versionPath}/static/config/index-${env}.js window.SITE_CONFIG.staticFileName配置变量
  31. gulp.task('replace:staticFileName', ['create:versionPath'], function () {
  32. return gulp.src(`${versionPath}/static/config/index-${env}.js`)
  33. .pipe($.replace(/window.SITE_CONFIG.staticFileName = \'.*\'/g, `window.SITE_CONFIG.staticFileName = \'${version}\'`))
  34. .pipe(gulp.dest(`${versionPath}/static/config/`))
  35. });
  36. // 合并${versionPath}/static/config/[index-${env}, init].js 至 ${distPath}/config/index.js
  37. gulp.task('concat:config', ['replace:staticFileName'], function () {
  38. return gulp.src([`${versionPath}/static/config/index-${env}.js`, `${versionPath}/static/config/init.js`])
  39. .pipe($.concat('index.js'))
  40. .pipe(gulp.dest(`${distPath}/config/`))
  41. });
  42. // 清空
  43. gulp.task('clean', function () {
  44. return del([versionPath])
  45. });
  46. gulp.task('default', ['clean'], function () {
  47. // 获取环境配置
  48. env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod'
  49. // 开始打包编译
  50. gulp.start(['replace:cdnUrl', 'replace:staticFileName', 'concat:config'], function () {
  51. // 清除, 编译 / 处理项目中产生的文件
  52. del([`${distPath}/static`, `${versionPath}/static/config`])
  53. })
  54. });