Skip to content

Commit

Permalink
perf: revert special handling for vue hmr
Browse files Browse the repository at this point in the history
this avoids full babel parse on every vue file
  • Loading branch information
yyx990803 committed May 26, 2020
1 parent df526b1 commit 43ccaf7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
16 changes: 16 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ socket.addEventListener('message', async ({ data }) => {
case 'connected':
console.log(`[vite] connected.`)
break
case 'vue-reload':
import(`${path}?t=${timestamp}`)
.then((m) => {
__VUE_HMR_RUNTIME__.reload(path, m.default)
console.log(`[vite] ${path} reloaded.`)
})
.catch((err) => warnFailedFetch(err, path))
break
case 'vue-rerender':
const templatePath = `${path}?type=template`
await bustSwCache(templatePath)
import(`${templatePath}&t=${timestamp}`).then((m) => {
__VUE_HMR_RUNTIME__.rerender(path, m.render)
console.log(`[vite] ${path} template updated.`)
})
break
case 'style-update':
const importQuery = path.includes('?') ? '&import' : '?import'
await bustSwCache(`${path}${importQuery}`)
Expand Down
17 changes: 11 additions & 6 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const hmrClientPublicPath = `/vite/hmr`
interface HMRPayload {
type:
| 'js-update'
| 'vue-reload'
| 'vue-rerender'
| 'style-update'
| 'style-remove'
| 'full-reload'
Expand Down Expand Up @@ -160,14 +162,14 @@ export const hmrPlugin: ServerPlugin = ({
})
console.log(chalk.green(`[vite] `) + `page reloaded.`)
} else {
hmrBoundaries.forEach((jsBoundary) => {
hmrBoundaries.forEach((boundary) => {
console.log(
chalk.green(`[vite:hmr] `) +
`${jsBoundary} updated due to change in ${relativeFile}.`
`${boundary} updated due to change in ${relativeFile}.`
)
send({
type: 'js-update',
path: jsBoundary,
type: boundary.endsWith('vue') ? 'vue-reload' : 'js-update',
path: boundary,
changeSrcPath: publicPath,
timestamp
})
Expand Down Expand Up @@ -214,9 +216,12 @@ function walkImportChain(

let hasDeadEnd = false
for (const importer of importers) {
if (isHmrAccepted(importer, importee)) {
if (importer.endsWith('.vue') || isHmrAccepted(importer, importee)) {
// vue boundaries are considered dirty for the reload
if (importer.endsWith('.vue')) {
dirtyFiles.add(importer)
}
hmrBoundaries.add(importer)
// js boundaries themselves are not considered dirty
currentChain.forEach((file) => dirtyFiles.add(file))
} else {
const parentImpoters = importerMap.get(importer)
Expand Down
5 changes: 4 additions & 1 deletion src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ export const resolveImport = (
// only force re-fetch if this is a marked dirty file (in the import
// chain of the changed file) or a vue part request (made by a dirty
// vue main request)
if ((dirtyFiles && dirtyFiles.has(pathname)) || /\.vue\?type/.test(id)) {
if (
(dirtyFiles && dirtyFiles.has(pathname)) ||
/\?type=(template|style)/.test(id)
) {
query += `${query ? `&` : `?`}t=${timestamp}`
}
}
Expand Down
19 changes: 3 additions & 16 deletions src/node/server/serverPluginVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ export const vuePlugin: ServerPlugin = ({

const sendReload = () => {
send({
type: 'js-update',
type: 'vue-reload',
path: publicPath,
changeSrcPath: publicPath,
timestamp
})
console.log(
Expand Down Expand Up @@ -224,9 +223,8 @@ export const vuePlugin: ServerPlugin = ({

if (needRerender) {
send({
type: 'js-update',
type: 'vue-rerender',
path: publicPath,
changeSrcPath: `${publicPath}?type=template`,
timestamp
})
}
Expand Down Expand Up @@ -363,12 +361,6 @@ async function compileSFCMain(
code += `const __script = {}`
}

code += `\n if (import.meta.hot) {
import.meta.hot.accept((m) => {
__VUE_HMR_RUNTIME__.reload("${id}", m.default)
})
}`

let hasScoped = false
let hasCSSModules = false
if (descriptor.styles) {
Expand Down Expand Up @@ -401,13 +393,8 @@ async function compileSFCMain(
templateRequest
)}`
code += `\n__script.render = __render`
code += `\n if (import.meta.hot) {
import.meta.hot.acceptDeps(${JSON.stringify(templateRequest)}, (m) => {
__VUE_HMR_RUNTIME__.rerender("${id}", m.render)
})
}`
}
code += `\n__script.__hmrId = ${JSON.stringify(id)}`
code += `\n__script.__hmrId = ${JSON.stringify(publicPath)}`
code += `\n__script.__file = ${JSON.stringify(filePath)}`
code += `\nexport default __script`

Expand Down

0 comments on commit 43ccaf7

Please sign in to comment.