|
|
@@ -1,10 +1,22 @@
|
|
|
-import { genDirectives, genHandlers } from './directives/index'
|
|
|
-import { isReservedTag } from '../../runtime/util/dom'
|
|
|
+import { genHandlers } from './events'
|
|
|
+import { ref } from './directives/ref'
|
|
|
|
|
|
+const baseDirectives = {
|
|
|
+ ref,
|
|
|
+ cloak: function () {} // noop
|
|
|
+}
|
|
|
+
|
|
|
+// platform-injected utils
|
|
|
+let platformDirectives
|
|
|
+let isPlatformReservedTag
|
|
|
+
|
|
|
+// reset on each call
|
|
|
let staticRenderFns
|
|
|
|
|
|
-export function generate (ast) {
|
|
|
+export function generate (ast, options) {
|
|
|
staticRenderFns = []
|
|
|
+ platformDirectives = options.directives || {}
|
|
|
+ isPlatformReservedTag = options.isReservedTag || (() => false)
|
|
|
const code = ast ? genElement(ast) : '__h__("div")'
|
|
|
return {
|
|
|
render: `with (this) { return ${code}}`,
|
|
|
@@ -28,7 +40,7 @@ function genElement (el) {
|
|
|
} else {
|
|
|
// if the element is potentially a component,
|
|
|
// wrap its children as a thunk.
|
|
|
- const children = genChildren(el, !isReservedTag(el.tag) /* asThunk */)
|
|
|
+ const children = genChildren(el, !isPlatformReservedTag(el.tag) /* asThunk */)
|
|
|
const code = `__h__('${el.tag}', ${genData(el)}, ${children}, '${el.ns || ''}')`
|
|
|
if (el.staticRoot) {
|
|
|
// hoist static sub-trees out
|
|
|
@@ -135,6 +147,36 @@ function genData (el) {
|
|
|
return data.replace(/,$/, '') + '}'
|
|
|
}
|
|
|
|
|
|
+function genDirectives (el) {
|
|
|
+ const dirs = el.directives
|
|
|
+ let res = 'directives:['
|
|
|
+ let hasRuntime = false
|
|
|
+ let i, l, dir, needRuntime
|
|
|
+ for (i = 0, l = dirs.length; i < l; i++) {
|
|
|
+ dir = dirs[i]
|
|
|
+ needRuntime = true
|
|
|
+ let gen = platformDirectives[dir.name] || baseDirectives[dir.name]
|
|
|
+ if (gen) {
|
|
|
+ // compile-time directive that manipulates AST.
|
|
|
+ // returns true if it also needs a runtime counterpart.
|
|
|
+ needRuntime = !!gen(el, dir)
|
|
|
+ }
|
|
|
+ if (needRuntime) {
|
|
|
+ hasRuntime = true
|
|
|
+ res += `{def:__resolveDirective__("${dir.name}")${
|
|
|
+ dir.value ? `,value:(${dir.value})` : ''
|
|
|
+ }${
|
|
|
+ dir.arg ? `,arg:"${dir.arg}"` : ''
|
|
|
+ }${
|
|
|
+ dir.modifiers ? `,modifiers:${JSON.stringify(dir.modifiers)}` : ''
|
|
|
+ }},`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (hasRuntime) {
|
|
|
+ return res.slice(0, -1) + ']'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
function genChildren (el, asThunk) {
|
|
|
if (!el.children.length) {
|
|
|
return 'undefined'
|