gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 plumber = require('gulp-plumber');
  8. const yaml = require('js-yaml');
  9. const webpack = require('webpack');
  10. const webpackConfig = require('./scripts/webpack.conf');
  11. const i18n = require('./scripts/i18n');
  12. const string = require('./scripts/string');
  13. const { IS_DEV } = require('./scripts/utils');
  14. const pkg = require('./package.json');
  15. const paths = {
  16. manifest: 'src/manifest.yml',
  17. copy: [
  18. 'src/public/images/**',
  19. 'src/public/lib/**',
  20. ],
  21. locales: [
  22. 'src/_locales/**',
  23. ],
  24. templates: [
  25. 'src/**/*.@(js|html|json|yml|vue)',
  26. ],
  27. };
  28. function webpackCallback(err, stats) {
  29. if (err) {
  30. gutil.log('[FATAL]', err);
  31. return;
  32. }
  33. if (stats.hasErrors()) {
  34. gutil.log('[ERROR] webpack compilation failed\n', stats.toJson().errors.join('\n'));
  35. return;
  36. }
  37. if (stats.hasWarnings()) {
  38. gutil.log('[WARNING] webpack compilation has warnings\n', stats.toJson().warnings.join('\n'));
  39. }
  40. (Array.isArray(stats.stats) ? stats.stats : [stats])
  41. .forEach(stat => {
  42. const timeCost = (stat.endTime - stat.startTime) / 1000;
  43. const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
  44. gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  45. });
  46. }
  47. gulp.task('clean', () => del(['dist']));
  48. gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
  49. gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
  50. gulp.watch(paths.manifest, ['manifest']);
  51. gulp.watch(paths.copy, ['copy-files']);
  52. gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
  53. });
  54. gulp.task('build', ['pack', 'js-prd', 'svg']);
  55. gulp.task('js-dev', () => {
  56. webpack(webpackConfig).watch({}, webpackCallback);
  57. });
  58. gulp.task('js-prd', cb => {
  59. webpack(webpackConfig, (...args) => {
  60. webpackCallback(...args);
  61. cb();
  62. });
  63. });
  64. gulp.task('manifest', () => (
  65. gulp.src(paths.manifest, { base: 'src' })
  66. .pipe(string((input, file) => {
  67. const data = yaml.safeLoad(input);
  68. // Strip alphabetic suffix
  69. data.version = pkg.version.replace(/-[^.]*/, '');
  70. if (process.env.TARGET === 'firefox') {
  71. data.version += 'f';
  72. delete data.applications.gecko.update_url;
  73. }
  74. file.path = file.path.replace(/\.yml$/, '.json');
  75. return JSON.stringify(data);
  76. }))
  77. .pipe(gulp.dest('dist'))
  78. ));
  79. gulp.task('copy-files', () => {
  80. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  81. let stream = gulp.src(paths.copy, { base: 'src' });
  82. if (!IS_DEV) stream = stream
  83. .pipe(jsFilter)
  84. .pipe(uglify())
  85. .pipe(jsFilter.restore);
  86. return stream
  87. .pipe(gulp.dest('dist/'));
  88. });
  89. gulp.task('copy-i18n', () => (
  90. gulp.src(paths.templates)
  91. .pipe(plumber(logError))
  92. .pipe(i18n.extract({
  93. base: 'src',
  94. prefix: '_locales',
  95. touchedOnly: true,
  96. useDefaultLang: true,
  97. markUntouched: false,
  98. extension: '.json',
  99. }))
  100. .pipe(gulp.dest('dist'))
  101. ));
  102. gulp.task('svg', () => (
  103. gulp.src('src/resources/icons/*.svg')
  104. .pipe(svgSprite({
  105. mode: {
  106. symbol: {
  107. dest: '',
  108. sprite: 'sprite.svg',
  109. },
  110. },
  111. }))
  112. .pipe(gulp.dest('dist/public'))
  113. ));
  114. /**
  115. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  116. * update them with keys in template files, then store in `message.yml`.
  117. */
  118. gulp.task('i18n', () => (
  119. gulp.src(paths.templates)
  120. .pipe(plumber(logError))
  121. .pipe(i18n.extract({
  122. base: 'src',
  123. prefix: '_locales',
  124. touchedOnly: false,
  125. useDefaultLang: false,
  126. markUntouched: true,
  127. extension: '.yml',
  128. }))
  129. .pipe(gulp.dest('src'))
  130. ));
  131. function logError(err) {
  132. gutil.log(err.toString());
  133. return this.emit('end');
  134. }