|
| 1 | +import { extractAssignedNames } from "@rollup/pluginutils"; |
| 2 | +import type { Node, Program } from "estree"; |
| 3 | +import MagicString from "magic-string"; |
| 4 | +import type { Plugin } from "vite"; |
| 5 | +import { walk } from "zimmerframe"; |
| 6 | + |
| 7 | +import type { RollupFilter } from "./utils.js"; |
| 8 | +import { parseId } from "./utils.js"; |
| 9 | + |
| 10 | +const HOOK_IMPORTS = new Set(["preact/hooks", "preact/compat", "react"]); |
| 11 | +const HOOK_NAME_RE = /^(useState|useReducer|useRef|useMemo)$/; |
| 12 | +const FILTER_CODE_RE = /\buse(?:State|Reducer|Ref|Memo)\b/; |
| 13 | + |
| 14 | +type NodeWithRange<N extends Node = Node> = N & { |
| 15 | + start: number; |
| 16 | + end: number; |
| 17 | +}; |
| 18 | + |
| 19 | +export interface TransformHookNamesPluginOptions { |
| 20 | + devtoolsInProd?: boolean; |
| 21 | + devToolsEnabled?: boolean; |
| 22 | + shouldTransform: RollupFilter; |
| 23 | +} |
| 24 | + |
| 25 | +export function transformHookNamesPlugin({ |
| 26 | + devtoolsInProd, |
| 27 | + devToolsEnabled, |
| 28 | + shouldTransform, |
| 29 | +}: TransformHookNamesPluginOptions): Plugin { |
| 30 | + return { |
| 31 | + name: "preact:transform-hook-names", |
| 32 | + configResolved(config) { |
| 33 | + devToolsEnabled = |
| 34 | + devToolsEnabled ?? (!config.isProduction || devtoolsInProd); |
| 35 | + }, |
| 36 | + transform(code, url) { |
| 37 | + if (!devToolsEnabled) return; |
| 38 | + |
| 39 | + const { id } = parseId(url); |
| 40 | + if (!shouldTransform(id)) return; |
| 41 | + if (!FILTER_CODE_RE.test(code)) return; |
| 42 | + |
| 43 | + let ast: NodeWithRange<Program>; |
| 44 | + try { |
| 45 | + ast = this.parse(code) as NodeWithRange<Program>; |
| 46 | + } catch { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const importedHooks = getImportedHooks(ast); |
| 51 | + if (importedHooks.size === 0) return; |
| 52 | + |
| 53 | + const s = new MagicString(code); |
| 54 | + let hasHelper = false; |
| 55 | + walk<NodeWithRange, null>(ast, null, { |
| 56 | + CallExpression(node, { path, next }) { |
| 57 | + const callee = node.callee; |
| 58 | + if (callee.type !== "Identifier") { |
| 59 | + next(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const hookName = callee.name; |
| 64 | + // the hook name might be shadowed by a local variable, |
| 65 | + // but that false-positive is fine |
| 66 | + // since `addHookName` is transparent |
| 67 | + if (!HOOK_NAME_RE.test(hookName) || !importedHooks.has(hookName)) { |
| 68 | + next(); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const parent = path[path.length - 1]; |
| 73 | + const bindingNames = getOuterBindingNames(parent); |
| 74 | + if (bindingNames.length === 0) { |
| 75 | + next(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + let name = bindingNames[0]; |
| 80 | + hasHelper = true; |
| 81 | + s.prependLeft(node.start, "addHookName("); |
| 82 | + s.appendRight(node.end, `, ${JSON.stringify(name)})`); |
| 83 | + next(); |
| 84 | + }, |
| 85 | + }); |
| 86 | + if (!hasHelper) return; |
| 87 | + |
| 88 | + const firstNode = ast.body[0] as NodeWithRange; |
| 89 | + s.prependLeft( |
| 90 | + firstNode.start, |
| 91 | + 'import { addHookName } from "preact/devtools";\n', |
| 92 | + ); |
| 93 | + |
| 94 | + return { |
| 95 | + code: s.toString(), |
| 96 | + map: s.generateMap({ hires: "boundary" }), |
| 97 | + }; |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function getImportedHooks(ast: NodeWithRange<Program>): Set<string> { |
| 103 | + const hooks = new Set<string>(); |
| 104 | + for (const node of ast.body) { |
| 105 | + if (node.type !== "ImportDeclaration") continue; |
| 106 | + |
| 107 | + const source = |
| 108 | + typeof node.source.value === "string" ? node.source.value : null; |
| 109 | + if (!source || !HOOK_IMPORTS.has(source)) continue; |
| 110 | + |
| 111 | + for (const specifier of node.specifiers) { |
| 112 | + if (specifier.type !== "ImportSpecifier") continue; |
| 113 | + |
| 114 | + const importedName = specifier.imported.name; |
| 115 | + if ( |
| 116 | + !HOOK_NAME_RE.test(importedName) || |
| 117 | + specifier.local.name !== importedName |
| 118 | + ) |
| 119 | + continue; |
| 120 | + |
| 121 | + hooks.add(importedName); |
| 122 | + } |
| 123 | + } |
| 124 | + return hooks; |
| 125 | +} |
| 126 | + |
| 127 | +function getOuterBindingNames(node: Node | undefined): string[] { |
| 128 | + if (!node) return []; |
| 129 | + |
| 130 | + switch (node.type) { |
| 131 | + case "VariableDeclarator": |
| 132 | + return extractAssignedNames(node.id); |
| 133 | + case "AssignmentExpression": |
| 134 | + return extractAssignedNames(node.left); |
| 135 | + case "Identifier": |
| 136 | + case "ArrayPattern": |
| 137 | + case "ObjectPattern": |
| 138 | + case "RestElement": |
| 139 | + case "AssignmentPattern": |
| 140 | + return extractAssignedNames(node); |
| 141 | + default: |
| 142 | + return []; |
| 143 | + } |
| 144 | +} |
0 commit comments