Skip to content

Commit

Permalink
fix: improve HMR reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 11, 2018
1 parent 67a0782 commit 4ccd96f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
31 changes: 13 additions & 18 deletions lib/codegen/hotReload.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
const hotReloadAPIPath = JSON.stringify(require.resolve('vue-hot-reload-api'))

exports.genHotReloadCode = (id, functional) => {
return wrap(`
if (!module.hot.data) {
api.createRecord('${id}', component.options)
} else {
api.${functional ? `rerender` : `reload`}('${id}', component.options)
}
`)
}

exports.genTemplateHotReloadCode = id => {
return wrap(`
if (module.hot.data) {
require(${hotReloadAPIPath}).rerender('${id}', {
const genTemplateHotReloadCode = (id, request) => {
return `
module.hot.accept(${request}, function () {
api.rerender('${id}', {
render: render,
staticRenderFns: staticRenderFns
})
}
`)
})
`.trim()
}

function wrap (inner) {
exports.genHotReloadCode = (id, functional, templateRequest) => {
return `
/* hot reload */
if (module.hot) {
var api = require(${hotReloadAPIPath})
api.install(require('vue'))
if (api.compatible) {
module.hot.accept()
${inner.trim()}
if (!module.hot.data) {
api.createRecord('${id}', component.options)
} else {
api.${functional ? `rerender` : `reload`}('${id}', component.options)
}
${templateRequest ? genTemplateHotReloadCode(id, templateRequest) : ``}
}
}
`.trim()
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ module.exports = function (source) {

// template
let templateImport = `var render, staticRenderFns`
let templateRequest
if (descriptor.template) {
const src = descriptor.template.src || resourcePath
const idQuery = `&id=${id}`
const scopedQuery = hasScoped ? `&scoped=true` : ``
const attrsQuery = attrsToQuery(descriptor.template.attrs)
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}`
const request = stringifyRequest(src + query)
const request = templateRequest = stringifyRequest(src + query)
templateImport = `import { render, staticRenderFns } from ${request}`
}

Expand Down Expand Up @@ -145,7 +146,7 @@ var component = normalizer(
}

if (needsHotReload) {
code += `\n` + genHotReloadCode(id, hasFunctional)
code += `\n` + genHotReloadCode(id, hasFunctional, templateRequest)
}

// Expose filename. This is used by the devtools and vue runtime warnings.
Expand Down
12 changes: 2 additions & 10 deletions lib/loaders/templateLoader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const qs = require('querystring')
const loaderUtils = require('loader-utils')
const { compileTemplate } = require('@vue/component-compiler-utils')
const { genTemplateHotReloadCode } = require('../codegen/hotReload')

// Loader that compiles raw template into JavaScript functions.
// This is injected by the global pitcher (../pitch) for template
Expand All @@ -18,7 +17,6 @@ module.exports = function (source) {
const isServer = loaderContext.target === 'node'
const isProduction = loaderContext.minimize || process.env.NODE_ENV === 'production'
const isFunctional = query.functional
const needsHotReload = !isServer && !isProduction && options.hotReload !== false

// allow using custom compiler via options
const compiler = options.compiler || require('vue-template-compiler')
Expand Down Expand Up @@ -59,16 +57,10 @@ module.exports = function (source) {
)
}

let { code } = compiled

// hot-reload
if (needsHotReload) {
code += genTemplateHotReloadCode(id)
}
const { code } = compiled

// finish with ESM exports
code += `export { render, staticRenderFns }`
return code
return code + `\nexport { render, staticRenderFns }`
}

function pad (source) {
Expand Down

0 comments on commit 4ccd96f

Please sign in to comment.