Le blog de Victor Héry https://blog.victor-hery.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.9 KiB

  1. import fs from "fs";
  2. import path from "path";
  3. import { src, dest, watch, parallel, series } from "gulp";
  4. import { exec } from "child_process";
  5. import { create as browserSyncCreate } from "browser-sync";
  6. import run from "gulp-run-command";
  7. import postcss from "gulp-postcss";
  8. import magician from "postcss-font-magician";
  9. import cssnano from "cssnano";
  10. import postcssPresetEnv from "postcss-preset-env";
  11. import rfs from "rfs";
  12. import concat from "gulp-concat";
  13. import terser from "gulp-terser";
  14. const browserSync = browserSyncCreate();
  15. const path404 = path.join(__dirname, "documentation/output/404.html");
  16. const content_404 = () =>
  17. fs.existsSync(path404) ? fs.readFileSync(path404) : null;
  18. const cleanOutput = () => exec("cd documentation && rm -rf outout/");
  19. const buildContent = () => exec("cd documentation && invoke build");
  20. const compileBootstrapLess = () =>
  21. exec(
  22. "node_modules/recess/bin/recess --compile static/bootstrap/bootstrap.less > static/css/bootstrap.css"
  23. );
  24. const compileResponsiveLess = () =>
  25. exec(
  26. "node_modules/recess/bin/recess --compile static/bootstrap/responsive.less > static/css/bootstrap_responsive.css"
  27. );
  28. const reload = cb => {
  29. browserSync.init(
  30. {
  31. ui: {
  32. port: 9002
  33. },
  34. server: {
  35. baseDir: "documentation/output",
  36. serveStaticOptions: {
  37. extensions: ["html"]
  38. }
  39. },
  40. files: "documentation/output/*.html",
  41. port: 9001
  42. },
  43. (_, bs) => {
  44. bs.addMiddleware("*", (_, res) => {
  45. res.write(content_404());
  46. res.end();
  47. });
  48. }
  49. );
  50. cb();
  51. };
  52. const watchFiles = () => {
  53. watch(
  54. [
  55. "documentation/content/**/*.md",
  56. "documentation/content/**/*.rest",
  57. "documentation/pelicanconf.py",
  58. "documentation/publishconf.py",
  59. "templates/**/*.html",
  60. "static/**/*.css",
  61. "static/**/*.less",
  62. "static/**/*.js",
  63. "!static/**/bootstrap.css",
  64. "!static/**/bootstrap_responsive.css",
  65. "!static/**/elegant.prod.9e9d5ce754.css",
  66. "!static/js/elegant.prod.9e9d5ce754.js"
  67. ],
  68. { ignoreInitial: false },
  69. buildAll
  70. );
  71. };
  72. const pathProdCSS = path.join(
  73. __dirname,
  74. "static/css/elegant.prod.9e9d5ce754.css"
  75. );
  76. const rmProdCSS = cb => {
  77. if (fs.existsSync(pathProdCSS)) {
  78. fs.unlinkSync(pathProdCSS);
  79. }
  80. cb();
  81. };
  82. const minifyJS = () => {
  83. return src([
  84. "static/applause-button/applause-button.js",
  85. "static/photoswipe/photoswipe.js",
  86. "static/photoswipe/photoswipe-ui-default.js",
  87. "static/photoswipe/photoswipe-array-from-dom.js",
  88. "static/lunr/lunr.js",
  89. "static/clipboard/clipboard.js",
  90. "static/js/copy-to-clipboard.js",
  91. "static/js/lunr-search-result.js",
  92. "!static/js/elegant.prod.9e9d5ce754.js"
  93. ])
  94. .pipe(concat("elegant.prod.9e9d5ce754.js"))
  95. .pipe(terser())
  96. .pipe(dest("static/js/"));
  97. };
  98. const compileCSS = () => {
  99. const plugins = [
  100. // postcssPresetEnv comes with autoprefixer
  101. postcssPresetEnv({ stage: 1 }),
  102. magician({}),
  103. rfs(),
  104. cssnano()
  105. ];
  106. return src([
  107. "static/applause-button/applause-button.css",
  108. "static/photoswipe/photoswipe.css",
  109. "static/photoswipe/default-skin/default-skin.css",
  110. "static/css/*.css",
  111. "!static/css/elegant.prod.9e9d5ce754.css"
  112. ])
  113. .pipe(postcss(plugins))
  114. .pipe(concat("elegant.prod.9e9d5ce754.css"))
  115. .pipe(dest("static/css/"));
  116. };
  117. const buildAll = series(
  118. rmProdCSS,
  119. compileBootstrapLess,
  120. compileResponsiveLess,
  121. compileCSS,
  122. minifyJS,
  123. buildContent
  124. );
  125. exports.validate = run("jinja-ninja templates");
  126. exports.js = minifyJS;
  127. exports.css = series(
  128. rmProdCSS,
  129. compileBootstrapLess,
  130. compileResponsiveLess,
  131. compileCSS
  132. );
  133. const build = series(
  134. compileBootstrapLess,
  135. compileResponsiveLess,
  136. compileCSS,
  137. minifyJS,
  138. cleanOutput,
  139. buildContent
  140. );
  141. exports.build = build;
  142. const elegant = series(build, parallel(watchFiles, reload));
  143. exports.elegant = elegant;
  144. exports.default = elegant;