forked from vuejs/vue-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
251 lines (232 loc) · 7.96 KB
/
template.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
process.env.VUE_LOADER_TEST = true
const path = require('path')
const { expect } = require('chai')
const {
mockBundleAndRun,
mockRender
} = require('./shared')
const normalizeNewline = require('normalize-newline')
describe('template block features', () => {
it('template with comments', done => {
mockBundleAndRun({
entry: 'template-comment.vue'
}, (window, module, rawModule) => {
expect(module.comments).to.equal(true)
const vnode = mockRender(module, {
msg: 'hi'
})
expect(vnode.tag).to.equal('div')
expect(vnode.children.length).to.equal(2)
expect(vnode.children[0].data.staticClass).to.equal('red')
expect(vnode.children[0].children[0].text).to.equal('hi')
expect(vnode.children[1].isComment).to.true
expect(vnode.children[1].text).to.equal(' comment here ')
done()
})
})
it('transpile ES2015 features in template', done => {
mockBundleAndRun({
entry: 'es2015.vue'
}, (window, module) => {
const vnode = mockRender(module, {
a: 'hello',
b: true
})
// <div :class="{[a]:true}"></div>
expect(vnode.tag).to.equal('div')
expect(vnode.data.class['test-hello']).to.equal(true)
expect(vnode.data.class['b']).to.equal(true)
done()
})
})
it('translates relative URLs and respects resolve alias', done => {
mockBundleAndRun({
entry: 'resolve.vue',
resolve: {
alias: {
fixtures: path.resolve(__dirname, './fixtures')
}
},
module: {
rules: [
{ test: /\.png$/, loader: 'file-loader?name=[name].[hash:6].[ext]' }
]
}
}, (window, module) => {
const vnode = mockRender(module)
// <div>
// <img src="logo.c9e00e.png">
// <img src="logo.c9e00e.png">
// </div>
expect(vnode.children[0].tag).to.equal('img')
expect(vnode.children[0].data.attrs.src).to.equal('logo.c9e00e.png')
expect(vnode.children[2].tag).to.equal('img')
expect(vnode.children[2].data.attrs.src).to.equal('logo.c9e00e.png')
const style = window.document.querySelector('style').textContent
expect(style).to.contain('html { background-image: url(logo.c9e00e.png);\n}')
expect(style).to.contain('body { background-image: url(logo.c9e00e.png);\n}')
done()
})
})
it('transformToRequire option', done => {
mockBundleAndRun({
entry: 'transform.vue',
module: {
rules: [
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader'
}
]
}
}, (window, module) => {
function includeDataURL (s) {
return !!s.match(/\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*/i)
}
const vnode = mockRender(module)
// img tag
expect(includeDataURL(vnode.children[0].data.attrs.src)).to.equal(true)
// image tag (SVG)
expect(includeDataURL(vnode.children[2].children[0].data.attrs['xlink:href'])).to.equal(true)
const style = window.document.querySelector('style').textContent
const dataURL = vnode.children[0].data.attrs.src
// image tag with srcset
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL)
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + ' 2x')
// image tag with multiline srcset
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + ', ' + dataURL + ' 2x')
expect(vnode.children[10].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL)
expect(vnode.children[12].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[14].data.attrs.srcset).to.equal(dataURL + ', ' + dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[16].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
// style
expect(includeDataURL(style)).to.equal(true)
done()
})
})
it('should allow adding custom html loaders', done => {
mockBundleAndRun({
entry: 'markdown.vue',
vue: {
loaders: {
html: 'marked'
}
}
}, (window, module) => {
const vnode = mockRender(module, {
msg: 'hi'
})
// <h2 id="-msg-">{{msg}}</h2>
expect(vnode.tag).to.equal('h2')
expect(vnode.data.attrs.id).to.equal('-msg-')
expect(vnode.children[0].text).to.equal('hi')
done()
})
})
it('custom compiler modules', done => {
mockBundleAndRun({
entry: 'custom-module.vue',
vue: {
compilerModules: [
{
postTransformNode: el => {
if (el.staticStyle) {
el.staticStyle = `$processStyle(${el.staticStyle})`
}
if (el.styleBinding) {
el.styleBinding = `$processStyle(${el.styleBinding})`
}
}
}
]
}
}, (window, module) => {
const results = []
// var vnode =
mockRender(
Object.assign(module, { methods: { $processStyle: style => results.push(style) }}),
{ transform: 'translateX(10px)' }
)
expect(results).to.deep.equal([
{ 'flex-direction': 'row' },
{ 'transform': 'translateX(10px)' }
])
done()
})
})
it('custom compiler directives', done => {
mockBundleAndRun({
entry: 'custom-directive.vue',
vue: {
compilerDirectives: {
i18n (el, dir) {
if (dir.name === 'i18n' && dir.value) {
el.i18n = dir.value
if (!el.props) {
el.props = []
}
el.props.push({ name: 'textContent', value: `_s(${JSON.stringify(dir.value)})` })
}
}
}
}
}, (window, module) => {
const vnode = mockRender(module)
expect(vnode.data.domProps.textContent).to.equal('keypath')
done()
})
})
it('functional component with styles', done => {
mockBundleAndRun({
entry: 'functional-style.vue'
}, (window, module, rawModule) => {
expect(module.functional).to.equal(true)
const vnode = mockRender(module)
// <div class="foo">hi</div>
expect(vnode.tag).to.equal('div')
expect(vnode.data.class).to.equal('foo')
expect(vnode.children[0].text).to.equal('functional')
let style = window.document.querySelector('style').textContent
style = normalizeNewline(style)
expect(style).to.contain('.foo { color: red;\n}')
done()
})
})
it('functional template', done => {
mockBundleAndRun({
entry: 'functional-root.vue',
vue: {
preserveWhitespace: false
}
}, (window, module) => {
expect(module.components.Functional._compiled).to.equal(true)
expect(module.components.Functional.functional).to.equal(true)
expect(module.components.Functional.staticRenderFns).to.exist
expect(module.components.Functional.render).to.be.a('function')
const vnode = mockRender(module, {
fn () {
done()
}
}).children[0]
// Basic vnode
expect(vnode.children[0].data.staticClass).to.equal('red')
expect(vnode.children[0].children[0].text).to.equal('hello')
// Default slot vnode
expect(vnode.children[1].tag).to.equal('span')
expect(vnode.children[1].children[0].text).to.equal('hello')
// Named slot vnode
expect(vnode.children[2].tag).to.equal('div')
expect(vnode.children[2].children[0].text).to.equal('Second slot')
// // Scoped slot vnode
expect(vnode.children[3].text).to.equal('hello')
// // Static content vnode
expect(vnode.children[4].tag).to.equal('div')
expect(vnode.children[4].children[0].text).to.equal('Some ')
expect(vnode.children[4].children[1].tag).to.equal('span')
expect(vnode.children[4].children[1].children[0].text).to.equal('text')
// // v-if vnode
expect(vnode.children[5].text).to.equal('')
vnode.children[6].data.on.click()
})
})
})