Skip to content

Commit

Permalink
fix: should work with variable named render (close #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 3, 2018
1 parent 3b19c1e commit 273827b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/compileTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ function actuallyCompile (
// transpile code with vue-template-es2015-compiler, which is a forked
// version of Buble that applies ES2015 transforms + stripping `with` usage
let code = transpile(
`var render = ${toFunction(render)}\n` +
`var staticRenderFns = [${staticRenderFns.map(toFunction)}]`,
`var __render__ = ${toFunction(render)}\n` +
`var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
finalTranspileOptions
) + `\n`

// #23 we use __render__ to avoid `render` not being prefixed by the
// transpiler when stripping with, but revert it back to `render` to
// maintain backwards compat
code = code.replace(/\s__(render|staticRenderFns)__\s/g, ' $1 ')

if (!isProduction) {
// mark with stripped (this enables Vue to use correct runtime proxy
// detection)
Expand Down
20 changes: 20 additions & 0 deletions test/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { parse } from '../lib/parse'
import { compileTemplate } from '../lib/compileTemplate'
import * as compiler from 'vue-template-compiler'

test('should work', () => {
const source = `<div><p>{{ render }}</p></div>`

const result = compileTemplate({
filename: 'example.vue',
source,
compiler: compiler
})

expect(result.errors.length).toBe(0)
expect(result.source).toBe(source)
// should expose render fns
expect(result.code).toMatch(`var render = function`)
expect(result.code).toMatch(`var staticRenderFns = []`)
// should mark with stripped
expect(result.code).toMatch(`render._withStripped = true`)
// should prefix bindings
expect(result.code).toMatch(`_vm.render`)
})

test('preprocess pug', () => {
const template = parse({
source:
Expand Down

0 comments on commit 273827b

Please sign in to comment.