index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // We listen for hexo changes on *.es6 extensions.
  2. // We fire our own build-examples.js and tell it which example to build -
  3. // that script then writes temporary js files
  4. // which we return via the callback.
  5. const { exec } = require('child_process')
  6. const path = require('path')
  7. const fs = require('fs')
  8. const uuid = require('uuid')
  9. const webRoot = path.dirname(path.dirname(__dirname))
  10. const browserifyScript = `${webRoot}/build-examples.js`
  11. function parseExamplesBrowserify (data, options, callback) {
  12. if (!data || !data.path) {
  13. callback(null)
  14. return
  15. }
  16. if (!data.path.match(/\/examples\//)) {
  17. callback(null, data.text)
  18. }
  19. // var slug = data.path.replace(/[^a-zA-Z0-9\_\.]/g, '-')
  20. const slug = uuid.v4()
  21. const tmpFile = `/tmp/${slug}.js`
  22. const cmd = `node ${browserifyScript} ${data.path} ${tmpFile} --colors`
  23. // hexo.log.i('hexo-renderer-uppyexamples: change detected in examples. running: ' + cmd);
  24. exec(cmd, (err, stdout) => {
  25. if (err) {
  26. callback(err)
  27. return
  28. }
  29. hexo.log.i(`hexo-renderer-uppyexamples: ${stdout.trim()}`)
  30. // eslint-disable-next-line no-shadow
  31. fs.readFile(tmpFile, 'utf-8', (err, bundledJS) => {
  32. if (err) {
  33. callback(err)
  34. return
  35. }
  36. // hexo.log.i('hexo-renderer-uppyexamples: read: ' + tmpFile);
  37. // @TODO remove this hack
  38. // once this is resolved: https://github.com/hexojs/hexo/issues/1663
  39. // bundledJS = bundledJS.replace(/</g, ' < ');
  40. // eslint-disable-next-line no-param-reassign
  41. bundledJS = bundledJS.replace(/<(?!=)/g, ' < ')
  42. callback(null, bundledJS)
  43. })
  44. })
  45. }
  46. hexo.extend.renderer.register('es6', 'js', parseExamplesBrowserify)