/
templateTransformAssetUrl.spec.ts
166 lines (147 loc) 路 4.68 KB
/
templateTransformAssetUrl.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
generate,
baseParse,
transform,
TransformOptions
} from '@vue/compiler-core'
import {
transformAssetUrl,
createAssetUrlTransformWithOptions,
AssetURLOptions,
normalizeOptions
} from '../src/templateTransformAssetUrl'
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
import { transformBind } from '../../compiler-core/src/transforms/vBind'
import { stringifyStatic } from '../../compiler-dom/src/transforms/stringifyStatic'
function compileWithAssetUrls(
template: string,
options?: AssetURLOptions,
transformOptions?: TransformOptions
) {
const ast = baseParse(template)
const t = options
? createAssetUrlTransformWithOptions(normalizeOptions(options))
: transformAssetUrl
transform(ast, {
nodeTransforms: [t, transformElement],
directiveTransforms: {
bind: transformBind
},
...transformOptions
})
return generate(ast, { mode: 'module' })
}
describe('compiler sfc: transform asset url', () => {
test('transform assetUrls', () => {
const result = compileWithAssetUrls(`
<img src="./logo.png"/>
<img src="~fixtures/logo.png"/>
<img src="~/fixtures/logo.png"/>
<img src="http://example.com/fixtures/logo.png"/>
<img src="//example.com/fixtures/logo.png"/>
<img src="/fixtures/logo.png"/>
<img src="data:image/png;base64,i"/>
`)
expect(result.code).toMatchSnapshot()
})
/**
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
*/
test('support uri fragment', () => {
const result = compileWithAssetUrls(
'<use href="~@svg/file.svg#fragment"></use>' +
'<use href="~@svg/file.svg#fragment"></use>'
)
expect(result.code).toMatchSnapshot()
})
/**
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
*/
test('support uri is empty', () => {
const result = compileWithAssetUrls('<use href="~"></use>')
expect(result.code).toMatchSnapshot()
})
test('with explicit base', () => {
const { code } = compileWithAssetUrls(
`<img src="./bar.png"></img>` + // -> /foo/bar.png
`<img src="bar.png"></img>` + // -> bar.png (untouched)
`<img src="~bar.png"></img>` + // -> still converts to import
`<img src="@theme/bar.png"></img>`, // -> still converts to import
{
base: '/foo'
}
)
expect(code).toMatch(`import _imports_0 from 'bar.png'`)
expect(code).toMatch(`import _imports_1 from '@theme/bar.png'`)
expect(code).toMatchSnapshot()
})
test('with includeAbsolute: true', () => {
const { code } = compileWithAssetUrls(
`<img src="./bar.png"/>` +
`<img src="/bar.png"/>` +
`<img src="https://foo.bar/baz.png"/>` +
`<img src="//foo.bar/baz.png"/>`,
{
includeAbsolute: true
}
)
expect(code).toMatchSnapshot()
})
// vitejs/vite#298
test('should not transform hash fragments', () => {
const { code } = compileWithAssetUrls(
`<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<circle id="myCircle" cx="0" cy="0" r="5" />
</defs>
<use x="5" y="5" xlink:href="#myCircle" />
</svg>`
)
// should not remove it
expect(code).toMatch(`"xlink:href": "#myCircle"`)
})
test('should allow for full base URLs, with paths', () => {
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
base: 'http://localhost:3000/src/'
})
expect(code).toMatchSnapshot()
})
test('should allow for full base URLs, without paths', () => {
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
base: 'http://localhost:3000'
})
expect(code).toMatchSnapshot()
})
test('should allow for full base URLs, without port', () => {
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
base: 'http://localhost'
})
expect(code).toMatchSnapshot()
})
test('should allow for full base URLs, without protocol', () => {
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
base: '//localhost'
})
expect(code).toMatchSnapshot()
})
test('transform with stringify', () => {
const { code } = compileWithAssetUrls(
`<div>` +
`<img src="./bar.png"/>` +
`<img src="/bar.png"/>` +
`<img src="https://foo.bar/baz.png"/>` +
`<img src="//foo.bar/baz.png"/>` +
`<img src="./bar.png"/>` +
`</div>`,
{
includeAbsolute: true
},
{
hoistStatic: true,
transformHoist: stringifyStatic
}
)
expect(code).toMatch(`_createStaticVNode`)
expect(code).toMatchSnapshot()
})
})