-
Notifications
You must be signed in to change notification settings - Fork 916
/
Copy pathloader.js
257 lines (230 loc) · 7.67 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var loaderUtils = require('loader-utils')
var assign = require('object-assign')
var parse = require('./parser')
var path = require('path')
var normalize = require('./normalize')
var genId = require('./gen-id')
// internal lib loaders
var selectorPath = normalize.lib('selector')
var templateLoaderPath = normalize.lib('template-loader')
var templateCompilerPath = normalize.lib('template-compiler')
var styleRewriterPath = normalize.lib('style-rewriter')
// dep loaders
var styleLoaderPath = normalize.dep('vue-style-loader')
var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
var hasBabel = false
try {
hasBabel = !!require('babel-loader')
} catch (e) {}
var hasBuble = false
try {
hasBuble = !!require('buble-loader')
} catch (e) {}
var rewriterInjectRE = /\b(css(?:-loader)?(?:\?[^!]+)?)(?:!|$)/
var defaultLang = {
template: 'html',
styles: 'css',
script: 'js'
}
var checkNamedExports =
'if (Object.keys(__vue_exports__).some(function (key) { return key !== "default" && key !== "__esModule" })) {' +
'console.error("named exports are not supported in *.vue files.")}\n'
module.exports = function (content) {
this.cacheable()
var isServer = this.options.target === 'node'
var loaderContext = this
var options = this.options.vue || {}
var query = loaderUtils.parseQuery(this.query)
var filePath = this.resourcePath
var fileName = path.basename(filePath)
var moduleId = 'data-v-' + genId(filePath)
var styleRewriter = styleRewriterPath + '?id=' + moduleId
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
var needCssSourceMap =
!isProduction &&
this.sourceMap &&
options.cssSourceMap !== false
var defaultLoaders = {
html: templateCompilerPath + '?id=' + moduleId,
css: styleLoaderPath + '!css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
js: hasBuble ? 'buble-loader' : hasBabel ? 'babel-loader' : ''
}
// check if there are custom loaders specified via
// webpack config, otherwise use defaults
var loaders = assign({}, defaultLoaders, options.loaders)
function getRequire (type, part, index, scoped) {
return 'require(' +
getRequireString(type, part, index, scoped) +
')\n'
}
function getRequireString (type, part, index, scoped) {
return loaderUtils.stringifyRequest(loaderContext,
// disable all configuration loaders
'!!' +
// get loader string for pre-processors
getLoaderString(type, part, scoped) +
// select the corresponding part from the vue file
getSelectorString(type, index || 0) +
// the url to the actual vuefile
filePath
)
}
function getRequireForImport (type, impt, scoped) {
return 'require(' +
getRequireForImportString(type, impt, scoped) +
')\n'
}
function getRequireForImportString (type, impt, scoped) {
return loaderUtils.stringifyRequest(loaderContext,
'!!' +
getLoaderString(type, impt, scoped) +
impt.src
)
}
function getLoaderString (type, part, scoped) {
var lang = part.lang || defaultLang[type]
var loader = loaders[lang]
var rewriter = type === 'styles' ? styleRewriter + (scoped ? '&scoped=true!' : '!') : ''
var injectString = (type === 'script' && query.inject) ? 'inject!' : ''
if (loader !== undefined) {
// inject rewriter before css/html loader for
// extractTextPlugin use cases
if (rewriterInjectRE.test(loader)) {
loader = loader.replace(rewriterInjectRE, function (m, $1) {
return ensureBang($1) + rewriter
})
} else {
loader = ensureBang(loader) + rewriter
}
return injectString + ensureBang(loader)
} else {
// unknown lang, infer the loader to be used
switch (type) {
case 'template':
return defaultLoaders.html + '!' + templateLoaderPath + '?raw&engine=' + lang + '!'
case 'styles':
return defaultLoaders.css + '!' + rewriter + lang + '!'
case 'script':
return injectString + lang + '!'
}
}
}
function getSelectorString (type, index) {
return selectorPath +
'?type=' + type +
'&index=' + index + '!'
}
function ensureBang (loader) {
if (loader.charAt(loader.length - 1) !== '!') {
return loader + '!'
} else {
return loader
}
}
var parts = parse(content, fileName, this.sourceMap)
var hasScoped = parts.styles.some(function (s) { return s.scoped })
var output = 'var __vue_exports__, __vue_options__\n'
// add requires for styles
if (!isServer && parts.styles.length) {
output += '\n/* styles */\n'
parts.styles.forEach(function (style, i) {
output += style.src
? getRequireForImport('styles', style, style.scoped)
: getRequire('styles', style, i, style.scoped)
})
}
// add require for script
var script = parts.script
if (script) {
output += '\n/* script */\n'
output +=
'__vue_exports__ = ' + (
script.src
? getRequireForImport('script', script)
: getRequire('script', script)
)
}
var exports =
'__vue_options__ = __vue_exports__ = __vue_exports__ || {}\n' +
// ES6 modules interop
'if (\n' +
' typeof __vue_exports__.default === "object" ||\n' +
' typeof __vue_exports__.default === "function"\n' +
') {\n' +
(isProduction ? '' : checkNamedExports) +
'__vue_options__ = __vue_exports__ = __vue_exports__.default\n' +
'}\n' +
// constructor export interop
'if (typeof __vue_options__ === "function") {\n' +
' __vue_options__ = __vue_options__.options\n' +
'}\n'
// add require for template
var template = parts.template
if (template) {
output += '\n/* template */\n'
output += 'var __vue_template__ = ' + (
template.src
? getRequireForImport('template', template)
: getRequire('template', template)
)
// attach render functions to exported options
exports +=
'__vue_options__.render = __vue_template__.render\n' +
'__vue_options__.staticRenderFns = __vue_template__.staticRenderFns\n'
}
// attach scoped id
if (hasScoped) {
exports += '__vue_options__._scopeId = "' + moduleId + '"\n'
}
if (!query.inject) {
output += exports
// hot reload
if (
!isServer &&
!isProduction &&
(parts.script || parts.template)
) {
output +=
'\n/* hot reload */\n' +
'if (module.hot) {(function () {\n' +
' var hotAPI = require("' + hotReloadAPIPath + '")\n' +
' hotAPI.install(require("vue"), false)\n' +
' if (!hotAPI.compatible) return\n' +
' module.hot.accept()\n' +
' if (!module.hot.data) {\n' +
// initial insert
' hotAPI.createRecord("' + moduleId + '", __vue_options__)\n' +
' } else {\n' +
// update
' hotAPI.reload("' + moduleId + '", __vue_options__)\n' +
' }\n' +
'})()}\n'
}
// check functional
if (!isProduction) {
output +=
'if (__vue_options__.functional) {console.error("' +
'[vue-loader] ' + fileName + ': functional components are not ' +
'supported and should be defined in plain js files using render ' +
'functions.' +
'")}\n'
}
// final export
if (options.esModule) {
output += '\nexports.__esModule = true;\nexports["default"] = __vue_exports__\n'
} else {
output += '\nmodule.exports = __vue_exports__\n'
}
} else {
// inject-loader support
output +=
'\n/* dependency injection */\n' +
'module.exports = function (injections) {\n' +
' __vue_exports__ = __vue_exports__(injections)\n' +
exports +
' return __vue_exports__\n' +
'}'
}
// done
return output
}