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