You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
819 B

4 years ago
  1. const qs = require('querystring')
  2. const { attrsToQuery } = require('./utils')
  3. module.exports = function genCustomBlocksCode (
  4. blocks,
  5. resourcePath,
  6. resourceQuery,
  7. stringifyRequest
  8. ) {
  9. return `\n/* custom blocks */\n` + blocks.map((block, i) => {
  10. const src = block.attrs.src || resourcePath
  11. const attrsQuery = attrsToQuery(block.attrs)
  12. const issuerQuery = block.attrs.src ? `&issuerPath=${qs.escape(resourcePath)}` : ''
  13. const inheritQuery = resourceQuery ? `&${resourceQuery.slice(1)}` : ''
  14. const query = `?vue&type=custom&index=${i}&blockType=${qs.escape(block.type)}${issuerQuery}${attrsQuery}${inheritQuery}`
  15. return (
  16. `import block${i} from ${stringifyRequest(src + query)}\n` +
  17. `if (typeof block${i} === 'function') block${i}(component)`
  18. )
  19. }).join(`\n`) + `\n`
  20. }