Skip to content

Commit c9bd506

Browse files
committed
support 2.0 hot-reload
1 parent 961c065 commit c9bd506

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

lib/loader.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ var selectorPath = 'vue-loader/lib/selector'
1515
var templateLoader = 'vue-loader/lib/template-loader'
1616

1717
module.exports = function (content) {
18-
var defaultLoaders = {
19-
html: 'vue-loader/lib/template-compiler',
20-
css: 'vue-style-loader!css-loader',
21-
js: 'babel-loader?presets[]=es2015&plugins[]=transform-runtime&comments=false'
22-
}
23-
2418
this.cacheable()
2519
var loaderContext = this
2620
var options = this.options.vue || {}
@@ -30,6 +24,12 @@ module.exports = function (content) {
3024
var moduleId = '_v-' + hash(filePath)
3125
var styleRewriter = 'vue-loader/lib/style-rewriter?id=' + moduleId
3226

27+
var defaultLoaders = {
28+
html: 'vue-loader/lib/template-compiler?id=' + moduleId,
29+
css: 'vue-style-loader!css-loader',
30+
js: 'babel-loader?presets[]=es2015&plugins[]=transform-runtime&comments=false'
31+
}
32+
3333
// respect user babel options
3434
if (this.options.babel) {
3535
defaultLoaders.js = 'babel-loader'
@@ -127,7 +127,7 @@ module.exports = function (content) {
127127

128128
var parts = parse(content, fileName, this.sourceMap)
129129
var hasScoped = false
130-
var output = ''
130+
var output = 'var __vue_script__\n'
131131

132132
// add requires for styles
133133
parts.styles.forEach(function (style, i) {
@@ -142,7 +142,7 @@ module.exports = function (content) {
142142
var script = parts.script
143143
if (script) {
144144
output +=
145-
'var __vue_script__ = ' + (
145+
'__vue_script__ = ' + (
146146
script.src
147147
? getRequireForImport('script', script)
148148
: getRequire('script', script)
@@ -191,27 +191,26 @@ module.exports = function (content) {
191191
exports +
192192
'module.exports = __exports__'
193193
// hot reload
194-
// if (
195-
// !this.minimize &&
196-
// process.env.NODE_ENV !== 'production' &&
197-
// (parts.script.length || parts.template.length)
198-
// ) {
199-
// output +=
200-
// 'if (module.hot) {(function () {' +
201-
// ' module.hot.accept()\n' +
202-
// ' var hotAPI = require("vue-hot-reload-api")\n' +
203-
// ' hotAPI.install(require("vue"), false)\n' +
204-
// ' if (!hotAPI.compatible) return\n' +
205-
// ' var id = ' + loaderUtils.stringifyRequest(loaderContext, filePath) + '\n' +
206-
// ' if (!module.hot.data) {\n' +
207-
// // initial insert
208-
// ' hotAPI.createRecord(id, module.exports)\n' +
209-
// ' } else {\n' +
210-
// // update
211-
// ' hotAPI.update(id, module.exports, __vue_template__)\n' +
212-
// ' }\n' +
213-
// '})()}'
214-
// }
194+
if (
195+
!this.minimize &&
196+
process.env.NODE_ENV !== 'production' &&
197+
(parts.script || parts.template)
198+
) {
199+
output +=
200+
'\nif (module.hot) {(function () {' +
201+
' var hotAPI = require("vue-hot-reload-api")\n' +
202+
' hotAPI.install(require("vue"), false)\n' +
203+
' if (!hotAPI.compatible) return\n' +
204+
' module.hot.accept()\n' +
205+
' if (!module.hot.data) {\n' +
206+
// initial insert
207+
' hotAPI.createRecord("' + moduleId + '", __exports__)\n' +
208+
' } else {\n' +
209+
// update
210+
' hotAPI.reload("' + moduleId + '", __exports__)\n' +
211+
' }\n' +
212+
'})()}'
213+
}
215214
} else {
216215
output +=
217216
'module.exports = function (injections) {\n' +

lib/template-compiler.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var compiler = require('vue-template-compiler')
2+
var loaderUtils = require('loader-utils')
23

34
// vue compiler module for using file-loader img src
45
var options = {
@@ -27,17 +28,32 @@ function rewrite (attr) {
2728
module.exports = function (html) {
2829
this.cacheable()
2930
var compiled = compiler.compile(html, options)
31+
var code
3032
if (compiled.errors.length) {
3133
var self = this
3234
compiled.errors.forEach(function (err) {
3335
self.emitError('template syntax error ' + err)
3436
})
35-
return 'module.exports={render:function(){},staticRenderFns:[]}'
37+
code = 'module.exports={render:function(){},staticRenderFns:[]}'
38+
} else {
39+
code = 'module.exports={' +
40+
'render:' + toFunction(compiled.render) + ',' +
41+
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
42+
'}'
3643
}
37-
return 'module.exports={' +
38-
'render:' + toFunction(compiled.render) + ',' +
39-
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
40-
'}'
44+
// hot-reload
45+
if (!this.minimize &&
46+
process.env.NODE_ENV !== 'production') {
47+
var moduleId = loaderUtils.parseQuery(this.query).id
48+
code +=
49+
'\nif (module.hot) {\n' +
50+
' module.hot.accept()\n' +
51+
' if (module.hot.data) {\n' +
52+
' require("vue-hot-reload-api").rerender("' + moduleId + '", module.exports)\n' +
53+
' }\n' +
54+
'}'
55+
}
56+
return code
4157
}
4258

4359
function toFunction (code) {

0 commit comments

Comments
 (0)