gulpfile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. keep: true,
  62. });
  63. }
  64. function manifest() {
  65. return 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. file.path = file.path.replace(/\.yml$/, '.json');
  71. return JSON.stringify(data);
  72. }))
  73. .pipe(gulp.dest(DIST));
  74. }
  75. function copyFiles() {
  76. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  77. let stream = gulp.src(paths.copy, { base: 'src' });
  78. if (isProd) stream = stream
  79. .pipe(jsFilter)
  80. .pipe(uglify())
  81. .pipe(jsFilter.restore);
  82. return stream
  83. .pipe(gulp.dest(DIST));
  84. }
  85. function checkI18n() {
  86. return gulp.src(paths.templates)
  87. .pipe(i18n.extract({
  88. base: 'src/_locales',
  89. extension: '.json',
  90. }));
  91. }
  92. function copyI18n() {
  93. return gulp.src(paths.templates)
  94. .pipe(plumber(logError))
  95. .pipe(i18n.extract({
  96. base: 'src/_locales',
  97. touchedOnly: true,
  98. useDefaultLang: true,
  99. markUntouched: false,
  100. extension: '.json',
  101. }))
  102. .pipe(gulp.dest(`${DIST}/_locales`));
  103. }
  104. /**
  105. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  106. * update them with keys in template files, then store in `message.yml`.
  107. */
  108. function updateI18n() {
  109. return gulp.src(paths.templates)
  110. .pipe(plumber(logError))
  111. .pipe(i18n.extract({
  112. base: 'src/_locales',
  113. touchedOnly: false,
  114. useDefaultLang: false,
  115. markUntouched: true,
  116. extension: '.yml',
  117. }))
  118. .pipe(gulp.dest('src/_locales'));
  119. }
  120. function logError(err) {
  121. log(err.toString());
  122. return this.emit('end');
  123. }
  124. const pack = gulp.parallel(manifest, copyFiles, copyI18n);
  125. exports.clean = clean;
  126. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  127. exports.build = gulp.series(clean, gulp.parallel(pack, jsProd));
  128. exports.i18n = updateI18n;
  129. exports.check = checkI18n;