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