|
1 | 1 | var loaderUtils = require('loader-utils')
|
2 | 2 | var selectorPath = require.resolve('./selector')
|
3 | 3 | var parserPath = require.resolve('./parser')
|
| 4 | +var hotShimPath = require.resolve('./hot-shim') |
4 | 5 |
|
5 | 6 | var defaultLang = {
|
6 | 7 | template: 'html',
|
@@ -29,19 +30,23 @@ module.exports = function (content) {
|
29 | 30 |
|
30 | 31 | function getRequire (type, part, index, scoped) {
|
31 | 32 | return 'require(' +
|
32 |
| - loaderUtils.stringifyRequest(self, |
33 |
| - // disable system loaders (except post loaders) |
34 |
| - '-!' + |
35 |
| - // get loader string for pre-processors |
36 |
| - getLoaderString(type, part, scoped) + |
37 |
| - // select the corresponding part from the vue file |
38 |
| - getSelectorString(type, index || 0) + |
39 |
| - // the url to the actual vuefile |
40 |
| - vueUrl |
41 |
| - ) + |
| 33 | + getRequireString(type, part, index, scoped) + |
42 | 34 | ')\n'
|
43 | 35 | }
|
44 | 36 |
|
| 37 | + function getRequireString (type, part, index, scoped) { |
| 38 | + return loaderUtils.stringifyRequest(self, |
| 39 | + // disable system loaders (except post loaders) |
| 40 | + '-!' + |
| 41 | + // get loader string for pre-processors |
| 42 | + getLoaderString(type, part, scoped) + |
| 43 | + // select the corresponding part from the vue file |
| 44 | + getSelectorString(type, index || 0) + |
| 45 | + // the url to the actual vuefile |
| 46 | + vueUrl |
| 47 | + ) |
| 48 | + } |
| 49 | + |
45 | 50 | function getRequireForImport (impt) {
|
46 | 51 | return 'require(' +
|
47 | 52 | loaderUtils.stringifyRequest(self,
|
@@ -113,6 +118,44 @@ module.exports = function (content) {
|
113 | 118 | getRequire('template', parts.template[0], 0, hasLocalStyles)
|
114 | 119 | }
|
115 | 120 |
|
| 121 | + // hot reload |
| 122 | + if (parts.script.length || parts.template.length) { |
| 123 | + var scriptString = parts.script.length ? getRequireString('script', parts.script[0], 0) : '' |
| 124 | + var templateString = parts.template.length ? getRequireString('template', parts.template[0], 0, hasLocalStyles) : '' |
| 125 | + var accepted = [] |
| 126 | + if (scriptString) { |
| 127 | + accepted.push(scriptString.slice(1, -1)) |
| 128 | + } |
| 129 | + if (templateString) { |
| 130 | + accepted.push(templateString.slice(1, -1)) |
| 131 | + } |
| 132 | + output += |
| 133 | + 'if (module.hot) {\n' + |
| 134 | + '(function () {\n' + |
| 135 | + // shim the component directive so that it |
| 136 | + // registers the instances |
| 137 | + 'var Vue = require("vue")\n' + |
| 138 | + 'require("' + hotShimPath + '")(Vue)\n' + |
| 139 | + 'var map = Vue.config._hotComponents\n' + |
| 140 | + 'var id = module.exports.hotID = ' + (scriptString || templateString) + '\n' + |
| 141 | + 'module.hot.accept(' + JSON.stringify(accepted) + ', function () {\n' + |
| 142 | + 'var Ctor = map[id].Ctor\n' + |
| 143 | + // overwrite existing constructor's options |
| 144 | + (scriptString ? 'Ctor.options = Vue.util.mergeOptions(Vue.options, require(' + scriptString + '))\n' : '') + |
| 145 | + (templateString ? 'Ctor.options.template = require(' + templateString + ')\n' : '') + |
| 146 | + // reset linker |
| 147 | + 'Ctor.linker = null\n' + |
| 148 | + // reload directive instances |
| 149 | + 'map[id].instances.forEach(function (view) {\n' + |
| 150 | + 'var state = view.childVM.$data\n' + |
| 151 | + 'view.mountComponent()\n' + |
| 152 | + 'view.childVM.$data = state\n' + |
| 153 | + '})\n' + |
| 154 | + '})\n' + |
| 155 | + '})()\n' + |
| 156 | + '}' |
| 157 | + } |
| 158 | + |
116 | 159 | // done
|
117 | 160 | cb(null, output)
|
118 | 161 | })
|
|
0 commit comments