Skip to content

Commit 273827b

Browse files
committed
fix: should work with variable named render (close #23)
1 parent 3b19c1e commit 273827b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/compileTemplate.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,16 @@ function actuallyCompile (
149149
// transpile code with vue-template-es2015-compiler, which is a forked
150150
// version of Buble that applies ES2015 transforms + stripping `with` usage
151151
let code = transpile(
152-
`var render = ${toFunction(render)}\n` +
153-
`var staticRenderFns = [${staticRenderFns.map(toFunction)}]`,
152+
`var __render__ = ${toFunction(render)}\n` +
153+
`var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
154154
finalTranspileOptions
155155
) + `\n`
156156

157+
// #23 we use __render__ to avoid `render` not being prefixed by the
158+
// transpiler when stripping with, but revert it back to `render` to
159+
// maintain backwards compat
160+
code = code.replace(/\s__(render|staticRenderFns)__\s/g, ' $1 ')
161+
157162
if (!isProduction) {
158163
// mark with stripped (this enables Vue to use correct runtime proxy
159164
// detection)

test/compileTemplate.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import { parse } from '../lib/parse'
22
import { compileTemplate } from '../lib/compileTemplate'
33
import * as compiler from 'vue-template-compiler'
44

5+
test('should work', () => {
6+
const source = `<div><p>{{ render }}</p></div>`
7+
8+
const result = compileTemplate({
9+
filename: 'example.vue',
10+
source,
11+
compiler: compiler
12+
})
13+
14+
expect(result.errors.length).toBe(0)
15+
expect(result.source).toBe(source)
16+
// should expose render fns
17+
expect(result.code).toMatch(`var render = function`)
18+
expect(result.code).toMatch(`var staticRenderFns = []`)
19+
// should mark with stripped
20+
expect(result.code).toMatch(`render._withStripped = true`)
21+
// should prefix bindings
22+
expect(result.code).toMatch(`_vm.render`)
23+
})
24+
525
test('preprocess pug', () => {
626
const template = parse({
727
source:

0 commit comments

Comments
 (0)