gulpfile.js 3.1 KB

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