gulpfile.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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('[FATAL]', err);
  31. return;
  32. }
  33. if (stats.hasErrors()) {
  34. gutil.log('[ERROR] webpack compilation failed\n', stats.toJson().errors.join('\n'));
  35. return;
  36. }
  37. if (stats.hasWarnings()) {
  38. gutil.log('[WARNING] webpack compilation has warnings\n', stats.toJson().warnings.join('\n'));
  39. }
  40. stats.stats.forEach(stat => {
  41. const timeCost = (stat.endTime - stat.startTime) / 1000;
  42. const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
  43. gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  44. });
  45. }
  46. gulp.task('clean', () => del(['dist']));
  47. gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
  48. gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
  49. gulp.watch(paths.manifest, ['manifest']);
  50. gulp.watch(paths.copy, ['copy-files']);
  51. gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
  52. });
  53. gulp.task('build', ['pack', 'js-prd', 'svg']);
  54. gulp.task('js-dev', () => webpack(webpackConfig).watch({}, webpackCallback));
  55. gulp.task('js-prd', () => webpack(webpackConfig, webpackCallback));
  56. gulp.task('manifest', () => (
  57. gulp.src(paths.manifest, {base: 'src'})
  58. .pipe(json(data => {
  59. data.version = pkg.version.replace(/-[^.]*/, '');
  60. return data;
  61. }))
  62. .pipe(gulp.dest('dist'))
  63. ));
  64. gulp.task('copy-files', () => {
  65. const jsFilter = gulpFilter(['**/*.js'], {restore: true});
  66. const cssFilter = gulpFilter(['**/*.css'], {restore: true});
  67. let stream = gulp.src(paths.copy, {base: 'src'});
  68. if (isProd) stream = stream
  69. .pipe(jsFilter)
  70. .pipe(uglify())
  71. .pipe(jsFilter.restore);
  72. stream = stream
  73. .pipe(cssFilter)
  74. .pipe(postcss([
  75. require('precss')(),
  76. isProd && require('cssnano')({
  77. // zindex: false,
  78. }),
  79. ].filter(Boolean)))
  80. .pipe(cssFilter.restore)
  81. .pipe(gulp.dest('dist/'));
  82. });
  83. gulp.task('copy-i18n', () => (
  84. gulp.src(paths.templates)
  85. .pipe(i18n.extract({
  86. base: 'src',
  87. prefix: '_locales',
  88. touchedOnly: true,
  89. useDefaultLang: true,
  90. markUntouched: false,
  91. extension: '.json',
  92. }))
  93. .pipe(gulp.dest('dist'))
  94. ));
  95. gulp.task('svg', () => (
  96. gulp.src('icons/*.svg')
  97. .pipe(svgSprite({
  98. mode: {
  99. symbol: {
  100. dest: '',
  101. sprite: 'sprite.svg',
  102. },
  103. },
  104. }))
  105. .pipe(gulp.dest('dist/public'))
  106. ));
  107. gulp.task('i18n', () => (
  108. gulp.src(paths.templates)
  109. .pipe(i18n.extract({
  110. base: 'src',
  111. prefix: '_locales',
  112. touchedOnly: false,
  113. useDefaultLang: false,
  114. markUntouched: true,
  115. extension: '.yml',
  116. }))
  117. .pipe(gulp.dest('src'))
  118. ));