Skip to content

Commit 89c66a3

Browse files
committed
add checkCompileToFunctions
1 parent 041c17a commit 89c66a3

File tree

9 files changed

+107
-28
lines changed

9 files changed

+107
-28
lines changed

Diff for: packages/create-instance/create-render-slot.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function createVNodes (
1010
slotValue: Component | string
1111
): ?Array<VNode> {
1212
if (typeof slotValue === 'string') {
13+
// Since compileToFunctions is checked in createSlotVNodes(),
14+
// it is not necessary to check compileToFunctions.
1315
const compiledResult = compileToFunctions(`<div>${slotValue}</div>`)
1416
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
1517
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns

Diff for: packages/create-instance/create-scoped-slots.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Vue from 'vue'
44
import { compileToFunctions } from 'vue-template-compiler'
55
import { throwError, vueVersion } from 'shared/util'
6+
import { checkCompileToFunctions } from 'shared/validators'
67

78
function isDestructuringSlotScope (slotScope: string): boolean {
89
return slotScope[0] === '{' && slotScope[slotScope.length - 1] === '}'
@@ -36,6 +37,7 @@ function getVueTemplateCompilerHelpers (): { [name: string]: Function } {
3637
}
3738

3839
function validateEnvironment (): void {
40+
checkCompileToFunctions()
3941
if (window.navigator.userAgent.match(/PhantomJS/i)) {
4042
throwError(
4143
`the scopedSlots option does not support PhantomJS. ` +

Diff for: packages/create-instance/create-slot-vnodes.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import { compileToFunctions } from 'vue-template-compiler'
4+
import { checkCompileToFunctions } from 'shared/validators'
45

56
function startsWithTag (str: SlotValue): boolean {
67
return typeof str === 'string' && str.trim()[0] === '<'
@@ -15,6 +16,7 @@ function createVNodesForSlot (
1516
return slotValue
1617
}
1718

19+
checkCompileToFunctions()
1820
const el =
1921
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue
2022

Diff for: packages/create-instance/validate-slots.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @flow
22

33
import { throwError } from 'shared/util'
4-
import { compileToFunctions } from 'vue-template-compiler'
5-
import { isVueComponent } from '../shared/validators'
4+
import {
5+
checkCompileToFunctions,
6+
isVueComponent
7+
} from 'shared/validators'
68

79
function isValidSlot (slot: any): boolean {
810
return (
@@ -11,16 +13,6 @@ function isValidSlot (slot: any): boolean {
1113
)
1214
}
1315

14-
function requiresTemplateCompiler (slot: any): void {
15-
if (typeof slot === 'string' && !compileToFunctions) {
16-
throwError(
17-
`vueTemplateCompiler is undefined, you must pass ` +
18-
`precompiled components if vue-template-compiler is ` +
19-
`undefined`
20-
)
21-
}
22-
}
23-
2416
export function validateSlots (slots: SlotsObject): void {
2517
Object.keys(slots).forEach(key => {
2618
const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]]
@@ -32,7 +24,9 @@ export function validateSlots (slots: SlotsObject): void {
3224
`of Components`
3325
)
3426
}
35-
requiresTemplateCompiler(slotValue)
27+
if (typeof slotValue === 'string') {
28+
checkCompileToFunctions()
29+
}
3630
})
3731
})
3832
}

Diff for: packages/shared/compile-template.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// @flow
22

33
import { compileToFunctions } from 'vue-template-compiler'
4+
import { checkCompileToFunctions } from './validators'
45

56
export function compileTemplate (component: Component): void {
67
if (component.template) {
8+
checkCompileToFunctions()
79
Object.assign(component, compileToFunctions(component.template))
810
}
911

Diff for: packages/shared/stub-components.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
hyphenate
1010
} from './util'
1111
import {
12+
checkCompileToFunctions,
1213
componentNeedsCompiling,
1314
templateContainsComponent,
1415
isVueComponent
@@ -66,13 +67,7 @@ function createStubFromString (
6667
originalComponent: Component,
6768
name: string
6869
): Component {
69-
if (!compileToFunctions) {
70-
throwError(
71-
`vueTemplateCompiler is undefined, you must pass ` +
72-
`precompiled components if vue-template-compiler is ` +
73-
`undefined`
74-
)
75-
}
70+
checkCompileToFunctions()
7671

7772
if (templateContainsComponent(templateString, name)) {
7873
throwError('options.stub cannot contain a circular reference')
@@ -176,13 +171,7 @@ export function createComponentStubs (
176171
}
177172
} else {
178173
if (typeof stub === 'string') {
179-
if (!compileToFunctions) {
180-
throwError(
181-
`vueTemplateCompiler is undefined, you must pass ` +
182-
`precompiled components if vue-template-compiler is ` +
183-
`undefined`
184-
)
185-
}
174+
checkCompileToFunctions()
186175
components[stubName] = {
187176
...compileToFunctions(stub)
188177
}

Diff for: packages/shared/validators.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// @flow
2+
import { compileToFunctions } from 'vue-template-compiler'
23
import { throwError, capitalize, camelize, hyphenate } from './util'
34

5+
export function checkCompileToFunctions (): void {
6+
if (!compileToFunctions) {
7+
throwError(
8+
`vueTemplateCompiler is undefined, you must pass ` +
9+
`precompiled components if vue-template-compiler is ` +
10+
`undefined`
11+
)
12+
}
13+
}
14+
415
export function isDomSelector (selector: any): boolean {
516
if (typeof selector !== 'string') {
617
return false

Diff for: test/specs/mounting-options/scopedSlots.spec.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isRunningPhantomJS
55
} from '~resources/utils'
66
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
7-
import { itDoNotRunIf } from 'conditional-specs'
7+
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'
88

99
describeWithShallowAndMount('scopedSlots', mountingMethod => {
1010
const windowSave = window
@@ -165,4 +165,43 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
165165
.with.property('message', message)
166166
}
167167
)
168+
169+
itSkipIf(
170+
mountingMethod.name === 'renderToString',
171+
'throws error if passed string in default slot object and vue-template-compiler is undefined',
172+
() => {
173+
const compilerSave =
174+
require.cache[require.resolve('vue-template-compiler')].exports
175+
.compileToFunctions
176+
require.cache[
177+
require.resolve('vue-template-compiler')
178+
].exports.compileToFunctions = undefined
179+
delete require.cache[require.resolve('../../../packages/test-utils')]
180+
const mountingMethodFresh = require('../../../packages/test-utils')[
181+
mountingMethod.name
182+
]
183+
const message =
184+
'[vue-test-utils]: vueTemplateCompiler is undefined, you must pass precompiled components if vue-template-compiler is undefined'
185+
const fn = () => {
186+
mountingMethodFresh(ComponentWithScopedSlots, {
187+
scopedSlots: {
188+
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
189+
}
190+
})
191+
}
192+
try {
193+
expect(fn)
194+
.to.throw()
195+
.with.property('message', message)
196+
} catch (err) {
197+
require.cache[
198+
require.resolve('vue-template-compiler')
199+
].exports.compileToFunctions = compilerSave
200+
throw err
201+
}
202+
require.cache[
203+
require.resolve('vue-template-compiler')
204+
].exports.compileToFunctions = compilerSave
205+
}
206+
)
168207
})

Diff for: test/specs/wrapper.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describeWithShallowAndMount } from '~resources/utils'
2+
import { itSkipIf } from 'conditional-specs'
23

34
describeWithShallowAndMount('Wrapper', mountingMethod => {
45
['vnode', 'element', 'vm', 'options'].forEach(property => {
@@ -12,4 +13,41 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
1213
.with.property('message', message)
1314
})
1415
})
16+
17+
itSkipIf(
18+
mountingMethod.name === 'renderToString',
19+
'throws error if passed string in default slot array when vue-template-compiler is undefined',
20+
() => {
21+
const compilerSave =
22+
require.cache[require.resolve('vue-template-compiler')].exports
23+
.compileToFunctions
24+
require.cache[require.resolve('vue-template-compiler')].exports = {
25+
compileToFunctions: undefined
26+
}
27+
delete require.cache[require.resolve('../../packages/test-utils')]
28+
const mountingMethodFresh = require('../../packages/test-utils')[
29+
mountingMethod.name
30+
]
31+
const message =
32+
'[vue-test-utils]: vueTemplateCompiler is undefined, you must pass precompiled components if vue-template-compiler is undefined'
33+
const fn = () => {
34+
mountingMethodFresh({
35+
template: '<div />'
36+
})
37+
}
38+
try {
39+
expect(fn)
40+
.to.throw()
41+
.with.property('message', message)
42+
} catch (err) {
43+
require.cache[
44+
require.resolve('vue-template-compiler')
45+
].exports.compileToFunctions = compilerSave
46+
throw err
47+
}
48+
require.cache[
49+
require.resolve('vue-template-compiler')
50+
].exports.compileToFunctions = compilerSave
51+
}
52+
)
1553
})

0 commit comments

Comments
 (0)