@@ -27,8 +27,8 @@ module.exports = function (content) {
27
27
var self = this
28
28
this . cacheable ( )
29
29
var cb = this . async ( )
30
- var output = ''
31
30
var options = this . options . vue || { }
31
+ var query = loaderUtils . parseQuery ( this . query )
32
32
var vueUrl = loaderUtils . getRemainingRequest ( this )
33
33
var filePath = this . resourcePath
34
34
var fileName = path . basename ( filePath )
@@ -80,6 +80,7 @@ module.exports = function (content) {
80
80
var lang = part . lang || defaultLang [ type ]
81
81
var loader = loaders [ lang ]
82
82
var rewriter = getRewriter ( type , scoped )
83
+ var injectString = ( type === 'script' && query . inject ) ? 'inject!' : ''
83
84
if ( loader !== undefined ) {
84
85
// inject rewriter before css/html loader for
85
86
// extractTextPlugin use cases
@@ -90,7 +91,7 @@ module.exports = function (content) {
90
91
} else {
91
92
loader = ensureBang ( loader ) + rewriter
92
93
}
93
- return ensureBang ( loader )
94
+ return injectString + ensureBang ( loader )
94
95
} else {
95
96
// unknown lang, assume a loader for it is used
96
97
switch ( type ) {
@@ -99,7 +100,7 @@ module.exports = function (content) {
99
100
case 'style' :
100
101
return 'style!css!' + rewriter + lang + '!'
101
102
case 'script' :
102
- return lang + '!'
103
+ return injectString + lang + '!'
103
104
}
104
105
}
105
106
}
@@ -139,6 +140,7 @@ module.exports = function (content) {
139
140
// which is an object that contains info about the vue file.
140
141
var parts = self . exec ( source , url )
141
142
var hasLocalStyles = false
143
+ var output = 'var __vue_script__, __vue_template__\n'
142
144
143
145
// add requires for src imports
144
146
parts . styleImports . forEach ( function ( impt ) {
@@ -157,46 +159,61 @@ module.exports = function (content) {
157
159
if ( parts . script . length ) {
158
160
script = parts . script [ 0 ]
159
161
output +=
160
- 'module.exports = ' + (
162
+ '__vue_script__ = ' + (
161
163
script . src
162
164
? getRequireForImport ( 'script' , script , 0 )
163
165
: getRequire ( 'script' , script , 0 )
164
- ) + '\nif (module.exports.__esModule) module.exports = module.exports.default\n'
166
+ )
165
167
}
166
168
167
169
// add require for template
168
170
var template
169
171
if ( parts . template . length ) {
170
172
template = parts . template [ 0 ]
171
- output += ';(typeof module.exports === "function" ' +
172
- '? module.exports.options ' +
173
- ': module.exports).template = ' + (
173
+ output += '__vue_template__ = ' + (
174
174
template . src
175
175
? getRequireForImport ( 'template' , template , hasLocalStyles )
176
176
: getRequire ( 'template' , template , 0 , hasLocalStyles )
177
177
)
178
178
}
179
179
180
- // hot reload
181
- if (
182
- process . env . NODE_ENV !== 'production' &&
183
- ( parts . script . length || parts . template . length )
184
- ) {
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
+ ';(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 {
185
208
output +=
186
- 'if (module.hot) {(function () {' +
187
- ' module.hot.accept()\n' +
188
- ' var hotAPI = require("vue-hot-reload-api")\n' +
189
- ' hotAPI.install(require("vue"), true)\n' +
190
- ' if (!hotAPI.compatible) return\n' +
191
- ' var id = ' + JSON . stringify ( filePath ) + '\n' +
192
- ' if (!module.hot.data) {\n' +
193
- // initial insert
194
- ' hotAPI.createRecord(id, module.exports)\n' +
195
- ' } else {\n' +
196
- // update
197
- ' hotAPI.update(id, module.exports, (typeof module.exports === "function" ? module.exports.options : module.exports).template)\n' +
198
- ' }\n' +
199
- '})()}'
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
+ ' ;(typeof mod === "function" ? mod.options : mod).template = __vue_template__\n' +
215
+ ' return mod\n' +
216
+ '}'
200
217
}
201
218
202
219
// done
0 commit comments