gulpfile.js 2.7 KB

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