|
| 1 | +/** |
| 2 | + * @typedef {import('estree-jsx').Node} Node |
| 3 | + * @typedef {import('estree-jsx').Program} Program |
| 4 | + * @typedef {import('estree-jsx').Expression} Expression |
| 5 | + * @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration |
| 6 | + * @typedef {any} Options |
| 7 | + */ |
| 8 | + |
| 9 | +import { visit, EXIT, SKIP } from 'estree-util-visit' |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {Node} node |
| 13 | + * @returns {node is Program} |
| 14 | + */ |
| 15 | +function isProgram(node) { |
| 16 | + return node.type === 'Program' |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * A plugin to add Astro Fragment to the final component |
| 21 | + * |
| 22 | + * @type {import('unified').Plugin<[], Program>} |
| 23 | + */ |
| 24 | +export function addAstroFragment() { |
| 25 | + return (tree) => { |
| 26 | + // console.log(inspect(tree, false, null, true)) |
| 27 | + tree.body.unshift({ |
| 28 | + type: 'ImportDeclaration', |
| 29 | + specifiers: [ |
| 30 | + { |
| 31 | + type: 'ImportDefaultSpecifier', |
| 32 | + local: { |
| 33 | + type: 'Identifier', |
| 34 | + name: 'Fragment', |
| 35 | + }, |
| 36 | + }, |
| 37 | + ], |
| 38 | + source: { |
| 39 | + type: 'Literal', |
| 40 | + value: 'astro/jsx-runtime', |
| 41 | + }, |
| 42 | + }) |
| 43 | + |
| 44 | + /** @type {import('estree-jsx').ExportNamedDeclaration} */ |
| 45 | + const code = { |
| 46 | + type: 'ExportNamedDeclaration', |
| 47 | + declaration: { |
| 48 | + type: 'VariableDeclaration', |
| 49 | + declarations: [ |
| 50 | + { |
| 51 | + type: 'VariableDeclarator', |
| 52 | + id: { |
| 53 | + type: 'Identifier', |
| 54 | + name: 'Content', |
| 55 | + }, |
| 56 | + init: { |
| 57 | + type: 'ArrowFunctionExpression', |
| 58 | + expression: true, |
| 59 | + generator: false, |
| 60 | + async: false, |
| 61 | + params: [ |
| 62 | + { |
| 63 | + type: 'AssignmentPattern', |
| 64 | + left: { |
| 65 | + type: 'Identifier', |
| 66 | + name: 'props', |
| 67 | + }, |
| 68 | + right: { |
| 69 | + type: 'ObjectExpression', |
| 70 | + properties: [], |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + body: { |
| 75 | + type: 'CallExpression', |
| 76 | + callee: { |
| 77 | + type: 'Identifier', |
| 78 | + name: 'OrgContent', |
| 79 | + }, |
| 80 | + arguments: [ |
| 81 | + { |
| 82 | + type: 'ObjectExpression', |
| 83 | + properties: [ |
| 84 | + { |
| 85 | + type: 'SpreadElement', |
| 86 | + argument: { |
| 87 | + type: 'Identifier', |
| 88 | + name: 'props', |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + type: 'Property', |
| 93 | + method: false, |
| 94 | + shorthand: false, |
| 95 | + computed: false, |
| 96 | + key: { |
| 97 | + type: 'Identifier', |
| 98 | + name: 'components', |
| 99 | + }, |
| 100 | + value: { |
| 101 | + type: 'ObjectExpression', |
| 102 | + properties: [ |
| 103 | + { |
| 104 | + type: 'Property', |
| 105 | + method: false, |
| 106 | + shorthand: true, |
| 107 | + computed: false, |
| 108 | + key: { |
| 109 | + type: 'Identifier', |
| 110 | + name: 'Fragment', |
| 111 | + }, |
| 112 | + kind: 'init', |
| 113 | + value: { |
| 114 | + type: 'Identifier', |
| 115 | + name: 'Fragment', |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + type: 'SpreadElement', |
| 120 | + argument: { |
| 121 | + type: 'MemberExpression', |
| 122 | + object: { |
| 123 | + type: 'Identifier', |
| 124 | + name: 'props', |
| 125 | + }, |
| 126 | + property: { |
| 127 | + type: 'Identifier', |
| 128 | + name: 'components', |
| 129 | + }, |
| 130 | + computed: false, |
| 131 | + optional: false, |
| 132 | + }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + kind: 'init', |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + ], |
| 141 | + optional: false, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + ], |
| 146 | + kind: 'const', |
| 147 | + }, |
| 148 | + specifiers: [], |
| 149 | + } |
| 150 | + |
| 151 | + visit(tree, (node, _key, index, ancestors) => { |
| 152 | + if (ancestors.length === 0 || index === null) return |
| 153 | + const parent = ancestors[ancestors.length - 1] |
| 154 | + if (!isProgram(parent)) return SKIP |
| 155 | + if ( |
| 156 | + node.type === 'ExportDefaultDeclaration' && |
| 157 | + node.declaration.type === 'Identifier' |
| 158 | + ) { |
| 159 | + node.declaration.name = 'Content' |
| 160 | + parent.body.splice(index, 0, code) |
| 161 | + return EXIT |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments