|
| 1 | +const { parse } = require('@patternfly/ast-helpers'); |
| 2 | + |
| 3 | +/** |
| 4 | + * TypeScript-only names that @types/react declares but the `react` package does not |
| 5 | + * export at runtime. Value-importing them breaks strict ESM linking (e.g. Rspack). |
| 6 | + * @see https://github.com/facebook/react/issues/11503 |
| 7 | + */ |
| 8 | +const REACT_TYPE_ONLY_EXPORTS = new Set([ |
| 9 | + 'AnimationEvent', |
| 10 | + 'AriaAttributes', |
| 11 | + 'BaseSyntheticEvent', |
| 12 | + 'ChangeEvent', |
| 13 | + 'ClipboardEvent', |
| 14 | + 'ComponentClass', |
| 15 | + 'ComponentProps', |
| 16 | + 'ComponentPropsWithRef', |
| 17 | + 'ComponentPropsWithoutRef', |
| 18 | + 'ComponentType', |
| 19 | + 'CompositionEvent', |
| 20 | + 'CSSProperties', |
| 21 | + 'DetailedHTMLProps', |
| 22 | + 'Dispatch', |
| 23 | + 'DOMAttributes', |
| 24 | + 'DragEvent', |
| 25 | + 'ElementType', |
| 26 | + 'ExoticComponent', |
| 27 | + 'FC', |
| 28 | + 'FocusEvent', |
| 29 | + 'FormEvent', |
| 30 | + 'ForwardRefExoticComponent', |
| 31 | + 'FunctionComponent', |
| 32 | + 'HTMLAttributes', |
| 33 | + 'InvalidEvent', |
| 34 | + 'JSXElementConstructor', |
| 35 | + 'KeyboardEvent', |
| 36 | + 'Key', |
| 37 | + 'LazyExoticComponent', |
| 38 | + 'LegacyRef', |
| 39 | + 'MemoExoticComponent', |
| 40 | + 'MouseEvent', |
| 41 | + 'MutableRefObject', |
| 42 | + 'PointerEvent', |
| 43 | + 'PropsWithChildren', |
| 44 | + 'PropsWithoutRef', |
| 45 | + 'PropsWithRef', |
| 46 | + 'ReactChangeEvent', |
| 47 | + 'ReactChild', |
| 48 | + 'ReactElement', |
| 49 | + 'ReactFragment', |
| 50 | + 'ReactFormEvent', |
| 51 | + 'ReactKeyboardEvent', |
| 52 | + 'ReactMouseEvent', |
| 53 | + 'ReactNode', |
| 54 | + 'ReactPointerEvent', |
| 55 | + 'ReactPortal', |
| 56 | + 'ReactText', |
| 57 | + 'Ref', |
| 58 | + 'RefAttributes', |
| 59 | + 'RefCallback', |
| 60 | + 'RefObject', |
| 61 | + 'SetStateAction', |
| 62 | + 'StyleHTMLAttributes', |
| 63 | + 'SVGAttributes', |
| 64 | + 'SVGProps', |
| 65 | + 'SyntheticEvent', |
| 66 | + 'TouchEvent', |
| 67 | + 'TransitionEvent', |
| 68 | + 'UIEvent', |
| 69 | + 'VoidFunctionComponent', |
| 70 | + 'VFC', |
| 71 | + 'WheelEvent', |
| 72 | +]); |
| 73 | + |
| 74 | +function specifierImportedName(spec) { |
| 75 | + if (spec.type !== 'ImportSpecifier') { |
| 76 | + return null; |
| 77 | + } |
| 78 | + const id = spec.imported; |
| 79 | + return id.type === 'Identifier' ? id.name : null; |
| 80 | +} |
| 81 | + |
| 82 | +function importDeclarationToSource(decl, specifiers) { |
| 83 | + const srcLiteral = decl.source.raw || JSON.stringify(decl.source.value); |
| 84 | + if (specifiers.length === 0) { |
| 85 | + return ''; |
| 86 | + } |
| 87 | + |
| 88 | + const def = specifiers.find((s) => s.type === 'ImportDefaultSpecifier'); |
| 89 | + const ns = specifiers.find((s) => s.type === 'ImportNamespaceSpecifier'); |
| 90 | + const named = specifiers.filter((s) => s.type === 'ImportSpecifier'); |
| 91 | + |
| 92 | + if (ns) { |
| 93 | + return `import * as ${ns.local.name} from ${srcLiteral}`; |
| 94 | + } |
| 95 | + |
| 96 | + const formatNamed = () => |
| 97 | + named |
| 98 | + .map((sp) => { |
| 99 | + const imp = sp.imported.name; |
| 100 | + const loc = sp.local.name; |
| 101 | + return imp === loc ? loc : `${imp} as ${loc}`; |
| 102 | + }) |
| 103 | + .join(', '); |
| 104 | + |
| 105 | + if (def && named.length) { |
| 106 | + return `import ${def.local.name}, { ${formatNamed()} } from ${srcLiteral}`; |
| 107 | + } |
| 108 | + if (def) { |
| 109 | + return `import ${def.local.name} from ${srcLiteral}`; |
| 110 | + } |
| 111 | + return `import { ${formatNamed()} } from ${srcLiteral}`; |
| 112 | +} |
| 113 | + |
| 114 | +function filterReactSpecifiers(decl) { |
| 115 | + if (decl.importKind === 'type') { |
| 116 | + return []; |
| 117 | + } |
| 118 | + return decl.specifiers.filter((spec) => { |
| 119 | + if (spec.type === 'ImportDefaultSpecifier' || spec.type === 'ImportNamespaceSpecifier') { |
| 120 | + return true; |
| 121 | + } |
| 122 | + if (spec.type === 'ImportSpecifier' && spec.importKind === 'type') { |
| 123 | + return false; |
| 124 | + } |
| 125 | + const name = specifierImportedName(spec); |
| 126 | + if (!name) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + return !REACT_TYPE_ONLY_EXPORTS.has(name); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Removes TypeScript-only specifiers from `import … from 'react'` in MDX import blocks. |
| 135 | + * MDX often provides many import lines in a single node; this handles multi-statement programs. |
| 136 | + */ |
| 137 | +function stripReactTypeOnlyImports(importSource) { |
| 138 | + const trimmed = importSource.trim(); |
| 139 | + if (!trimmed || !trimmed.includes('react')) { |
| 140 | + return importSource; |
| 141 | + } |
| 142 | + |
| 143 | + const toParse = trimmed.endsWith(';') ? trimmed : `${trimmed};`; |
| 144 | + let ast; |
| 145 | + try { |
| 146 | + ast = parse(toParse); |
| 147 | + } catch { |
| 148 | + return importSource; |
| 149 | + } |
| 150 | + |
| 151 | + if (!ast.body.length) { |
| 152 | + return importSource; |
| 153 | + } |
| 154 | + |
| 155 | + const pureImports = ast.body.every((stmt) => stmt.type === 'ImportDeclaration'); |
| 156 | + if (!pureImports) { |
| 157 | + return stripSingleImportDeclaration(toParse, trimmed, importSource); |
| 158 | + } |
| 159 | + |
| 160 | + const parts = []; |
| 161 | + for (const stmt of ast.body) { |
| 162 | + if (stmt.type !== 'ImportDeclaration') { |
| 163 | + continue; |
| 164 | + } |
| 165 | + const mod = stmt.source && stmt.source.value; |
| 166 | + if (mod !== 'react') { |
| 167 | + parts.push(toParse.slice(stmt.start, stmt.end)); |
| 168 | + continue; |
| 169 | + } |
| 170 | + const filtered = filterReactSpecifiers(stmt); |
| 171 | + if (filtered.length === 0) { |
| 172 | + continue; |
| 173 | + } |
| 174 | + if (filtered.length === stmt.specifiers.length) { |
| 175 | + parts.push(toParse.slice(stmt.start, stmt.end)); |
| 176 | + continue; |
| 177 | + } |
| 178 | + const out = importDeclarationToSource(stmt, filtered); |
| 179 | + if (out) { |
| 180 | + parts.push(out.endsWith(';') ? out : `${out};`); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (parts.length === 0) { |
| 185 | + return ''; |
| 186 | + } |
| 187 | + |
| 188 | + return parts.join('\n'); |
| 189 | +} |
| 190 | + |
| 191 | +/** Previous behavior: one import statement per node. */ |
| 192 | +function stripSingleImportDeclaration(toParse, trimmed, importSource) { |
| 193 | + if (toParse.length === 0) { |
| 194 | + return importSource; |
| 195 | + } |
| 196 | + let ast; |
| 197 | + try { |
| 198 | + ast = parse(toParse); |
| 199 | + } catch { |
| 200 | + return importSource; |
| 201 | + } |
| 202 | + if (ast.body.length !== 1 || ast.body[0].type !== 'ImportDeclaration') { |
| 203 | + return importSource; |
| 204 | + } |
| 205 | + const decl = ast.body[0]; |
| 206 | + if (decl.source.value !== 'react') { |
| 207 | + return importSource; |
| 208 | + } |
| 209 | + if (decl.importKind === 'type') { |
| 210 | + return ''; |
| 211 | + } |
| 212 | + const filtered = filterReactSpecifiers(decl); |
| 213 | + if (filtered.length === decl.specifiers.length) { |
| 214 | + return importSource; |
| 215 | + } |
| 216 | + const out = importDeclarationToSource(decl, filtered); |
| 217 | + if (!out) { |
| 218 | + return ''; |
| 219 | + } |
| 220 | + const hadSemicolon = /;\s*$/.test(trimmed); |
| 221 | + return hadSemicolon ? `${out};` : out; |
| 222 | +} |
| 223 | + |
| 224 | +module.exports = { |
| 225 | + stripReactTypeOnlyImports, |
| 226 | + REACT_TYPE_ONLY_EXPORTS, |
| 227 | +}; |
0 commit comments