Skip to content

Commit 108c1c1

Browse files
committed
feat: basic hmr
1 parent 7351144 commit 108c1c1

File tree

7 files changed

+49
-8
lines changed

7 files changed

+49
-8
lines changed

example/App.vue

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<Button/>
3+
<Button/>
4+
<Button/>
5+
</template>
6+
7+
<script>
8+
import Button from './Button.vue'
9+
10+
export default {
11+
components: {
12+
Button
13+
}
14+
}
15+
</script>

example/source.vue example/Button.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<button @click="inc">{{ count }}</button>
2+
<button @click="inc">{{ count }}!</button>
33
</template>
44

55
<script>

example/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import App from './source.vue'
1+
import App from './App.vue'
22
import { createApp } from 'vue'
33

44
createApp().mount(App, '#app')

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
],
1111
"scripts": {
1212
"build": "tsc",
13-
"dev": "webpack-dev-server --config example/webpack.config.js --inline --hot"
13+
"dev": "webpack-dev-server --config example/webpack.config.js --inline --hot",
14+
"build-example": "webpack --config example/webpack.config.js --mode=production"
1415
},
1516
"peerDependencies": {
1617
"@vue/compiler-sfc": "^3.0.0-alpha.0"

src/hotReload.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// __VUE_HMR_RUNTIME__ is injected to global scope by @vue/runtime-core
2+
3+
export function genHotReloadCode(id: string, templateRequest?: string): string {
4+
return `
5+
/* hot reload */
6+
if (module.hot) {
7+
const api = __VUE_HMR_RUNTIME__
8+
module.hot.accept()
9+
if (!api.isRecorded('${id}')) {
10+
api.createRecord('${id}', script)
11+
} else {
12+
api.reload('${id}', script)
13+
}
14+
${templateRequest ? genTemplateHotReloadCode(id, templateRequest) : ''}
15+
}
16+
`
17+
}
18+
19+
function genTemplateHotReloadCode(id: string, request: string) {
20+
return `
21+
module.hot.accept(${request}, () => {
22+
api.rerender('${id}', render)
23+
})
24+
`
25+
}

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TemplateCompileOptions
1212
} from '@vue/compiler-sfc'
1313
import { selectBlock } from './select'
14+
import { genHotReloadCode } from './hotReload'
1415

1516
const VueLoaderPlugin = require('./plugin')
1617

@@ -147,8 +148,8 @@ const loader: webpack.loader.Loader = function(source) {
147148
}
148149

149150
if (needsHotReload) {
150-
// TODO hot reload
151-
templateRequest
151+
code += `\nscript.__hmrId = "${id}"`
152+
code += genHotReloadCode(id, templateRequest)
152153
}
153154

154155
// Expose filename. This is used by the devtools and Vue runtime warnings.

src/pitcher.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import loaderUtils from 'loader-utils'
44
import hash from 'hash-sum'
55
import { VueLoaderOptions } from 'src'
66

7-
const selfPath = require.resolve('../index')
7+
const selfPath = require.resolve('./index')
88
const templateLoaderPath = require.resolve('./templateLoader')
99
const stylePostLoaderPath = require.resolve('./stylePostLoader')
1010

@@ -163,8 +163,7 @@ pitcher.pitch = function() {
163163
...preLoaders
164164
])
165165
// console.log(request)
166-
// the template compiler uses esm exports
167-
return `export * from ${request}`
166+
return `import mod from ${request}; export default mod;`
168167
}
169168

170169
// if a custom block has no other matching loader other than vue-loader itself

0 commit comments

Comments
 (0)