-
Notifications
You must be signed in to change notification settings - Fork 90
/
loader.js
165 lines (143 loc) 路 5.28 KB
/
loader.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
const loaderUtils = require('loader-utils')
const acorn = require('acorn')
const acornWalk = require('acorn-walk')
const vuetifyMatcher = require('./matcher/tag')
const vuetifyAttrsMatcher = require('./matcher/attr')
const { camelize, capitalize, hyphenate } = require('./util')
function getMatches (type, items, matches) {
const imports = []
items.forEach(item => {
for (const matcher of matches) {
const match = matcher(item, {
[`kebab${type}`]: hyphenate(item),
[`camel${type}`]: capitalize(camelize(item)),
path: this.resourcePath.substring(this.rootContext.length + 1),
})
if (match) {
imports.push(match)
break
}
}
})
imports.sort((a, b) => a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0))
return imports
}
function injectStylesSSR (imports) {
const styles = imports.map(componentImport => (componentImport[2] || [])).reduce((acc, styles) => {
styles && styles.forEach(style => acc.add(style))
return acc
}, new Set())
if (styles.size) {
return `
render._vuetifyStyles = function (component) {
if (process.env.VUE_ENV === 'server') {
const options = typeof component.exports === 'function'
? component.exports.extendOptions
: component.options
const existing = options.beforeCreate
const hook = function () {
${[...styles].map((style) => ` require('vuetify/${style}').__inject__(this.$ssrContext)`).join('\n')}
}
options.beforeCreate = existing
? [].concat(existing, hook)
: [hook]
}
}`
}
return ''
}
module.exports = async function (content, sourceMap) {
this.async()
this.cacheable()
const options = {
match: [],
attrsMatch: [],
registerStylesSSR: false,
...loaderUtils.getOptions(this)
}
if (!Array.isArray(options.match)) options.match = [options.match]
if (!Array.isArray(options.attrsMatch)) options.attrsMatch = [options.attrsMatch]
options.match.push(vuetifyMatcher)
options.attrsMatch.push(vuetifyAttrsMatcher)
const qs = new URLSearchParams(this.resourceQuery)
if (qs.has('vue') && qs.get('type') === 'template') {
this.addDependency(this.resourcePath)
const matches = {
components: [],
directives: [],
}
const ast = acorn.parse(content, { sourceType: 'module', ecmaVersion: 'latest' })
acornWalk.simple(ast, {
CallExpression (node) {
if (node.callee.name === '_c') {
if (node.arguments[0].type === 'Literal') {
matches.components.push([node.arguments[0].value, node.arguments[0].start, node.arguments[0].end])
}
if (node.arguments.length >= 2 && node.arguments[1].type === 'ObjectExpression') {
const props = node.arguments[1].properties
props.forEach(prop => {
if (prop.key.type === 'Identifier' && prop.key.name === 'directives' && prop.value.type === 'ArrayExpression') {
prop.value.elements.forEach(directive => {
if (directive.type === 'ObjectExpression') {
directive.properties.forEach(prop => {
if (prop.key.type === 'Identifier' && prop.key.name === 'name') {
matches.directives.push([prop.value.value, prop.start, prop.end])
}
})
}
})
}
})
}
}
}
})
const components = getMatches.call(this, 'Tag', dedupe(matches.components), options.match)
const directives = getMatches.call(this, 'Attr', dedupe(matches.directives), options.attrsMatch)
const allMatches = [...matches.components.map(v => ({
type: 'component',
name: capitalize(camelize(v[0])),
start: v[1],
end: v[2],
})), ...matches.directives.map(v => ({
type: 'directive',
name: capitalize(camelize(v[0])),
start: v[1],
end: v[2],
}))].sort((a, b) => a.start - b.start)
for (let i = allMatches.length - 1; i >= 0; i--) {
const tag = allMatches[i]
if (tag.type === 'component') {
if (!components.some(c => c[0] === tag.name)) continue
content = content.slice(0, tag.start) + tag.name + content.slice(tag.end)
} else {
if (!directives.some(c => c[0] === tag.name)) continue
const indent = content.slice(0, tag.start).match(/\s*$/)[0]
content = content.slice(0, tag.start) +
'def: ' + tag.name + ',' +
indent + content.slice(tag.start)
}
}
const imports = [...components, ...directives]
if (imports.length) {
content = imports.map(v => v[1]).join('\n') + '\n\n' + content
}
if (options.registerStylesSSR) {
content += injectStylesSSR(imports)
}
} else if (options.registerStylesSSR && !this.resourceQuery) {
this.addDependency(this.resourcePath)
const newContent = 'if (render._vuetifyStyles) { render._vuetifyStyles(component) }'
// Insert our modification before the HMR code
const hotReload = content.indexOf('/* hot reload */')
if (hotReload > -1) {
content = content.slice(0, hotReload) + newContent + '\n\n' + content.slice(hotReload)
} else {
content += '\n\n' + newContent
}
}
this.callback(null, content, sourceMap)
}
function dedupe (matches) {
return [...new Set(matches.map(i => capitalize(camelize(i[0]))))]
}