Skip to content

Commit

Permalink
rename _h -> _c so that vue-template-es2015-compiler can use the new …
Browse files Browse the repository at this point in the history
…internal createElement without breaking backwards compatibility
  • Loading branch information
yyx990803 committed Dec 5, 2016
1 parent 207c18c commit 4b51ad0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ declare interface Component {
_render: () => VNode;
__patch__: (a: Element | VNode | void, b: VNode) => any;
// createElement
_h: (vnode?: VNode, data?: VNodeData, children?: VNodeChildren) => VNode | void;
_c: (vnode?: VNode, data?: VNodeData, children?: VNodeChildren) => VNode | void;
// renderStatic
_m: (index: number, isInFor?: boolean) => VNode | VNodeChildren;
// markOnce
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/codegen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function generate (
transforms = pluckModuleFunction(options.modules, 'transformCode')
dataGenFns = pluckModuleFunction(options.modules, 'genData')
platformDirectives = options.directives || {}
const code = ast ? genElement(ast) : '_h("div")'
const code = ast ? genElement(ast) : '_c("div")'
staticRenderFns = prevStaticRenderFns
onceCount = prevOnceCount
return {
Expand Down Expand Up @@ -62,7 +62,7 @@ function genElement (el: ASTElement): string {
const data = el.plain ? undefined : genData(el)

const children = el.inlineTemplate ? null : genChildren(el, true)
code = `_h('${el.tag}'${
code = `_c('${el.tag}'${
data ? `,${data}` : '' // data
}${
children ? `,${children}` : '' // children
Expand Down Expand Up @@ -336,7 +336,7 @@ function genSlot (el: ASTElement): string {
// componentName is el.component, take it as argument to shun flow's pessimistic refinement
function genComponent (componentName, el): string {
const children = el.inlineTemplate ? null : genChildren(el, true)
return `_h(${componentName},${genData(el)}${
return `_c(${componentName},${genData(el)}${
children ? `,${children}` : ''
})`
}
Expand Down
8 changes: 5 additions & 3 deletions src/core/instance/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export function initRender (vm: Component) {
vm.$scopedSlots = {}
// bind the createElement fn to this instance
// so that we get proper render context inside it.
// args order: tag, data, children, needNormalization
// the needNormalization flag is disabled for the public version.
vm._h = (a, b, c, d) => createElement(vm, a, b, c, d, false)
// args order: tag, data, children, needNormalization, alwaysNormalize
// internal version is used by render functions compiled from templates
vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)
// normalization is always applied for the public version, used in
// user-written render functions.
vm.$createElement = (a, b, c, d) => createElement(vm, a, b, c, d, true)
if (vm.$options.el) {
vm.$mount(vm.$options.el)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/modules/compiler/compiler-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('compile options', () => {
result[validator.name] = null
})
// generate code
return `_h('validate',{props:{
return `_c('validate',{props:{
field:${JSON.stringify(el.validate.field)},
groups:${JSON.stringify(el.validate.groups)},
validators:${JSON.stringify(el.validators)},
Expand Down

4 comments on commit 4b51ad0

@RDelorier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyx990803 think this may have been a breaking change for packages. PeachScript/vue-infinite-loading#29 (comment) I'm also seeing the same error for other packages after upgrading.

@yyx990803
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unfortunately components that ship pre-compiled dist files need to re-compile with latest version of vue-template-compiler...

@gurghet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you ship a component without a pre-compiled dist file if you want to give the user the possibility to install it just with a <script> tag?

@yyx990803
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gurghet pre-compiled components can also be used with just a <script> tag. But if you really don't want to pre-compile, just keep the raw template string and let the user compile on the fly (this forces the user to use the standalone build).

Still, this type of internal compilation changes are rare - we will also definitely be more careful to avoid such changes in the future.

Please sign in to comment.