2020-11-21 03:22:51 +08:00
|
|
|
import {
|
|
|
|
compileTemplate,
|
|
|
|
SFCTemplateCompileOptions
|
|
|
|
} from '../src/compileTemplate'
|
2019-12-11 01:01:56 +08:00
|
|
|
import { parse, SFCTemplateBlock } from '../src/parse'
|
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
function compile(opts: Omit<SFCTemplateCompileOptions, 'id'>) {
|
|
|
|
return compileTemplate({
|
|
|
|
...opts,
|
|
|
|
id: ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-11 01:01:56 +08:00
|
|
|
test('should work', () => {
|
|
|
|
const source = `<div><p>{{ render }}</p></div>`
|
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({ filename: 'example.vue', source })
|
2019-12-11 01:01:56 +08:00
|
|
|
|
|
|
|
expect(result.errors.length).toBe(0)
|
|
|
|
expect(result.source).toBe(source)
|
|
|
|
// should expose render fn
|
2020-02-11 06:29:12 +08:00
|
|
|
expect(result.code).toMatch(`export function render(`)
|
2019-12-11 01:01:56 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('preprocess pug', () => {
|
|
|
|
const template = parse(
|
|
|
|
`
|
|
|
|
<template lang="pug">
|
|
|
|
body
|
|
|
|
h1 Pug Examples
|
|
|
|
div.container
|
|
|
|
p Cool Pug example!
|
|
|
|
</template>
|
|
|
|
`,
|
2019-12-11 06:41:56 +08:00
|
|
|
{ filename: 'example.vue', sourceMap: true }
|
2019-12-23 08:44:21 +08:00
|
|
|
).descriptor.template as SFCTemplateBlock
|
2019-12-11 01:01:56 +08:00
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({
|
2019-12-11 01:01:56 +08:00
|
|
|
filename: 'example.vue',
|
|
|
|
source: template.content,
|
2019-12-11 11:23:55 +08:00
|
|
|
preprocessLang: template.lang
|
2019-12-11 01:01:56 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
expect(result.errors.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('warn missing preprocessor', () => {
|
2019-12-23 08:44:21 +08:00
|
|
|
const template = parse(`<template lang="unknownLang">hi</template>\n`, {
|
2019-12-11 01:01:56 +08:00
|
|
|
filename: 'example.vue',
|
2019-12-11 06:41:56 +08:00
|
|
|
sourceMap: true
|
2019-12-23 08:44:21 +08:00
|
|
|
}).descriptor.template as SFCTemplateBlock
|
2019-12-11 01:01:56 +08:00
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({
|
2019-12-11 01:01:56 +08:00
|
|
|
filename: 'example.vue',
|
|
|
|
source: template.content,
|
|
|
|
preprocessLang: template.lang
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result.errors.length).toBe(1)
|
|
|
|
})
|
2019-12-11 22:26:14 +08:00
|
|
|
|
|
|
|
test('transform asset url options', () => {
|
2020-01-20 22:57:17 +08:00
|
|
|
const input = { source: `<foo bar="~baz"/>`, filename: 'example.vue' }
|
2019-12-11 22:26:14 +08:00
|
|
|
// Object option
|
2020-11-21 03:22:51 +08:00
|
|
|
const { code: code1 } = compile({
|
2019-12-11 22:26:14 +08:00
|
|
|
...input,
|
2020-05-05 04:45:19 +08:00
|
|
|
transformAssetUrls: {
|
|
|
|
tags: { foo: ['bar'] }
|
|
|
|
}
|
2019-12-11 22:26:14 +08:00
|
|
|
})
|
|
|
|
expect(code1).toMatch(`import _imports_0 from 'baz'\n`)
|
2020-05-05 04:45:19 +08:00
|
|
|
|
|
|
|
// legacy object option (direct tags config)
|
2020-11-21 03:22:51 +08:00
|
|
|
const { code: code2 } = compile({
|
2020-05-05 04:45:19 +08:00
|
|
|
...input,
|
|
|
|
transformAssetUrls: {
|
|
|
|
foo: ['bar']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(code2).toMatch(`import _imports_0 from 'baz'\n`)
|
|
|
|
|
|
|
|
// false option
|
2020-11-21 03:22:51 +08:00
|
|
|
const { code: code3 } = compile({
|
2019-12-11 22:26:14 +08:00
|
|
|
...input,
|
|
|
|
transformAssetUrls: false
|
|
|
|
})
|
2020-05-05 04:45:19 +08:00
|
|
|
expect(code3).not.toMatch(`import _imports_0 from 'baz'\n`)
|
2019-12-11 22:26:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('source map', () => {
|
|
|
|
const template = parse(
|
|
|
|
`
|
|
|
|
<template>
|
|
|
|
<div><p>{{ render }}</p></div>
|
2019-12-23 08:44:21 +08:00
|
|
|
</template>
|
2019-12-11 22:26:14 +08:00
|
|
|
`,
|
|
|
|
{ filename: 'example.vue', sourceMap: true }
|
2019-12-23 08:44:21 +08:00
|
|
|
).descriptor.template as SFCTemplateBlock
|
2019-12-11 22:26:14 +08:00
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({
|
2019-12-11 22:26:14 +08:00
|
|
|
filename: 'example.vue',
|
|
|
|
source: template.content
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result.map).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('template errors', () => {
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({
|
2019-12-11 22:26:14 +08:00
|
|
|
filename: 'example.vue',
|
2019-12-23 08:44:21 +08:00
|
|
|
source: `<div :foo
|
2019-12-11 22:26:14 +08:00
|
|
|
:bar="a[" v-model="baz"/>`
|
|
|
|
})
|
|
|
|
expect(result.errors).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('preprocessor errors', () => {
|
|
|
|
const template = parse(
|
|
|
|
`
|
|
|
|
<template lang="pug">
|
|
|
|
div(class='class)
|
|
|
|
</template>
|
|
|
|
`,
|
|
|
|
{ filename: 'example.vue', sourceMap: true }
|
2019-12-23 08:44:21 +08:00
|
|
|
).descriptor.template as SFCTemplateBlock
|
2019-12-11 22:26:14 +08:00
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
const result = compile({
|
2019-12-11 22:26:14 +08:00
|
|
|
filename: 'example.vue',
|
|
|
|
source: template.content,
|
|
|
|
preprocessLang: template.lang
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result.errors.length).toBe(1)
|
|
|
|
const message = result.errors[0].toString()
|
|
|
|
expect(message).toMatch(`Error: example.vue:3:1`)
|
|
|
|
expect(message).toMatch(
|
|
|
|
`The end of the string reached with no closing bracket ) found.`
|
|
|
|
)
|
|
|
|
})
|
2021-03-25 23:10:25 +08:00
|
|
|
|
|
|
|
// #3447
|
|
|
|
test('should generate the correct imports expression', () => {
|
|
|
|
const { code } = compile({
|
|
|
|
filename: 'example.vue',
|
|
|
|
source: `
|
|
|
|
<img src="./foo.svg"/>
|
|
|
|
<Comp>
|
|
|
|
<img src="./bar.svg"/>
|
|
|
|
</Comp>
|
|
|
|
`,
|
|
|
|
ssr: true
|
|
|
|
})
|
|
|
|
expect(code).toMatch(`_ssrRenderAttr(\"src\", _imports_1)`)
|
|
|
|
expect(code).toMatch(`_createVNode(\"img\", { src: _imports_1 })`)
|
|
|
|
})
|