-
Notifications
You must be signed in to change notification settings - Fork 918
/
Copy pathplugin.js
211 lines (188 loc) · 6.72 KB
/
plugin.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
const fs = require('fs')
const path = require('path')
const qs = require('querystring')
const RuleSet = require('webpack/lib/RuleSet')
const id = 'vue-loader-plugin'
const NS = path.dirname(fs.realpathSync(__filename))
class VueLoaderPlugin {
apply (compiler) {
// add NS marker so that the loader can detect and report missing plugin
if (compiler.hooks) {
// webpack 4
compiler.hooks.compilation.tap(id, compilation => {
compilation.hooks.normalModuleLoader.tap(id, loaderContext => {
loaderContext[NS] = true
})
})
} else {
// webpack < 4
compiler.plugin('compilation', compilation => {
compilation.plugin('normal-module-loader', loaderContext => {
loaderContext[NS] = true
})
})
}
// get a hold of the raw rules
const rawRules = compiler.options.module.rules.slice()
// use webpack's RuleSet utility to normalize user rules
const rawNormalizedRules = new RuleSet(rawRules).rules
const createMatcher = fakeFile => (rule, i) => {
// #1201 we need to skip the `include` check when locating the vue rule
const clone = Object.assign({}, rule)
delete clone.include
const normalized = RuleSet.normalizeRule(clone, {}, '')
return (
!rule.enforce &&
normalized.resource &&
normalized.resource(fakeFile)
)
}
// find the rule that applies to vue files
let vueRuleIndex = rawRules.findIndex(createMatcher(`foo.vue`))
if (vueRuleIndex < 0) {
vueRuleIndex = rawRules.findIndex(createMatcher(`foo.vue.html`))
}
const vueRule = rawRules[vueRuleIndex]
if (!vueRule) {
throw new Error(
`[VueLoaderPlugin Error] No matching rule for .vue files found.\n` +
`Make sure there is at least one root-level rule that matches .vue or .vue.html files.`
)
}
if (vueRule.oneOf) {
throw new Error(
`[VueLoaderPlugin Error] vue-loader 15 currently does not support vue rules with oneOf.`
)
}
// find the normalized version of the vue rule
const normalizedVueRule = rawNormalizedRules[vueRuleIndex]
// get the normlized "use" for vue files
const normalizedVueUse = normalizedVueRule.use
// get vue-loader options
const vueLoaderUseIndex = normalizedVueUse.findIndex(u => {
return /^vue-loader|(\/|\\)vue-loader/.test(u.loader)
})
if (vueLoaderUseIndex < 0) {
throw new Error(
`[VueLoaderPlugin Error] No matching use for vue-loader is found.\n` +
`Make sure the rule matching .vue files include vue-loader in its use.`
)
}
// make sure vue-loader options has a known ident so that we can share
// options by reference in the template-loader by using a ref query like
// template-loader??vue-loader-options
const ident = 'vue-loader-options'
const vueLoaderUse = normalizedVueUse[vueLoaderUseIndex]
// has options, just set ident
if (vueLoaderUse.options) {
vueLoaderUse.options.ident = ident
} else {
// user provided no options, but we must ensure the options is present
// otherwise RuleSet throws error if no option for a given ref is found.
if (vueRule.loader || vueRule.loaders) {
vueRule.options = { ident }
} else if (Array.isArray(vueRule.use)) {
const use = vueRule.use[vueLoaderUseIndex]
if (typeof use === 'string') {
vueRule.use[vueLoaderUseIndex] = { loader: use, options: { ident }}
} else {
use.options = { ident }
}
} else if (typeof vueRule.use === 'string') {
vueRule.use = [{ loader: vueRule.use, options: { ident }}]
} else {
throw new Error(
`VueLoaderPlugin Error: this should not happen. Please open an issue ` +
`with your webpack config.`
)
}
}
// get new rules without the vue rule
const baseRules = rawRules.filter(r => r !== vueRule)
const normalizedRules = rawNormalizedRules.filter(r => r !== normalizedVueRule)
// for each user rule, inject a cloned rule by checking if the rule
// matches the lang specified in the resourceQuery.
rawRules.unshift.apply(rawRules, baseRules.map((rule, i) => {
return cloneRule(rule, normalizedRules[i])
}))
// inject global pitcher (responsible for injecting template compiler
// loader & CSS post loader)
rawRules.unshift({
loader: require.resolve('./loaders/pitcher'),
resourceQuery: query => {
const parsed = qs.parse(query.slice(1))
return parsed.vue != null
}
})
// replace original rules
compiler.options.module.rules = rawRules
}
}
function cloneRule (rule, normalizedRule) {
// Assuming `test` and `resourceQuery` tests are executed in series and
// synchronously (which is true based on RuleSet's implementation), we can
// save the current resource being matched from `test` so that we can access
// it in `resourceQuery`. This ensures when we use the normalized rule's
// resource check, include/exclude are matched correctly.
let currentResource
const res = Object.assign({}, rule, {
test: resource => {
currentResource = resource
return true
},
resourceQuery: query => {
const parsed = qs.parse(query.slice(1))
if (parsed.vue == null) {
return false
}
const { resource, resourceQuery } = normalizedRule
if (resource && parsed.lang == null) {
return false
}
const fakeResourcePath = `${currentResource}.${parsed.lang}`
if (resource && !resource(fakeResourcePath)) {
return false
}
if (resourceQuery && !resourceQuery(query)) {
return false
}
return true
},
use: normalizedRule.use ? normalizedRule.use.map(cleanIdent) : undefined
})
// delete shorthand since we have normalized use
delete res.loader
delete res.loaders
delete res.options
// these are included in the normalized resource() check
delete res.resource
delete res.include
delete res.exclude
if (rule.oneOf) {
res.oneOf = rule.oneOf.map((r, i) => {
return cloneRule(r, normalizedRule.oneOf[i])
})
}
return res
}
const reuseIdentWhitelist = [
'css-loader',
'(vue-)?style-loader',
'postcss-loader',
'extract-text-webpack-plugin',
'mini-css-extract-plugin'
]
const reuseIdentPattern = new RegExp(`(${reuseIdentWhitelist.join('|')})`)
function cleanIdent (use) {
if (use.ident) {
if (reuseIdentPattern.test(use.loader)) {
// Reuse options ident, so that imports from within css-loader would get the
// exact same request prefixes, avoiding duplicated modules (#1199)
use.options.ident = use.ident
}
delete use.ident
}
return use
}
VueLoaderPlugin.NS = NS
module.exports = VueLoaderPlugin