|
| 1 | +import getHash from 'hash-sum' |
| 2 | +import { normalizePath } from 'unplugin-utils' |
| 3 | +import type { BabelFileResult, types } from '@babel/core' |
| 4 | + |
| 5 | +interface HotComponent { |
| 6 | + local: string |
| 7 | + exported: string |
| 8 | + id: string |
| 9 | +} |
| 10 | + |
| 11 | +export function registerHMR( |
| 12 | + result: BabelFileResult, |
| 13 | + id: string, |
| 14 | + defineComponentName = ['defineComponent', 'defineVaporComponent'], |
| 15 | +) { |
| 16 | + const { ast } = result |
| 17 | + |
| 18 | + // check for hmr injection |
| 19 | + const declaredComponents: string[] = [] |
| 20 | + const hotComponents: HotComponent[] = [] |
| 21 | + let defaultName = '' |
| 22 | + const ssr = false |
| 23 | + |
| 24 | + for (const node of ast!.program.body) { |
| 25 | + if (node.type === 'VariableDeclaration') { |
| 26 | + const names = parseComponentDecls(node, defineComponentName) |
| 27 | + if (names.length) { |
| 28 | + declaredComponents.push(...names) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (node.type === 'ExportNamedDeclaration') { |
| 33 | + if (node.declaration && node.declaration.type === 'VariableDeclaration') { |
| 34 | + hotComponents.push( |
| 35 | + ...parseComponentDecls(node.declaration, defineComponentName).map( |
| 36 | + (name) => ({ |
| 37 | + local: name, |
| 38 | + exported: name, |
| 39 | + id: getHash(id + name), |
| 40 | + }), |
| 41 | + ), |
| 42 | + ) |
| 43 | + } else if (node.specifiers.length) { |
| 44 | + for (const spec of node.specifiers) { |
| 45 | + if ( |
| 46 | + spec.type === 'ExportSpecifier' && |
| 47 | + spec.exported.type === 'Identifier' |
| 48 | + ) { |
| 49 | + const matched = declaredComponents.find( |
| 50 | + (name) => name === spec.local.name, |
| 51 | + ) |
| 52 | + if (matched) { |
| 53 | + hotComponents.push({ |
| 54 | + local: spec.local.name, |
| 55 | + exported: spec.exported.name, |
| 56 | + id: getHash(id + spec.exported.name), |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (node.type === 'ExportDefaultDeclaration') { |
| 65 | + if (node.declaration.type === 'Identifier') { |
| 66 | + const _name = node.declaration.name |
| 67 | + const matched = declaredComponents.find((name) => name === _name) |
| 68 | + if (matched) { |
| 69 | + hotComponents.push({ |
| 70 | + local: _name, |
| 71 | + exported: 'default', |
| 72 | + id: getHash(`${id}default`), |
| 73 | + }) |
| 74 | + } |
| 75 | + } else if (isDefineComponentCall(node.declaration, defineComponentName)) { |
| 76 | + defaultName = (node.declaration.callee as any).name |
| 77 | + hotComponents.push({ |
| 78 | + local: '__default__', |
| 79 | + exported: 'default', |
| 80 | + id: getHash(`${id}default`), |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (hotComponents.length) { |
| 87 | + if (defaultName || ssr) { |
| 88 | + result.code = `${result.code!.replaceAll( |
| 89 | + `export default ${defaultName}`, |
| 90 | + `const __default__ = ${defaultName}`, |
| 91 | + )}\nexport default __default__` |
| 92 | + } |
| 93 | + |
| 94 | + if (!ssr && !/\?vue&type=script/.test(id)) { |
| 95 | + let code = result.code |
| 96 | + let callbackCode = `` |
| 97 | + for (const { local, exported, id } of hotComponents) { |
| 98 | + code += |
| 99 | + `\n${local}.__hmrId = "${id}"` + |
| 100 | + `\n__VUE_HMR_RUNTIME__.createRecord("${id}", ${local})` |
| 101 | + callbackCode += ` |
| 102 | + if (mod._rerender_only) { |
| 103 | + __VUE_HMR_RUNTIME__.rerender(mod['${exported}'].__hmrId, mod['${exported}'].setup) |
| 104 | + } else { |
| 105 | + __VUE_HMR_RUNTIME__.reload(mod['${exported}'].__hmrId, mod['${exported}']) |
| 106 | + }` |
| 107 | + } |
| 108 | + |
| 109 | + code += `\nexport const _rerender_only = __VUE_HMR_RUNTIME__.CHANGED_FILE === ${JSON.stringify(normalizePath(id))}` |
| 110 | + code += ` |
| 111 | +if (import.meta.hot) { |
| 112 | + import.meta.hot.on('file-changed', ({ file }) => { |
| 113 | + __VUE_HMR_RUNTIME__.CHANGED_FILE = file |
| 114 | + }) |
| 115 | + import.meta.hot.accept((mod) => {${callbackCode}\n}) |
| 116 | +}` |
| 117 | + result.code = code |
| 118 | + } |
| 119 | + |
| 120 | + // if (ssr) { |
| 121 | + // const normalizedId = normalizePath(path.relative(root, id)) |
| 122 | + // let ssrInjectCode = |
| 123 | + // `\nimport { ssrRegisterHelper } from "${ssrRegisterHelperId}"` + |
| 124 | + // `\nconst __moduleId = ${JSON.stringify(normalizedId)}` |
| 125 | + // for (const { local } of hotComponents) { |
| 126 | + // ssrInjectCode += `\nssrRegisterHelper(${local}, __moduleId)` |
| 127 | + // } |
| 128 | + // result.code += ssrInjectCode |
| 129 | + // } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function parseComponentDecls( |
| 134 | + node: types.VariableDeclaration, |
| 135 | + fnNames: string[], |
| 136 | +) { |
| 137 | + const names = [] |
| 138 | + for (const decl of node.declarations) { |
| 139 | + if ( |
| 140 | + decl.id.type === 'Identifier' && |
| 141 | + isDefineComponentCall(decl.init, fnNames) |
| 142 | + ) { |
| 143 | + names.push(decl.id.name) |
| 144 | + } |
| 145 | + } |
| 146 | + return names |
| 147 | +} |
| 148 | + |
| 149 | +function isDefineComponentCall( |
| 150 | + node: types.Node | null | undefined, |
| 151 | + names: string[], |
| 152 | +): node is types.CallExpression { |
| 153 | + return !!( |
| 154 | + node && |
| 155 | + node.type === 'CallExpression' && |
| 156 | + node.callee.type === 'Identifier' && |
| 157 | + names.includes(node.callee.name) |
| 158 | + ) |
| 159 | +} |
0 commit comments