-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (51 loc) · 1.62 KB
/
index.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
function ensureLoaderSuffix(name) {
const loaderSuffixRegexp = /-loader$/
return loaderSuffixRegexp.test(name) ? name : `${name}-loader`
}
class VueLoaderOptionPlugin {
constructor(options = {}) {
this.options = options
this.vueLoaderIdentifier = 'vue-loader/lib/selector'
}
apply(compiler) {
compiler.plugin('compilation', compilation => {
compilation.plugin('normal-module-loader', (context, module) => {
const loaderOption = this.matchLoaderOption(module.request, module.resource)
if (!loaderOption.loaderName || !loaderOption.options) {
return
}
module.loaders.forEach(l => {
if (l.loader.indexOf(loaderOption.loader) >= 0) {
if (loaderOption.options.toContext) {
context[loaderOption.loaderName] = loaderOption.options
} else {
l.options = loaderOption.options
}
}
})
})
})
}
matchLoaderOption(request, resource) {
const VUE_TEST = /\.vue$/
const i = resource.indexOf('?')
if (!VUE_TEST.test(i < 0 ? resource : resource.substr(0, i))) {
return {}
}
const loaders = request.split('!')
return loaders.reduceRight((ret, loader) => {
Object.keys(this.options).reduce((_, loaderName) => {
if (loader.indexOf(ensureLoaderSuffix(loaderName)) >= 0) {
ret = {
loaderName: loaderName,
loader: ensureLoaderSuffix(loaderName),
options: this.options[loaderName]
}
}
return {}
}, {})
return ret
}, {})
}
}
module.exports = VueLoaderOptionPlugin