1
0

gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 { isProd } = require('@gera2ld/plaid/util');
  10. const webpackConfig = require('./scripts/webpack.conf');
  11. const i18n = require('./scripts/i18n');
  12. const string = require('./scripts/string');
  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. async function jsDev() {
  56. require('@gera2ld/plaid-webpack/bin/develop')();
  57. }
  58. async function jsProd() {
  59. return require('@gera2ld/plaid-webpack/bin/build')({
  60. api: true,
  61. });
  62. }
  63. function manifest() {
  64. return 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. function copyFiles() {
  79. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  80. let stream = gulp.src(paths.copy, { base: 'src' });
  81. if (isProd) stream = stream
  82. .pipe(jsFilter)
  83. .pipe(uglify())
  84. .pipe(jsFilter.restore);
  85. return stream
  86. .pipe(gulp.dest(DIST));
  87. }
  88. function checkI18n() {
  89. return gulp.src(paths.templates)
  90. .pipe(i18n.extract({
  91. base: 'src/_locales',
  92. extension: '.json',
  93. }));
  94. }
  95. function copyI18n() {
  96. return gulp.src(paths.templates)
  97. .pipe(plumber(logError))
  98. .pipe(i18n.extract({
  99. base: 'src/_locales',
  100. touchedOnly: true,
  101. useDefaultLang: true,
  102. markUntouched: false,
  103. extension: '.json',
  104. }))
  105. .pipe(gulp.dest(`${DIST}/_locales`));
  106. }
  107. /**
  108. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  109. * update them with keys in template files, then store in `message.yml`.
  110. */
  111. function updateI18n() {
  112. return gulp.src(paths.templates)
  113. .pipe(plumber(logError))
  114. .pipe(i18n.extract({
  115. base: 'src/_locales',
  116. touchedOnly: false,
  117. useDefaultLang: false,
  118. markUntouched: true,
  119. extension: '.yml',
  120. }))
  121. .pipe(gulp.dest('src/_locales'));
  122. }
  123. function logError(err) {
  124. log(err.toString());
  125. return this.emit('end');
  126. }
  127. const pack = gulp.parallel(manifest, copyFiles, copyI18n);
  128. exports.clean = clean;
  129. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  130. exports.build = gulp.parallel(pack, jsProd);
  131. exports.i18n = updateI18n;
  132. exports.check = checkI18n;