gulpfile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/images/**',
  17. 'src/public/lib/**',
  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. (Array.isArray(stats.stats) ? stats.stats : [stats])
  39. .forEach(stat => {
  40. const timeCost = (stat.endTime - stat.startTime) / 1000;
  41. const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
  42. gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  43. });
  44. }
  45. gulp.task('clean', () => del(['dist']));
  46. gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
  47. gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
  48. gulp.watch(paths.manifest, ['manifest']);
  49. gulp.watch(paths.copy, ['copy-files']);
  50. gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
  51. });
  52. gulp.task('build', ['pack', 'js-prd', 'svg']);
  53. gulp.task('js-dev', () => {
  54. webpack(webpackConfig).watch({}, webpackCallback);
  55. });
  56. gulp.task('js-prd', cb => {
  57. webpack(webpackConfig, (...args) => {
  58. webpackCallback(...args);
  59. cb();
  60. });
  61. });
  62. gulp.task('manifest', () => (
  63. gulp.src(paths.manifest, { base: 'src' })
  64. .pipe(json(data => {
  65. data.version = pkg.version.replace(/-[^.]*/, '');
  66. return data;
  67. }))
  68. .pipe(gulp.dest('dist'))
  69. ));
  70. gulp.task('copy-files', () => {
  71. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  72. let stream = gulp.src(paths.copy, { base: 'src' });
  73. if (!IS_DEV) stream = stream
  74. .pipe(jsFilter)
  75. .pipe(uglify())
  76. .pipe(jsFilter.restore);
  77. return stream
  78. .pipe(gulp.dest('dist/'));
  79. });
  80. gulp.task('copy-i18n', () => (
  81. gulp.src(paths.templates)
  82. .pipe(i18n.extract({
  83. base: 'src',
  84. prefix: '_locales',
  85. touchedOnly: true,
  86. useDefaultLang: true,
  87. markUntouched: false,
  88. extension: '.json',
  89. }))
  90. .pipe(gulp.dest('dist'))
  91. ));
  92. gulp.task('svg', () => (
  93. gulp.src('icons/*.svg')
  94. .pipe(svgSprite({
  95. mode: {
  96. symbol: {
  97. dest: '',
  98. sprite: 'sprite.svg',
  99. },
  100. },
  101. }))
  102. .pipe(gulp.dest('dist/public'))
  103. ));
  104. /**
  105. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  106. * update them with keys in template files, then store in `message.yml`.
  107. */
  108. gulp.task('i18n', () => (
  109. gulp.src(paths.templates)
  110. .pipe(i18n.extract({
  111. base: 'src',
  112. prefix: '_locales',
  113. touchedOnly: false,
  114. useDefaultLang: false,
  115. markUntouched: true,
  116. extension: '.yml',
  117. }))
  118. .pipe(gulp.dest('src'))
  119. ));