1
1
var loaderUtils = require ( 'loader-utils' )
2
2
var assign = require ( 'object-assign' )
3
+ var parse = require ( './parser' )
3
4
var selectorPath = require . resolve ( './selector' )
4
- var parserPath = require . resolve ( './parser' )
5
5
var hash = require ( 'hash-sum' )
6
6
var path = require ( 'path' )
7
7
@@ -26,7 +26,6 @@ var rewriters = {
26
26
module . exports = function ( content ) {
27
27
var self = this
28
28
this . cacheable ( )
29
- var cb = this . async ( )
30
29
var options = this . options . vue || { }
31
30
var query = loaderUtils . parseQuery ( this . query )
32
31
var vueUrl = loaderUtils . getRemainingRequest ( this )
@@ -39,8 +38,8 @@ module.exports = function (content) {
39
38
defaultLoaders . js = 'babel-loader'
40
39
}
41
40
42
- // check if there are custom loaders specified with
43
- // vueLoader.withLoaders() , otherwise use defaults
41
+ // check if there are custom loaders specified via
42
+ // webpack config , otherwise use defaults
44
43
var loaders = assign ( { } , defaultLoaders , options . loaders )
45
44
46
45
function getRequire ( type , part , index , scoped ) {
@@ -131,94 +130,86 @@ module.exports = function (content) {
131
130
}
132
131
}
133
132
134
- var url = '!!' + parserPath + '!' + vueUrl
135
- this . loadModule ( url , function ( err , source ) {
136
- if ( err ) return cb ( err )
137
-
138
- // up to this part, what we have done is basically executing
139
- // parser.js on the raw vue file and get the parsing result
140
- // which is an object that contains info about the vue file.
141
- var parts = self . exec ( source , url )
142
- var hasLocalStyles = false
143
- var output = 'var __vue_script__, __vue_template__\n'
144
-
145
- // add requires for src imports
146
- parts . styleImports . forEach ( function ( impt ) {
147
- if ( impt . scoped ) hasLocalStyles = true
148
- output += getRequireForImport ( 'style' , impt , impt . scoped )
149
- } )
150
-
151
- // add requires for styles
152
- parts . style . forEach ( function ( style , i ) {
153
- if ( style . scoped ) hasLocalStyles = true
154
- output += getRequire ( 'style' , style , i , style . scoped )
155
- } )
156
-
157
- // add require for script
158
- var script
159
- if ( parts . script . length ) {
160
- script = parts . script [ 0 ]
161
- output +=
162
- '__vue_script__ = ' + (
163
- script . src
164
- ? getRequireForImport ( 'script' , script , 0 )
165
- : getRequire ( 'script' , script , 0 )
166
- )
167
- }
133
+ var parts = parse ( content )
134
+ var hasLocalStyles = false
135
+ var output = 'var __vue_script__, __vue_template__\n'
168
136
169
- // add require for template
170
- var template
171
- if ( parts . template . length ) {
172
- template = parts . template [ 0 ]
173
- output += '__vue_template__ = ' + (
174
- template . src
175
- ? getRequireForImport ( 'template' , template , hasLocalStyles )
176
- : getRequire ( 'template' , template , 0 , hasLocalStyles )
177
- )
178
- }
137
+ // add requires for src imports
138
+ parts . styleImports . forEach ( function ( impt ) {
139
+ if ( impt . scoped ) hasLocalStyles = true
140
+ output += getRequireForImport ( 'style' , impt , impt . scoped )
141
+ } )
179
142
180
- if ( ! query . inject ) {
181
- // attach template
182
- output +=
183
- 'module.exports = __vue_script__ || {}\n' +
184
- 'if (module.exports.__esModule) module.exports = module.exports.default\n' +
185
- 'if (__vue_template__) { (typeof module.exports === "function" ? module.exports.options : module.exports).template = __vue_template__ }\n'
186
- // hot reload
187
- if (
188
- process . env . NODE_ENV !== 'production' &&
189
- ( parts . script . length || parts . template . length )
190
- ) {
191
- output +=
192
- 'if (module.hot) {(function () {' +
193
- ' module.hot.accept()\n' +
194
- ' var hotAPI = require("vue-hot-reload-api")\n' +
195
- ' hotAPI.install(require("vue"), true)\n' +
196
- ' if (!hotAPI.compatible) return\n' +
197
- ' var id = ' + JSON . stringify ( filePath ) + '\n' +
198
- ' if (!module.hot.data) {\n' +
199
- // initial insert
200
- ' hotAPI.createRecord(id, module.exports)\n' +
201
- ' } else {\n' +
202
- // update
203
- ' hotAPI.update(id, module.exports, __vue_template__)\n' +
204
- ' }\n' +
205
- '})()}'
206
- }
207
- } else {
143
+ // add requires for styles
144
+ parts . style . forEach ( function ( style , i ) {
145
+ if ( style . scoped ) hasLocalStyles = true
146
+ output += getRequire ( 'style' , style , i , style . scoped )
147
+ } )
148
+
149
+ // add require for script
150
+ var script
151
+ if ( parts . script . length ) {
152
+ script = parts . script [ 0 ]
153
+ output +=
154
+ '__vue_script__ = ' + (
155
+ script . src
156
+ ? getRequireForImport ( 'script' , script , 0 )
157
+ : getRequire ( 'script' , script , 0 )
158
+ )
159
+ }
160
+
161
+ // add require for template
162
+ var template
163
+ if ( parts . template . length ) {
164
+ template = parts . template [ 0 ]
165
+ output += '__vue_template__ = ' + (
166
+ template . src
167
+ ? getRequireForImport ( 'template' , template , hasLocalStyles )
168
+ : getRequire ( 'template' , template , 0 , hasLocalStyles )
169
+ )
170
+ }
171
+
172
+ if ( ! query . inject ) {
173
+ // attach template
174
+ output +=
175
+ 'module.exports = __vue_script__ || {}\n' +
176
+ 'if (module.exports.__esModule) module.exports = module.exports.default\n' +
177
+ 'if (__vue_template__) { (typeof module.exports === "function" ? module.exports.options : module.exports).template = __vue_template__ }\n'
178
+ // hot reload
179
+ if (
180
+ process . env . NODE_ENV !== 'production' &&
181
+ ( parts . script . length || parts . template . length )
182
+ ) {
208
183
output +=
209
- 'module.exports = function (injections) {\n' +
210
- ' var mod = __vue_script__\n' +
211
- ' ? __vue_script__(injections)\n' +
212
- ' : {}\n' +
213
- ' if (mod.__esModule) mod = mod.default\n' +
214
- ' if (__vue_template__) { (typeof mod === "function" ? mod.options : mod).template = __vue_template__ }\n' +
215
- ' return mod\n' +
216
- '}'
184
+ 'if (module.hot) {(function () {' +
185
+ ' module.hot.accept()\n' +
186
+ ' var hotAPI = require("vue-hot-reload-api")\n' +
187
+ ' hotAPI.install(require("vue"), true)\n' +
188
+ ' if (!hotAPI.compatible) return\n' +
189
+ ' var id = ' + JSON . stringify ( filePath ) + '\n' +
190
+ ' if (!module.hot.data) {\n' +
191
+ // initial insert
192
+ ' hotAPI.createRecord(id, module.exports)\n' +
193
+ ' } else {\n' +
194
+ // update
195
+ ' hotAPI.update(id, module.exports, __vue_template__)\n' +
196
+ ' }\n' +
197
+ '})()}'
217
198
}
199
+ } else {
200
+ output +=
201
+ 'module.exports = function (injections) {\n' +
202
+ ' var mod = __vue_script__\n' +
203
+ ' ? __vue_script__(injections)\n' +
204
+ ' : {}\n' +
205
+ ' if (mod.__esModule) mod = mod.default\n' +
206
+ ' if (__vue_template__) { (typeof mod === "function" ? mod.options : mod).template = __vue_template__ }\n' +
207
+ ' return mod\n' +
208
+ '}'
209
+ }
218
210
219
- // done
220
- cb ( null , output )
221
- } )
211
+ // done
212
+ return output
222
213
}
223
214
224
215
module . exports . withLoaders = function ( ) {
0 commit comments