|
| 1 | +// @ts-check |
| 2 | +const babel = require('@babel/core') |
| 3 | +const jsx = require('@vue/babel-plugin-jsx') |
| 4 | +const importMeta = require('@babel/plugin-syntax-import-meta') |
| 5 | +const hash = require('hash-sum') |
| 6 | + |
| 7 | +/** |
| 8 | + * @param {import('.').Options} options |
| 9 | + * @returns {import('vite').Plugin} |
| 10 | + */ |
| 11 | +module.exports = function vueJsxPlugin(options = {}) { |
| 12 | + let needHmr = false |
| 13 | + |
| 14 | + return { |
| 15 | + name: 'vue-jsx', |
| 16 | + |
| 17 | + config(config) { |
| 18 | + return { |
| 19 | + esbuild: false, |
| 20 | + define: { |
| 21 | + __VUE_OPTIONS_API__: true, |
| 22 | + __VUE_PROD_DEVTOOLS__: false, |
| 23 | + ...config.define |
| 24 | + } |
| 25 | + } |
| 26 | + }, |
| 27 | + |
| 28 | + configResolved(config) { |
| 29 | + needHmr = config.command === 'serve' && !config.isProduction |
| 30 | + }, |
| 31 | + |
| 32 | + transform(code, id) { |
| 33 | + if (/\.[jt]sx$/.test(id)) { |
| 34 | + const plugins = [importMeta, [jsx, options]] |
| 35 | + if (id.endsWith('.tsx')) { |
| 36 | + plugins.push([ |
| 37 | + require('@babel/plugin-transform-typescript'), |
| 38 | + // @ts-ignore |
| 39 | + { isTSX: true, allowExtensions: true } |
| 40 | + ]) |
| 41 | + } |
| 42 | + |
| 43 | + const result = babel.transformSync(code, { |
| 44 | + ast: true, |
| 45 | + plugins, |
| 46 | + sourceMaps: true, |
| 47 | + sourceFileName: id |
| 48 | + }) |
| 49 | + |
| 50 | + if (!needHmr) { |
| 51 | + return { |
| 52 | + code: result.code, |
| 53 | + map: result.map |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // check for hmr injection |
| 58 | + /** |
| 59 | + * @type {{ name: string, hash: string }[]} |
| 60 | + */ |
| 61 | + const declaredComponents = [] |
| 62 | + /** |
| 63 | + * @type {{ |
| 64 | + * local: string, |
| 65 | + * exported: string, |
| 66 | + * id: string, |
| 67 | + * hash: string |
| 68 | + * }[]} |
| 69 | + */ |
| 70 | + const hotComponents = [] |
| 71 | + let hasDefault = false |
| 72 | + |
| 73 | + for (const node of result.ast.program.body) { |
| 74 | + if (node.type === 'VariableDeclaration') { |
| 75 | + const names = parseComponentDecls(node, code) |
| 76 | + if (names.length) { |
| 77 | + declaredComponents.push(...names) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (node.type === 'ExportNamedDeclaration') { |
| 82 | + if ( |
| 83 | + node.declaration && |
| 84 | + node.declaration.type === 'VariableDeclaration' |
| 85 | + ) { |
| 86 | + hotComponents.push( |
| 87 | + ...parseComponentDecls(node.declaration, code).map( |
| 88 | + ({ name, hash: _hash }) => ({ |
| 89 | + local: name, |
| 90 | + exported: name, |
| 91 | + id: hash(id + name), |
| 92 | + hash: _hash |
| 93 | + }) |
| 94 | + ) |
| 95 | + ) |
| 96 | + } else if (node.specifiers.length) { |
| 97 | + for (const spec of node.specifiers) { |
| 98 | + if ( |
| 99 | + spec.type === 'ExportSpecifier' && |
| 100 | + spec.exported.type === 'Identifier' |
| 101 | + ) { |
| 102 | + const matched = declaredComponents.find( |
| 103 | + ({ name }) => name === spec.local.name |
| 104 | + ) |
| 105 | + if (matched) { |
| 106 | + hotComponents.push({ |
| 107 | + local: spec.local.name, |
| 108 | + exported: spec.exported.name, |
| 109 | + id: hash(id + spec.exported.name), |
| 110 | + hash: matched.hash |
| 111 | + }) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (node.type === 'ExportDefaultDeclaration') { |
| 119 | + if (node.declaration.type === 'Identifier') { |
| 120 | + const _name = node.declaration.name |
| 121 | + const matched = declaredComponents.find( |
| 122 | + ({ name }) => name === _name |
| 123 | + ) |
| 124 | + if (matched) { |
| 125 | + hotComponents.push({ |
| 126 | + local: node.declaration.name, |
| 127 | + exported: 'default', |
| 128 | + id: hash(id + 'default'), |
| 129 | + hash: matched.hash |
| 130 | + }) |
| 131 | + } |
| 132 | + } else if (isDefineComponentCall(node.declaration)) { |
| 133 | + hasDefault = true |
| 134 | + hotComponents.push({ |
| 135 | + local: '__default__', |
| 136 | + exported: 'default', |
| 137 | + id: hash(id + 'default'), |
| 138 | + hash: hash( |
| 139 | + code.slice(node.declaration.start, node.declaration.end) |
| 140 | + ) |
| 141 | + }) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (hotComponents.length) { |
| 147 | + let code = result.code |
| 148 | + if (hasDefault) { |
| 149 | + code = |
| 150 | + code.replace( |
| 151 | + /export default defineComponent/g, |
| 152 | + `const __default__ = defineComponent` |
| 153 | + ) + `\nexport default __default__` |
| 154 | + } |
| 155 | + |
| 156 | + let callbackCode = `` |
| 157 | + for (const { local, exported, id, hash } of hotComponents) { |
| 158 | + code += |
| 159 | + `\n${local}.__hmrId = "${id}"` + |
| 160 | + `\n${local}.__hmrHash = "${hash}"` + |
| 161 | + `\n__VUE_HMR_RUNTIME__.createRecord("${id}", ${local})` |
| 162 | + callbackCode += |
| 163 | + `\n if (__${exported}.__hmrHash !== ${local}.__hmrHash) ` + |
| 164 | + `__VUE_HMR_RUNTIME__.reload("${id}", __${exported})` |
| 165 | + } |
| 166 | + |
| 167 | + code += `\nimport.meta.hot.accept(({${hotComponents |
| 168 | + .map((c) => `${c.exported}: __${c.exported}`) |
| 169 | + .join(',')}}) => {${callbackCode}\n})` |
| 170 | + |
| 171 | + result.code = code |
| 172 | + } |
| 173 | + |
| 174 | + return { |
| 175 | + code: result.code, |
| 176 | + map: result.map |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * @param {import('@babel/core').types.VariableDeclaration} node |
| 185 | + * @param {string} source |
| 186 | + */ |
| 187 | +function parseComponentDecls(node, source) { |
| 188 | + const names = [] |
| 189 | + for (const decl of node.declarations) { |
| 190 | + if (decl.id.type === 'Identifier' && isDefineComponentCall(decl.init)) { |
| 191 | + names.push({ |
| 192 | + name: decl.id.name, |
| 193 | + hash: hash(source.slice(decl.init.start, decl.init.end)) |
| 194 | + }) |
| 195 | + } |
| 196 | + } |
| 197 | + return names |
| 198 | +} |
| 199 | + |
| 200 | +/** |
| 201 | + * @param {import('@babel/core').types.Node} node |
| 202 | + */ |
| 203 | +function isDefineComponentCall(node) { |
| 204 | + return ( |
| 205 | + node.type === 'CallExpression' && |
| 206 | + node.callee.type === 'Identifier' && |
| 207 | + node.callee.name === 'defineComponent' |
| 208 | + ) |
| 209 | +} |
0 commit comments