gulpfile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 i18n.read({
  87. base: 'src/_locales',
  88. extension: '.json',
  89. });
  90. }
  91. function copyI18n() {
  92. return i18n.read({
  93. base: 'src/_locales',
  94. touchedOnly: true,
  95. useDefaultLang: true,
  96. markUntouched: false,
  97. extension: '.json',
  98. })
  99. .pipe(gulp.dest(`${DIST}/_locales`));
  100. }
  101. /**
  102. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  103. * update them with keys in template files, then store in `message.yml`.
  104. */
  105. function updateI18n() {
  106. return gulp.src(paths.templates)
  107. .pipe(plumber(logError))
  108. .pipe(i18n.extract({
  109. base: 'src/_locales',
  110. touchedOnly: false,
  111. useDefaultLang: false,
  112. markUntouched: true,
  113. extension: '.yml',
  114. }))
  115. .pipe(gulp.dest('src/_locales'));
  116. }
  117. function logError(err) {
  118. log(err.toString());
  119. return this.emit('end');
  120. }
  121. const pack = gulp.parallel(manifest, copyFiles, copyI18n);
  122. exports.clean = clean;
  123. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  124. exports.build = gulp.series(clean, gulp.parallel(pack, jsProd));
  125. exports.i18n = updateI18n;
  126. exports.check = checkI18n;
  127. exports.copyI18n = copyI18n;