gulpfile.js 3.1 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 gulpFilter = require('gulp-filter');
  6. const uglify = require('gulp-uglify');
  7. const svgSprite = require('gulp-svg-sprite');
  8. const webpack = require('webpack');
  9. const i18n = require('./scripts/i18n');
  10. const json = require('./scripts/json');
  11. const pkg = require('./package.json');
  12. const isProd = process.env.NODE_ENV === 'production';
  13. const webpackConfig = require('./scripts/webpack.conf');
  14. const paths = {
  15. manifest: 'src/manifest.json',
  16. copy: [
  17. 'src/public/**',
  18. ],
  19. locales: [
  20. 'src/_locales/**',
  21. ],
  22. templates: [
  23. 'src/**/*.@(js|html|json|yml|vue)',
  24. ],
  25. };
  26. function webpackCallback(err, stats) {
  27. if (err) {
  28. gutil.log('[FATAL]', err);
  29. return;
  30. }
  31. if (stats.hasErrors()) {
  32. gutil.log('[ERROR] webpack compilation failed\n', stats.toJson().errors.join('\n'));
  33. return;
  34. }
  35. if (stats.hasWarnings()) {
  36. gutil.log('[WARNING] webpack compilation has warnings\n', stats.toJson().warnings.join('\n'));
  37. }
  38. stats.stats.forEach(stat => {
  39. const timeCost = (stat.endTime - stat.startTime) / 1000;
  40. const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
  41. gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  42. });
  43. }
  44. gulp.task('clean', () => del(['dist']));
  45. gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
  46. gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
  47. gulp.watch(paths.manifest, ['manifest']);
  48. gulp.watch(paths.copy, ['copy-files']);
  49. gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
  50. });
  51. gulp.task('build', ['pack', 'js-prd', 'svg']);
  52. gulp.task('js-dev', () => webpack(webpackConfig).watch({}, webpackCallback));
  53. gulp.task('js-prd', () => webpack(webpackConfig, webpackCallback));
  54. gulp.task('manifest', () => (
  55. gulp.src(paths.manifest, {base: 'src'})
  56. .pipe(json(data => {
  57. data.version = pkg.version.replace(/-[^.]*/, '');
  58. return data;
  59. }))
  60. .pipe(gulp.dest('dist'))
  61. ));
  62. gulp.task('copy-files', () => {
  63. const jsFilter = gulpFilter(['**/*.js'], {restore: true});
  64. let stream = gulp.src(paths.copy, {base: 'src'});
  65. if (isProd) stream = stream
  66. .pipe(jsFilter)
  67. .pipe(uglify())
  68. .pipe(jsFilter.restore);
  69. stream = stream
  70. .pipe(gulp.dest('dist/'));
  71. });
  72. gulp.task('copy-i18n', () => (
  73. gulp.src(paths.templates)
  74. .pipe(i18n.extract({
  75. base: 'src',
  76. prefix: '_locales',
  77. touchedOnly: true,
  78. useDefaultLang: true,
  79. markUntouched: false,
  80. extension: '.json',
  81. }))
  82. .pipe(gulp.dest('dist'))
  83. ));
  84. gulp.task('svg', () => (
  85. gulp.src('icons/*.svg')
  86. .pipe(svgSprite({
  87. mode: {
  88. symbol: {
  89. dest: '',
  90. sprite: 'sprite.svg',
  91. },
  92. },
  93. }))
  94. .pipe(gulp.dest('dist/public'))
  95. ));
  96. /**
  97. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  98. * update them with keys in template files, then store in `message.yml`.
  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. ));