gulpfile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 webpackConfig = require('./scripts/webpack.conf');
  9. const i18n = require('./scripts/i18n');
  10. const json = require('./scripts/json');
  11. const { IS_DEV } = require('./scripts/utils');
  12. const pkg = require('./package.json');
  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. (Array.isArray(stats.stats) ? stats.stats : [stats])
  38. .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', () => {
  53. webpack(webpackConfig).watch({}, webpackCallback);
  54. });
  55. gulp.task('js-prd', cb => {
  56. webpack(webpackConfig, (...args) => {
  57. webpackCallback(...args);
  58. cb();
  59. });
  60. });
  61. gulp.task('manifest', () => (
  62. gulp.src(paths.manifest, { base: 'src' })
  63. .pipe(json(data => {
  64. data.version = pkg.version.replace(/-[^.]*/, '');
  65. return data;
  66. }))
  67. .pipe(gulp.dest('dist'))
  68. ));
  69. gulp.task('copy-files', () => {
  70. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  71. let stream = gulp.src(paths.copy, { base: 'src' });
  72. if (!IS_DEV) stream = stream
  73. .pipe(jsFilter)
  74. .pipe(uglify())
  75. .pipe(jsFilter.restore);
  76. return stream
  77. .pipe(gulp.dest('dist/'));
  78. });
  79. gulp.task('copy-i18n', () => (
  80. gulp.src(paths.templates)
  81. .pipe(i18n.extract({
  82. base: 'src',
  83. prefix: '_locales',
  84. touchedOnly: true,
  85. useDefaultLang: true,
  86. markUntouched: false,
  87. extension: '.json',
  88. }))
  89. .pipe(gulp.dest('dist'))
  90. ));
  91. gulp.task('svg', () => (
  92. gulp.src('icons/*.svg')
  93. .pipe(svgSprite({
  94. mode: {
  95. symbol: {
  96. dest: '',
  97. sprite: 'sprite.svg',
  98. },
  99. },
  100. }))
  101. .pipe(gulp.dest('dist/public'))
  102. ));
  103. /**
  104. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  105. * update them with keys in template files, then store in `message.yml`.
  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. ));