gulpfile.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. const { src, dest, series, parallel, watch } = require("gulp");
  3. const buffer = require("gulp-buffer");
  4. const browserify = require("browserify");
  5. const concat = require("gulp-concat");
  6. const connect = require("gulp-connect");
  7. const eslint = require("gulp-eslint7");
  8. const header = require("gulp-header");
  9. const log = require("gulplog");
  10. const postcss = require("gulp-postcss");
  11. const sourcemaps = require("gulp-sourcemaps");
  12. const stylelint = require("gulp-stylelint");
  13. const tap = require("gulp-tap");
  14. const terser = require("gulp-terser");
  15. const output =
  16. process.argv.indexOf("--output") != -1
  17. ? process.argv[process.argv.indexOf("--output") + 1]
  18. : "build/converted-asciidoc";
  19. function info(cb) {
  20. log.info("Writing to " + output);
  21. cb();
  22. }
  23. function css() {
  24. return src("src/main/css/site.css")
  25. .pipe(
  26. stylelint({
  27. failAfterError: true,
  28. reporters: [{ formatter: "string", console: true }],
  29. })
  30. )
  31. .pipe(sourcemaps.init())
  32. .pipe(postcss())
  33. .pipe(
  34. /* MPL 2.0 requirement */
  35. header(
  36. "/*\n\nSource Available at https://github.com/spring-io/asciidoctor-spring-backend */\n\n\n"
  37. )
  38. )
  39. .pipe(sourcemaps.write("./"))
  40. .pipe(dest(output + "/css"))
  41. .pipe(connect.reload());
  42. }
  43. function img() {
  44. return src("src/main/img/*").pipe(dest(output + "/img"));
  45. }
  46. function jsSetup() {
  47. return jsFolder("setup");
  48. }
  49. function jsSite() {
  50. return jsFolder("site");
  51. }
  52. function jsFolder(name) {
  53. return src("src/main/js/" + name + "/*.js", { base: 'src/main/js' })
  54. .pipe(eslint())
  55. .pipe(eslint.format())
  56. .pipe(eslint.failAfterError())
  57. .pipe(tapBrowserify())
  58. .pipe(buffer())
  59. .pipe(initSourcemaps())
  60. .pipe(terser())
  61. .pipe(concat("js/" + name + ".js"))
  62. .pipe(sourcemaps.write("./"))
  63. .pipe(dest(output))
  64. .pipe(connect.reload());
  65. }
  66. function tapBrowserify() {
  67. return tap(function (file) {
  68. file.contents = browserify(file.path, {
  69. debug: true,
  70. })
  71. .plugin("browser-pack-flat/plugin")
  72. .bundle();
  73. });
  74. }
  75. function initSourcemaps() {
  76. return sourcemaps.init({
  77. loadMaps: true,
  78. });
  79. }
  80. function webServer(cb) {
  81. connect.server({
  82. root: output,
  83. livereload: true,
  84. });
  85. cb();
  86. }
  87. function watchFiles(cb) {
  88. watch("src/main/**", build);
  89. cb();
  90. }
  91. const build = series(info, css, img, jsSetup, jsSite);
  92. exports.default = build;
  93. exports.build = build;
  94. exports.dev = series(build, parallel(webServer, watchFiles));