gulpfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const gulp = require('gulp');
  2. const del = require('del');
  3. const log = require('fancy-log');
  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 { isProd } = require('./scripts/utils');
  13. const pkg = require('./package.json');
  14. const DIST = 'dist';
  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. log('[FATAL]', err);
  31. return;
  32. }
  33. if (stats.hasErrors()) {
  34. log('[ERROR] webpack compilation failed\n', stats.toJson().errors.join('\n'));
  35. return;
  36. }
  37. if (stats.hasWarnings()) {
  38. 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. log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
  45. });
  46. }
  47. function clean() {
  48. return del(DIST);
  49. }
  50. function watch() {
  51. gulp.watch(paths.manifest, manifest);
  52. gulp.watch(paths.copy, copyFiles);
  53. gulp.watch(paths.locales.concat(paths.templates), copyI18n);
  54. }
  55. function jsDev(done) {
  56. let firstRun = true;
  57. webpack(webpackConfig).watch({}, (...args) => {
  58. webpackCallback(...args);
  59. if (firstRun) {
  60. firstRun = false;
  61. done();
  62. }
  63. });
  64. }
  65. function jsProd(done) {
  66. webpack(webpackConfig, (...args) => {
  67. webpackCallback(...args);
  68. done();
  69. });
  70. }
  71. function manifest() {
  72. return gulp.src(paths.manifest, { base: 'src' })
  73. .pipe(string((input, file) => {
  74. const data = yaml.safeLoad(input);
  75. // Strip alphabetic suffix
  76. data.version = pkg.version.replace(/-[^.]*/, '');
  77. if (process.env.TARGET === 'firefox') {
  78. data.version += 'f';
  79. data.applications.gecko.update_url = 'https://violentmonkey.top/static/updates.json';
  80. }
  81. file.path = file.path.replace(/\.yml$/, '.json');
  82. return JSON.stringify(data);
  83. }))
  84. .pipe(gulp.dest(DIST));
  85. }
  86. function copyFiles() {
  87. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  88. let stream = gulp.src(paths.copy, { base: 'src' });
  89. if (isProd) stream = stream
  90. .pipe(jsFilter)
  91. .pipe(uglify())
  92. .pipe(jsFilter.restore);
  93. return stream
  94. .pipe(gulp.dest(DIST));
  95. }
  96. function checkI18n() {
  97. return gulp.src(paths.templates)
  98. .pipe(i18n.extract({
  99. base: 'src/_locales',
  100. extension: '.json',
  101. }));
  102. }
  103. function copyI18n() {
  104. return gulp.src(paths.templates)
  105. .pipe(plumber(logError))
  106. .pipe(i18n.extract({
  107. base: 'src/_locales',
  108. touchedOnly: true,
  109. useDefaultLang: true,
  110. markUntouched: false,
  111. extension: '.json',
  112. }))
  113. .pipe(gulp.dest(`${DIST}/_locales`));
  114. }
  115. /**
  116. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  117. * update them with keys in template files, then store in `message.yml`.
  118. */
  119. function updateI18n() {
  120. return gulp.src(paths.templates)
  121. .pipe(plumber(logError))
  122. .pipe(i18n.extract({
  123. base: 'src/_locales',
  124. touchedOnly: false,
  125. useDefaultLang: false,
  126. markUntouched: true,
  127. extension: '.yml',
  128. }))
  129. .pipe(gulp.dest('src/_locales'));
  130. }
  131. function logError(err) {
  132. log(err.toString());
  133. return this.emit('end');
  134. }
  135. const pack = gulp.parallel(manifest, copyFiles, copyI18n);
  136. exports.clean = clean;
  137. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  138. exports.build = gulp.parallel(pack, jsProd);
  139. exports.i18n = updateI18n;
  140. exports.check = checkI18n;