gulpfile.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // For using gulp
  2. const gulp = require('gulp');
  3. // For reading template parts
  4. const fs = require("fs");
  5. // For parsing codes, variables, loops etc. inside html templates
  6. const nunjucks = require('gulp-nunjucks');
  7. // For allowing custom loader
  8. const nunj = require('nunjucks');
  9. // For cleaning previous output
  10. //const del = require('del');
  11. const path = require('path');
  12. // Output directory
  13. const source_dir='src';
  14. const template_dir='src/html';
  15. const output_dir='public';
  16. var jsonInfo = JSON.parse(fs.readFileSync('src/info.json', 'utf8'));
  17. if ( jsonInfo.languages ) {
  18. jsonInfo.languages.forEach(function(lang) {
  19. if ( jsonInfo.default_langs.includes(lang.slug) ) {
  20. lang.default = 1
  21. }
  22. });
  23. }
  24. // Loader to include from relative path
  25. // @source https://github.com/mozilla/nunjucks/issues/1214#issuecomment-579534919
  26. class MyCustomLoader {
  27. constructor() {
  28. this.async = true
  29. }
  30. isRelative (filename) {
  31. console.log ('isRelative', filename)
  32. return true
  33. }
  34. resolve(parentName, filename) {
  35. console.log('resolve', parentName, filename)
  36. return filename
  37. }
  38. getSource (filePath, callback) {
  39. //console.log('getSource', filePath)
  40. try {
  41. const src = fs.readFileSync(filePath, 'utf8')
  42. const file = {
  43. src,
  44. path: filePath
  45. }
  46. callback(null, file)
  47. } catch (error) {
  48. callback(error)
  49. }
  50. }
  51. }
  52. // Runs when someone runs "gulp" on this directory
  53. gulp.task('default', async function(){
  54. var nunjucksenv = new nunj.Environment([new nunj.FileSystemLoader('views'),
  55. new MyCustomLoader()]);
  56. gulp.src(template_dir+'/index.html')
  57. .pipe(nunjucks.compile(
  58. {jsoninfo: jsonInfo}
  59. , {env: nunjucksenv}
  60. ))
  61. .pipe(gulp.dest('public'));
  62. });