Skip to content

Commit 2d02e1c

Browse files
committed
expose render function to vue modules
in case people want to do some processing on the VNode tree created by <template>, you could have a render function defined on the Vue options object like: const vnode = render.apply(this, arguments); if (vnode.children.find(v => v.tag === 'input')) { vnode.class += ' has-an-input-child'; }
1 parent c62985f commit 2d02e1c

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

transform.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ function removeSourceMap(ast) {
1212
});
1313
}
1414

15-
function getRenderFunctionExpression(ast) {
16-
return ast.program.body[0].declarations[0].init;
17-
}
18-
19-
function getStaticRenderFunctionExpressions(ast) {
20-
return ast.program.body[1].declarations[0].init;
21-
}
22-
2315
const {
2416
ArrayExpression,
2517
ObjectProperty,
@@ -29,10 +21,21 @@ const {
2921
ExpressionStatement,
3022
AssignmentExpression,
3123
MemberExpression,
32-
ExportDefaultDeclaration
24+
ExportDefaultDeclaration,
25+
LogicalExpression,
26+
FunctionDeclaration
3327
} = babel.types;
3428

35-
const getPlugin = (renderFunctionExpr, staticRenderArrayExpr, isFunctional) => {
29+
function getRenderFunctionDeclaration(ast) {
30+
const expr = ast.program.body[0].declarations[0].init;
31+
return FunctionDeclaration(Identifier('render'), expr.params, expr.body);
32+
}
33+
34+
function getStaticRenderFunctionExpressions(ast) {
35+
return ast.program.body[1].declarations[0].init;
36+
}
37+
38+
const getPlugin = (renderFunctionDeclr, staticRenderArrayExpr, isFunctional) => {
3639
return function AddFunctionPlugin() {
3740
return {
3841
visitor: {
@@ -47,13 +50,17 @@ const getPlugin = (renderFunctionExpr, staticRenderArrayExpr, isFunctional) => {
4750
ExportDefaultDeclaration(Identifier('__export__'))
4851
];
4952

50-
if (renderFunctionExpr) {
53+
if (renderFunctionDeclr) {
5154
statements.splice(1, 0, ExpressionStatement(
5255
AssignmentExpression('=',
5356
MemberExpression(Identifier('__export__'), Identifier('render')),
54-
renderFunctionExpr
57+
LogicalExpression('||',
58+
MemberExpression(Identifier('__export__'), Identifier('render')),
59+
Identifier('render')
60+
)
5561
)
5662
));
63+
statements.splice(1, 0, renderFunctionDeclr);
5764
}
5865

5966
if (staticRenderArrayExpr) {
@@ -102,16 +109,16 @@ module.exports = function (vueSource, vueFilename, extraPlugins) {
102109
compilerOptions: {outputSourceRange: true}
103110
}) : {};
104111

105-
let renderFunctionExpr, staticRenderArrayExpr;
112+
let renderFunctionDeclr, staticRenderArrayExpr;
106113

107114
if (code) {
108115
const ast = babel.parse(code);
109116
removeSourceMap(ast);
110-
renderFunctionExpr = getRenderFunctionExpression(ast);
117+
renderFunctionDeclr = getRenderFunctionDeclaration(ast);
111118
staticRenderArrayExpr = getStaticRenderFunctionExpressions(ast);
112119
}
113120

114-
plugins.unshift(getPlugin(renderFunctionExpr, staticRenderArrayExpr, template && template.attrs.functional));
121+
plugins.unshift(getPlugin(renderFunctionDeclr, staticRenderArrayExpr, template && template.attrs.functional));
115122

116123
const ast = babel.transformSync(script ? script.content : 'export default {};', {plugins, ast: true}).ast;
117124

0 commit comments

Comments
 (0)