Veikedo

jade-static-loader

Jun 21st, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const jade = require('jade')
  2. const Joi = require('joi')
  3.  
  4. module.exports = function (source) {
  5.   this.cacheable && this.cacheable(true)
  6.  
  7.   const _opts = (this.options ? this.options.jade : {}) || {}
  8.   const schema = Joi.object().keys({
  9.     filename: Joi.string().default(this.resourcePath),
  10.     pretty: Joi.boolean().default(true),
  11.     doctype: Joi.string(),
  12.     self: Joi.boolean(),
  13.     debug: Joi.boolean(),
  14.     compileDebug: Joi.boolean(),
  15.     cache: Joi.boolean(),
  16.     compiler: Joi.func(),
  17.     parser: Joi.func(),
  18.     globals: Joi.array().single(),
  19.     locals: Joi.object(),
  20.     basedir: Joi.string()
  21.   })
  22.  
  23.   // make original source available for plugins
  24.   this._module._src = source
  25.  
  26.   // validate options
  27.   const validated = Joi.validate(_opts, schema)
  28.   if (validated.error) { throw validated.error }
  29.   const opts = validated.value
  30.  
  31.   // compile the template to a function
  32.   const tpl = jade.compile(source, opts)
  33.  
  34.   // add all dependencies to webpack
  35.   tpl.dependencies.map(this.addDependency.bind(this))
  36.  
  37.   // render template
  38.   const rendered = tpl(opts.locals)
  39.  
  40.   // stringify before returning so it's valid js for webpack
  41.   return 'module.exports = ' + JSON.stringify(rendered)
  42. }
Advertisement
Add Comment
Please, Sign In to add comment