|
| 1 | +import fs from 'fs' |
| 2 | +import { parse, SFCBlock } from '@vue/compiler-sfc' |
| 3 | +import { getDescriptor, setDescriptor } from './utils/descriptorCache' |
| 4 | + |
| 5 | +/** |
| 6 | + * Vite-specific HMR handling |
| 7 | + */ |
| 8 | +export async function handleHotUpdate(file: string, modules: any[]) { |
| 9 | + if (!file.endsWith('.vue')) { |
| 10 | + return |
| 11 | + } |
| 12 | + |
| 13 | + const prevDescriptor = getDescriptor(file) |
| 14 | + if (!prevDescriptor) { |
| 15 | + // file hasn't been requested yet (e.g. async component) |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + let content = fs.readFileSync(file, 'utf-8') |
| 20 | + if (!content) { |
| 21 | + await untilModified(file) |
| 22 | + content = fs.readFileSync(file, 'utf-8') |
| 23 | + } |
| 24 | + |
| 25 | + const { descriptor } = parse(content, { |
| 26 | + filename: file, |
| 27 | + sourceMap: true, |
| 28 | + sourceRoot: process.cwd(), |
| 29 | + }) |
| 30 | + setDescriptor(file, descriptor) |
| 31 | + |
| 32 | + let needRerender = false |
| 33 | + const filteredModules = [] |
| 34 | + |
| 35 | + const reload = () => { |
| 36 | + console.log(`[vue:reload] ${file}`) |
| 37 | + return modules.filter((m) => /type=script/.test(m.id)) |
| 38 | + } |
| 39 | + |
| 40 | + if ( |
| 41 | + !isEqualBlock(descriptor.script, prevDescriptor.script) || |
| 42 | + !isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup) |
| 43 | + ) { |
| 44 | + return reload() |
| 45 | + } |
| 46 | + |
| 47 | + if (!isEqualBlock(descriptor.template, prevDescriptor.template)) { |
| 48 | + needRerender = true |
| 49 | + } |
| 50 | + |
| 51 | + let didUpdateStyle = false |
| 52 | + const prevStyles = prevDescriptor.styles || [] |
| 53 | + const nextStyles = descriptor.styles || [] |
| 54 | + |
| 55 | + // css modules update causes a reload because the $style object is changed |
| 56 | + // and it may be used in JS. It also needs to trigger a vue-style-update |
| 57 | + // event so the client busts the sw cache. |
| 58 | + if ( |
| 59 | + prevStyles.some((s) => s.module != null) || |
| 60 | + nextStyles.some((s) => s.module != null) |
| 61 | + ) { |
| 62 | + return reload() |
| 63 | + } |
| 64 | + |
| 65 | + // force reload if CSS vars injection changed |
| 66 | + if (descriptor.cssVars) { |
| 67 | + if (prevDescriptor.cssVars.join('') !== descriptor.cssVars.join('')) { |
| 68 | + return reload() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // force reload if scoped status has changed |
| 73 | + if (prevStyles.some((s) => s.scoped) !== nextStyles.some((s) => s.scoped)) { |
| 74 | + return reload() |
| 75 | + } |
| 76 | + |
| 77 | + // only need to update styles if not reloading, since reload forces |
| 78 | + // style updates as well. |
| 79 | + nextStyles.forEach((_, i) => { |
| 80 | + if (!prevStyles[i] || !isEqualBlock(prevStyles[i], nextStyles[i])) { |
| 81 | + didUpdateStyle = true |
| 82 | + filteredModules.push(modules.find((m) => m.id.includes(`index=${i}`))) |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + const prevCustoms = prevDescriptor.customBlocks || [] |
| 87 | + const nextCustoms = descriptor.customBlocks || [] |
| 88 | + |
| 89 | + // custom blocks update causes a reload |
| 90 | + // because the custom block contents is changed and it may be used in JS. |
| 91 | + if ( |
| 92 | + nextCustoms.some( |
| 93 | + (_, i) => !prevCustoms[i] || !isEqualBlock(prevCustoms[i], nextCustoms[i]) |
| 94 | + ) |
| 95 | + ) { |
| 96 | + return reload() |
| 97 | + } |
| 98 | + |
| 99 | + if (needRerender) { |
| 100 | + filteredModules.push(modules.find((m) => /type=template/.test(m.id))) |
| 101 | + } |
| 102 | + |
| 103 | + let updateType = [] |
| 104 | + if (needRerender) { |
| 105 | + updateType.push(`template`) |
| 106 | + } |
| 107 | + if (didUpdateStyle) { |
| 108 | + updateType.push(`style`) |
| 109 | + } |
| 110 | + if (updateType.length) { |
| 111 | + console.log(`[vue:update(${updateType.join('&')})] ${file}`) |
| 112 | + } |
| 113 | + return filteredModules |
| 114 | +} |
| 115 | + |
| 116 | +// vitejs/vite#610 when hot-reloading Vue files, we read immediately on file |
| 117 | +// change event and sometimes this can be too early and get an empty buffer. |
| 118 | +// Poll until the file's modified time has changed before reading again. |
| 119 | +async function untilModified(file: string) { |
| 120 | + const mtime = fs.statSync(file).mtimeMs |
| 121 | + return new Promise((r) => { |
| 122 | + let n = 0 |
| 123 | + const poll = async () => { |
| 124 | + n++ |
| 125 | + const newMtime = fs.statSync(file).mtimeMs |
| 126 | + if (newMtime !== mtime || n > 10) { |
| 127 | + r(0) |
| 128 | + } else { |
| 129 | + setTimeout(poll, 10) |
| 130 | + } |
| 131 | + } |
| 132 | + setTimeout(poll, 10) |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) { |
| 137 | + if (!a && !b) return true |
| 138 | + if (!a || !b) return false |
| 139 | + // src imports will trigger their own updates |
| 140 | + if (a.src && b.src && a.src === b.src) return true |
| 141 | + if (a.content !== b.content) return false |
| 142 | + const keysA = Object.keys(a.attrs) |
| 143 | + const keysB = Object.keys(b.attrs) |
| 144 | + if (keysA.length !== keysB.length) { |
| 145 | + return false |
| 146 | + } |
| 147 | + return keysA.every((key) => a.attrs[key] === b.attrs[key]) |
| 148 | +} |
0 commit comments