Skip to content

Commit d0e115c

Browse files
committed
fix: auto-import directives with v-on and v-bind
fixes #258
1 parent adc2d84 commit d0e115c

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

dev5/src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<v-text-field></v-text-field>
1010
</v-card-text>
1111
</v-card>
12+
<v-card v-bind="$attrs" v-ripple>v-bind</v-card>
13+
<v-card v-on="$listeners" v-ripple>v-on</v-card>
14+
<v-card v-bind="$attrs" v-on="$listeners" v-ripple>v-bind v-on</v-card>
1215
<pre>{{ {
1316
directives: [{
1417
name: "resize"

dev5/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module.exports = {
8484
hints: false
8585
},
8686
optimization: {
87+
minimize: false,
8788
concatenateModules: false
8889
}
8990
}

lib/loader.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,48 @@ module.exports = async function (content, sourceMap) {
8080
const ast = acorn.parse(content, { sourceType: 'module', ecmaVersion: 'latest' })
8181
acornWalk.simple(ast, {
8282
CallExpression (node) {
83-
if (node.callee.name === '_c') {
83+
let props
84+
85+
if (
86+
// _c() or _vm._c()
87+
node.callee.type === 'Identifier'
88+
? node.callee.name === '_c'
89+
: (node.callee.type === 'MemberExpression' &&
90+
node.callee.object.name === '_vm' &&
91+
node.callee.property.name === '_c')
92+
) {
8493
if (node.arguments[0].type === 'Literal') {
94+
// _c('div')
8595
matches.components.push([node.arguments[0].value, node.arguments[0].start, node.arguments[0].end])
8696
}
97+
8798
if (node.arguments.length >= 2 && node.arguments[1].type === 'ObjectExpression') {
88-
const props = node.arguments[1].properties
89-
props.forEach(prop => {
90-
if (prop.key.type === 'Identifier' && prop.key.name === 'directives' && prop.value.type === 'ArrayExpression') {
91-
prop.value.elements.forEach(directive => {
92-
if (directive.type === 'ObjectExpression') {
93-
directive.properties.forEach(prop => {
94-
if (prop.key.type === 'Identifier' && prop.key.name === 'name') {
95-
matches.directives.push([prop.value.value, prop.start, prop.end])
96-
}
97-
})
99+
props = node.arguments[1].properties
100+
}
101+
} else if (
102+
// _vm._b() or _vm._g()
103+
node.callee.type === 'MemberExpression' &&
104+
node.callee.object.name === '_vm' &&
105+
['_b', '_g'].includes(node.callee.property.name) &&
106+
node.arguments.length > 0 &&
107+
node.arguments[0].type === 'ObjectExpression'
108+
) {
109+
props = node.arguments[0].properties
110+
}
111+
112+
props && props.forEach(prop => {
113+
if (prop.key.type === 'Identifier' && prop.key.name === 'directives' && prop.value.type === 'ArrayExpression') {
114+
prop.value.elements.forEach(directive => {
115+
if (directive.type === 'ObjectExpression') {
116+
directive.properties.forEach(prop => {
117+
if (prop.key.type === 'Identifier' && prop.key.name === 'name') {
118+
matches.directives.push([prop.value.value, prop.start, prop.end])
98119
}
99120
})
100121
}
101122
})
102123
}
103-
}
124+
})
104125
}
105126
})
106127

0 commit comments

Comments
 (0)