-
Notifications
You must be signed in to change notification settings - Fork 915
/
Copy pathloader.js
182 lines (162 loc) · 5.44 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
var loaderUtils = require('loader-utils')
var assign = require('object-assign')
var selectorPath = require.resolve('./selector')
var parserPath = require.resolve('./parser')
var defaultLang = {
template: 'html',
style: 'css',
script: 'js'
}
var defaultLoaders = {
html: 'vue-html',
css: 'style!css',
js: 'babel?optional[]=runtime&loose=all&nonStandard=false'
}
var rewriters = {
template: require.resolve('./template-rewriter'),
style: require.resolve('./style-rewriter')
}
module.exports = function (content) {
var self = this
this.cacheable()
var cb = this.async()
var output = ''
var options = this.options.vue || {}
var vueUrl = loaderUtils.getRemainingRequest(this)
// check if there are custom loaders specified with
// vueLoader.withLoaders(), 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(self,
// disable system loaders (except post 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
vueUrl
)
}
function getRequireForImport (impt) {
return 'require(' +
loaderUtils.stringifyRequest(self,
'-!' +
getLoaderString('style', impt, impt.scoped) +
impt.src
) +
')\n'
}
function getLoaderString (type, part, scoped) {
var lang = part.lang || defaultLang[type]
var loader = loaders[lang]
var rewriter = getRewriter(type, scoped)
if (loader !== undefined) {
// lang with default or pre-configured loader
if (loader) loader += '!'
return loader + rewriter
} else {
// unknown lang, assume a loader for it is used
switch (type) {
case 'template':
return 'vue-html!' + rewriter + 'template-html?raw&engine=' + lang + '!'
case 'style':
return 'style!css!' + rewriter + lang + '!'
case 'script':
return lang + '!'
}
}
}
function getRewriter (type, scoped) {
switch (type) {
case 'template':
return scoped ? (rewriters.template + '!') : ''
case 'style':
return rewriters.style + (scoped ? '?scoped=true!' : '!')
default:
return ''
}
}
function getSelectorString (type, index) {
return selectorPath +
'?type=' + type +
'&index=' + index + '!'
}
var url = '!!' + parserPath + '!' + vueUrl
this.loadModule(url, function (err, source) {
if (err) return cb(err)
// up to this part, what we have done is basically executing
// parser.js on the raw vue file and get the parsing result
// which is an object that contains info about the vue file.
var parts = self.exec(source, url)
var hasLocalStyles = false
// add requires for src imports
parts.styleImports.forEach(function (impt) {
if (impt.scoped) hasLocalStyles = true
output += getRequireForImport(impt)
})
// add requires for styles
parts.style.forEach(function (style, i) {
if (style.scoped) hasLocalStyles = true
output += getRequire('style', style, i, style.scoped)
})
// add require for script
if (parts.script.length) {
output += 'module.exports = ' +
getRequire('script', parts.script[0], 0)
}
// add require for template
if (parts.template.length) {
output += ';(typeof module.exports === "function" ' +
'? module.exports.options ' +
': module.exports).template = ' +
getRequire('template', parts.template[0], 0, hasLocalStyles)
}
// hot reload
if (
process.env.NODE_ENV !== 'production' &&
(parts.script.length || parts.template.length)
) {
var scriptString = parts.script.length ? getRequireString('script', parts.script[0], 0) : ''
var templateString = parts.template.length ? getRequireString('template', parts.template[0], 0, hasLocalStyles) : ''
var accepted = []
if (scriptString) {
accepted.push(scriptString.slice(1, -1))
}
if (templateString) {
accepted.push(templateString.slice(1, -1))
}
output +=
'if (module.hot) {\n' +
'(function () {\n' +
// shim the component directive so that it
// registers the instances
'var hotAPI = require("vue-hot-reload-api")\n' +
'hotAPI.install(require("vue"))\n' +
'if (!hotAPI.compatible) return\n' +
'var id = ' + (scriptString || templateString) + '\n' +
// create the record
'hotAPI.createRecord(id, module.exports)\n' +
'module.hot.accept(' + JSON.stringify(accepted) + ', function () {\n' +
'var newOptions = ' + (scriptString ? 'require(' + scriptString + ')\n' : 'null\n') +
'var newTemplate = ' + (templateString ? 'require(' + templateString + ')\n' : 'null\n') +
'hotAPI.update(id, newOptions, newTemplate)\n' +
'})\n' +
'})()\n' +
'}'
}
// done
cb(null, output)
})
}
module.exports.withLoaders = function () {
throw new Error(
'vue.withLoaders has been deprecated in vue-loader 6.0. ' +
'Add a "vue" section to the webpack config instead.'
)
}