index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { model } from './model'
  2. import { text } from './text'
  3. import { html } from './html'
  4. import { ref } from './ref'
  5. export { genHandlers } from './on'
  6. export const directives = {
  7. model,
  8. text,
  9. html,
  10. ref,
  11. cloak: function () {} // noop
  12. }
  13. export function genDirectives (el) {
  14. const dirs = el.directives
  15. let res = 'directives:['
  16. let hasRuntime = false
  17. let i, l, dir, needRuntime
  18. for (i = 0, l = dirs.length; i < l; i++) {
  19. dir = dirs[i]
  20. needRuntime = true
  21. let gen = directives[dir.name]
  22. if (gen) {
  23. // compile-time directive that manipulates AST.
  24. // returns true if it also needs a runtime counterpart.
  25. needRuntime = !!gen(el, dir)
  26. }
  27. if (needRuntime) {
  28. hasRuntime = true
  29. res += `{def:__resolveDirective__("${dir.name}")${
  30. dir.value ? `,value:(${dir.value})` : ''
  31. }${
  32. dir.arg ? `,arg:"${dir.arg}"` : ''
  33. }${
  34. dir.modifiers ? `,modifiers:${JSON.stringify(dir.modifiers)}` : ''
  35. }},`
  36. }
  37. }
  38. if (hasRuntime) {
  39. return res.slice(0, -1) + ']'
  40. }
  41. }