Skip to content

Commit f418bd9

Browse files
committed
feat: basic hot reload
1 parent 878f1b8 commit f418bd9

File tree

4 files changed

+695
-19
lines changed

4 files changed

+695
-19
lines changed

lib/hotReload.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const hotReloadAPIPath = require.resolve('vue-hot-reload-api')
2+
3+
module.exports = function genHotReloadCode (id, functional) {
4+
// TODO handle CSSModules and style injection disposal
5+
return `
6+
/* hot reload */
7+
if (module.hot) {
8+
var api = require('${hotReloadAPIPath}')
9+
api.install(require('vue'))
10+
if (api.compatible) {
11+
module.hot.accept()
12+
if (!module.hot.data) {
13+
api.createRecord('${id}', component.options)
14+
} else {
15+
api.${functional ? `rerender` : `reload`}('${id}', component.options)
16+
}
17+
}
18+
}
19+
`.trim()
20+
}

lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const qs = require('querystring')
55
const loaderUtils = require('loader-utils')
66
const selectBlock = require('./selector')
77
const plugin = require('./plugin')
8+
const genHotReloadCode = require('./hotReload')
89
const componentNormalizerPath = require.resolve('./runtime/componentNormalizer')
910

1011
module.exports = function (source) {
@@ -119,7 +120,7 @@ var component = normalizer(
119120
${isServer ? JSON.stringify(hash(request)) : `null`}
120121
${incomingQuery.shadow ? `,true` : ``}
121122
)
122-
`.trim()
123+
`.trim()
123124

124125
if (descriptor.customBlocks && descriptor.customBlocks.length) {
125126
// TODO custom blocks
@@ -130,7 +131,7 @@ var component = normalizer(
130131
}
131132

132133
if (needsHotReload) {
133-
// TODO hot reload
134+
code += genHotReloadCode(id, hasFunctional)
134135
}
135136

136137
code += `\nexport default component.exports`

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"vue-template-compiler": "^2.5.16",
5555
"webpack": "^4.1.0",
5656
"webpack-cli": "^2.0.10",
57+
"webpack-dev-server": "^3.1.1",
5758
"webpack-merge": "^4.1.2",
5859
"yorkie": "^1.0.3"
5960
},

0 commit comments

Comments
 (0)