gulpfile.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const del = require('del');
  2. const gulp = require('gulp');
  3. const gutil = require('gulp-util');
  4. const concat = require('gulp-concat');
  5. const merge2 = require('merge2');
  6. const postcss = require('gulp-postcss');
  7. const gulpFilter = require('gulp-filter');
  8. const uglify = require('gulp-uglify');
  9. const svgSprite = require('gulp-svg-sprite');
  10. const webpack = require('webpack');
  11. const i18n = require('./scripts/i18n');
  12. const json = require('./scripts/json');
  13. const pkg = require('./package.json');
  14. const isProd = process.env.NODE_ENV === 'production';
  15. const webpackConfig = require('./scripts/webpack.conf');
  16. const paths = {
  17. manifest: 'src/manifest.json',
  18. copy: [
  19. 'src/public/**',
  20. ],
  21. locales: [
  22. 'src/_locales/**',
  23. ],
  24. templates: [
  25. 'src/**/*.@(js|html|json|yml|vue)',
  26. ],
  27. };
  28. function webpackCallback(err, stats) {
  29. if (err) {
  30. gutil.log('[ERROR]', err);
  31. return;
  32. }
  33. stats.stats.forEach(stat => {
  34. const timeCost = (stat.endTime - stat.startTime) / 1000;
  35. const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
  36. gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  37. });
  38. }
  39. gulp.task('clean', () => del(['dist']));
  40. gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
  41. gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
  42. gulp.watch(paths.manifest, ['manifest']);
  43. gulp.watch(paths.copy, ['copy-files']);
  44. gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
  45. });
  46. gulp.task('build', ['pack', 'js-prd', 'svg']);
  47. gulp.task('js-dev', () => webpack(webpackConfig).watch({}, webpackCallback));
  48. gulp.task('js-prd', () => webpack(webpackConfig, webpackCallback));
  49. gulp.task('manifest', () => (
  50. gulp.src(paths.manifest, {base: 'src'})
  51. .pipe(json(data => {
  52. data.version = pkg.version.replace(/-[^.]*/, '');
  53. return data;
  54. }))
  55. .pipe(gulp.dest('dist'))
  56. ));
  57. gulp.task('copy-files', () => {
  58. const jsFilter = gulpFilter(['**/*.js'], {restore: true});
  59. const cssFilter = gulpFilter(['**/*.css'], {restore: true});
  60. let stream = gulp.src(paths.copy, {base: 'src'});
  61. if (isProd) stream = stream
  62. .pipe(jsFilter)
  63. .pipe(uglify())
  64. .pipe(jsFilter.restore);
  65. stream = stream
  66. .pipe(cssFilter)
  67. .pipe(postcss([
  68. require('precss')(),
  69. isProd && require('cssnano')({
  70. // zindex: false,
  71. }),
  72. ].filter(Boolean)))
  73. .pipe(cssFilter.restore)
  74. .pipe(gulp.dest('dist/'));
  75. });
  76. gulp.task('copy-i18n', () => (
  77. gulp.src(paths.templates)
  78. .pipe(i18n.extract({
  79. base: 'src',
  80. prefix: '_locales',
  81. touchedOnly: true,
  82. useDefaultLang: true,
  83. markUntouched: false,
  84. extension: '.json',
  85. }))
  86. .pipe(gulp.dest('dist'))
  87. ));
  88. gulp.task('svg', () => (
  89. gulp.src('icons/*.svg')
  90. .pipe(svgSprite({
  91. mode: {
  92. symbol: {
  93. dest: '',
  94. sprite: 'sprite.svg',
  95. },
  96. },
  97. }))
  98. .pipe(gulp.dest('dist/public'))
  99. ));
  100. gulp.task('i18n', () => (
  101. gulp.src(paths.templates)
  102. .pipe(i18n.extract({
  103. base: 'src',
  104. prefix: '_locales',
  105. touchedOnly: false,
  106. useDefaultLang: false,
  107. markUntouched: true,
  108. extension: '.yml',
  109. }))
  110. .pipe(gulp.dest('src'))
  111. ));