gulpfile.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var gulp = require('gulp');
  2. var $ = require('gulp-load-plugins')();
  3. var path = require('path');
  4. var del = require('del');
  5. var exec = require('child_process').exec;
  6. var distPath = path.resolve('./dist');
  7. var version = ''; // 版本号
  8. var versionPath = ''; // 版本号路径
  9. var env = 'prod'; // 运行环境
  10. // 创建版本号(年月日时分)
  11. (function (cb) {
  12. var d = new Date();
  13. var yy = d.getFullYear().toString().slice(2);
  14. var MM = d.getMonth() + 1 >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
  15. var DD = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate();
  16. var h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours();
  17. var mm = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes();
  18. version = yy + MM + DD + h + mm;
  19. versionPath = distPath + '/' + version;
  20. })();
  21. // 编译
  22. gulp.task('build', function (cb) {
  23. exec('node build/build.js', () => cb())
  24. });
  25. // 创建版本号目录
  26. gulp.task('create:versionCatalog', function (cb) {
  27. gulp.src(`${distPath}/static/**/*`)
  28. .pipe(gulp.dest(`${versionPath}/static/`)).on('end', () => cb())
  29. });
  30. // 替换${versionPath}/static/js/manifest.js window.SITE_CONFIG.cdnUrl占位变量
  31. gulp.task('replace:cdnUrl', function (cb) {
  32. gulp.src(`${versionPath}/static/js/manifest.js`)
  33. .pipe($.replace(new RegExp(`"${require('./config').build.assetsPublicPath}"`, 'g'), 'window.SITE_CONFIG.cdnUrl + "/"'))
  34. .pipe(gulp.dest(`${versionPath}/static/js/`)).on('end', () => cb())
  35. });
  36. // 替换${versionPath}/static/config/index-${env}.js window.SITE_CONFIG['version']配置变量
  37. gulp.task('replace:version', function (cb) {
  38. gulp.src(`${versionPath}/static/config/index-${env}.js`)
  39. .pipe($.replace(/window.SITE_CONFIG\['version'\] = '.*'/g, `window.SITE_CONFIG['version'] = '${version}'`))
  40. .pipe(gulp.dest(`${versionPath}/static/config/`)).on('end', () => cb())
  41. });
  42. // 合并${versionPath}/static/config/[index-${env}, init].js 至 ${distPath}/config/index.js
  43. gulp.task('concat:config', function (cb) {
  44. gulp.src([`${versionPath}/static/config/index-${env}.js`, `${versionPath}/static/config/init.js`])
  45. .pipe($.concat('index.js'))
  46. .pipe(gulp.dest(`${distPath}/config/`)).on('end', () => cb())
  47. });
  48. // 清空
  49. gulp.task('clean', function (cb) {
  50. del([versionPath])
  51. });
  52. gulp.task('default', gulp.series('build', gulp.series('create:versionCatalog', 'replace:cdnUrl', 'replace:version', 'concat:config'), function (cb) {
  53. del([`${distPath}/static`, `${versionPath}/static/config`])
  54. cb()
  55. }));