preprocessor.js 873 B

123456789101112131415161718192021222324252627282930313233343536
  1. // loader for pre-processing templates with e.g. pug
  2. const cons = require('consolidate')
  3. const loaderUtils = require('loader-utils')
  4. module.exports = function (content) {
  5. this.cacheable && this.cacheable()
  6. const callback = this.async()
  7. const opt = loaderUtils.getOptions(this) || {}
  8. if (!cons[opt.engine]) {
  9. return callback(
  10. new Error(
  11. "Template engine '" +
  12. opt.engine +
  13. "' " +
  14. "isn't available in Consolidate.js"
  15. )
  16. )
  17. }
  18. // allow passing options to the template preprocessor via `template` option
  19. if (this.options.__vueOptions__) {
  20. Object.assign(opt, this.options.__vueOptions__.template)
  21. }
  22. // for relative includes
  23. opt.filename = this.resourcePath
  24. cons[opt.engine].render(content, opt, (err, html) => {
  25. if (err) {
  26. return callback(err)
  27. }
  28. callback(null, html)
  29. })
  30. }