forked from vuetifyjs/vuetify-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
36 lines (30 loc) 路 933 Bytes
/
module.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
const { hyphenate } = require('../lib/util')
module.exports = {
postTransformNode: transform
}
// Modified from @vue/component-compiler-utils
function transform(node) {
const tags = ['v-img', 'v-card-media', 'v-carousel-item']
if (tags.includes(hyphenate(node.tag)) && node.attrs) {
const attr = node.attrs.find(a => a.name === 'src')
if (!attr) return
const value = attr.value
// only transform static URLs
if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
attr.value = urlToRequire(value.slice(1, -1))
}
}
return node
}
function urlToRequire(url) {
const firstChar = url.charAt(0)
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
if (firstChar === '~') {
const secondChar = url.charAt(1)
url = url.slice(secondChar === '/' ? 2 : 1)
}
return `require("${url}?vuetify-preload")`
} else {
return `"${url}"`
}
}