customBlocks.js 819 B

123456789101112131415161718192021
  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. }