html.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import config from '../config';
  2. import path from 'path';
  3. import posthtml from 'posthtml';
  4. import expressions from 'posthtml-expressions';
  5. import fs from 'fs';
  6. const target = config.currentTarget;
  7. const mode = config.currentMode;
  8. const targetInfix = `${config.targets[target].delimiter}${config.targets[target].infix}`;
  9. const modeInfix = `${config.modes[mode].delimiter}${config.modes[mode].infix}`;
  10. /**
  11. * Generate HTML file from template with references to javascript and css files
  12. * based on target (development / production) and mode (online / offline)
  13. *
  14. * All configuration is contained in config.js
  15. *
  16. * @param cb: callback handler passed by gulp, signals task completion when called
  17. */
  18. export const html = (cb) => {
  19. const file = fs.readFileSync(
  20. path.join(
  21. config.paths.src,
  22. config.paths.html,
  23. `${config.fileNames.html}.html`
  24. ), 'utf8'
  25. );
  26. posthtml(
  27. expressions({
  28. locals: {
  29. javascript: path.join(
  30. config.paths.assets,
  31. config.paths.js,
  32. `${config.fileNames.js}${modeInfix}${targetInfix}.js`
  33. ),
  34. stylesheet: path.join(
  35. config.paths.assets,
  36. config.paths.css,
  37. `${config.fileNames.css}${targetInfix}.css`
  38. )
  39. }
  40. })
  41. ).process(file)
  42. .then((result) => {
  43. fs.writeFileSync(
  44. path.join(
  45. config.paths.dest,
  46. `${config.fileNames.html}.html`
  47. ), result.html
  48. );
  49. });
  50. cb();
  51. };