|
| 1 | +import * as t from '@babel/types' |
| 2 | +import { generate, parse, traverse } from '@/babel' |
| 3 | +// crash code |
| 4 | + |
| 5 | +export function inspectProcessTailwindFeaturesReturnContext(content: string) { |
| 6 | + const ast = parse(content) |
| 7 | + let hasPatched = false |
| 8 | + traverse(ast, { |
| 9 | + FunctionDeclaration(p) { |
| 10 | + const n = p.node |
| 11 | + if (n.id?.name === 'processTailwindFeatures' && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) { |
| 12 | + const rts = n.body.body[0] |
| 13 | + if (t.isFunctionExpression(rts.argument)) { |
| 14 | + const body = rts.argument.body.body |
| 15 | + const lastStatement = body[body.length - 1] |
| 16 | + hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === 'context' |
| 17 | + if (!hasPatched) { |
| 18 | + // return context; |
| 19 | + const rts = t.returnStatement(t.identifier('context')) |
| 20 | + body.push(rts) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + }, |
| 25 | + }) |
| 26 | + |
| 27 | + return { |
| 28 | + code: hasPatched ? content : generate(ast).code, |
| 29 | + hasPatched, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export function inspectPostcssPlugin(content: string) { |
| 34 | + const ast = parse(content) |
| 35 | + const exportKey = 'contextRef' |
| 36 | + const variableName = 'contextRef' |
| 37 | + const valueKey = 'value' |
| 38 | + let hasPatched = false |
| 39 | + traverse(ast, { |
| 40 | + Program(p) { |
| 41 | + const n = p.node |
| 42 | + // find module.exports = function tailwindcss(configOrPath) |
| 43 | + const idx = n.body.findIndex((x) => { |
| 44 | + return ( |
| 45 | + t.isExpressionStatement(x) |
| 46 | + && t.isAssignmentExpression(x.expression) |
| 47 | + && t.isMemberExpression(x.expression.left) |
| 48 | + && t.isFunctionExpression(x.expression.right) |
| 49 | + && x.expression.right.id?.name === 'tailwindcss' |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + if (idx > -1) { |
| 54 | + const prevStatement = n.body[idx - 1] |
| 55 | + const lastStatement = n.body[n.body.length - 1] |
| 56 | + const hasPatchedCondition0 |
| 57 | + = prevStatement |
| 58 | + && t.isVariableDeclaration(prevStatement) |
| 59 | + && prevStatement.declarations.length === 1 |
| 60 | + && t.isIdentifier(prevStatement.declarations[0].id) |
| 61 | + && prevStatement.declarations[0].id.name === variableName |
| 62 | + const hasPatchedCondition1 |
| 63 | + = t.isExpressionStatement(lastStatement) |
| 64 | + && t.isAssignmentExpression(lastStatement.expression) |
| 65 | + && t.isIdentifier(lastStatement.expression.right) |
| 66 | + && lastStatement.expression.right.name === variableName |
| 67 | + |
| 68 | + hasPatched = hasPatchedCondition0 || hasPatchedCondition1 |
| 69 | + if (!hasPatched) { |
| 70 | + // const contextRef = { |
| 71 | + // value: [] |
| 72 | + // }; |
| 73 | + const statement = t.variableDeclaration('const', [ |
| 74 | + t.variableDeclarator(t.identifier(variableName), t.objectExpression([t.objectProperty(t.identifier(valueKey), t.arrayExpression())])), |
| 75 | + ]) |
| 76 | + n.body.splice(idx, 0, statement) |
| 77 | + // module.exports.contextRef = contextRef; |
| 78 | + n.body.push( |
| 79 | + t.expressionStatement( |
| 80 | + t.assignmentExpression( |
| 81 | + '=', |
| 82 | + t.memberExpression(t.memberExpression(t.identifier('module'), t.identifier('exports')), t.identifier(exportKey)), |
| 83 | + t.identifier(variableName), |
| 84 | + ), |
| 85 | + ), |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + }, |
| 90 | + FunctionExpression(p) { |
| 91 | + if (hasPatched) { |
| 92 | + return |
| 93 | + } |
| 94 | + const n = p.node |
| 95 | + if (n.id?.name === 'tailwindcss' && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) { |
| 96 | + const returnStatement = n.body.body[0] |
| 97 | + if (t.isObjectExpression(returnStatement.argument) && returnStatement.argument.properties.length === 2) { |
| 98 | + const properties = returnStatement.argument.properties |
| 99 | + if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) { |
| 100 | + const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === 'postcssPlugin' |
| 101 | + const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === 'plugins' |
| 102 | + if ( |
| 103 | + pluginsMatched |
| 104 | + && keyMatched |
| 105 | + && t.isCallExpression(properties[1].value) |
| 106 | + && t.isMemberExpression(properties[1].value.callee) |
| 107 | + && t.isArrayExpression(properties[1].value.callee.object) |
| 108 | + ) { |
| 109 | + const pluginsCode = properties[1].value.callee.object.elements |
| 110 | + if (pluginsCode[1] && t.isFunctionExpression(pluginsCode[1])) { |
| 111 | + const targetBlockStatement = pluginsCode[1].body |
| 112 | + |
| 113 | + const lastStatement = targetBlockStatement.body[targetBlockStatement.body.length - 1] |
| 114 | + if (t.isExpressionStatement(lastStatement)) { |
| 115 | + // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root, result)); |
| 116 | + const newExpressionStatement = t.expressionStatement( |
| 117 | + t.callExpression( |
| 118 | + t.memberExpression( |
| 119 | + t.memberExpression(t.identifier(variableName), t.identifier('value')), |
| 120 | + |
| 121 | + t.identifier('push'), |
| 122 | + ), |
| 123 | + |
| 124 | + [lastStatement.expression], |
| 125 | + ), |
| 126 | + ) |
| 127 | + targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement |
| 128 | + } |
| 129 | + |
| 130 | + const ifIdx = targetBlockStatement.body.findIndex(x => t.isIfStatement(x)) |
| 131 | + if (ifIdx > -1) { |
| 132 | + const ifRoot = <t.IfStatement>targetBlockStatement.body[ifIdx] |
| 133 | + if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) { |
| 134 | + const forOf: t.ForOfStatement = ifRoot.consequent.body[1] |
| 135 | + if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) { |
| 136 | + const if2: t.IfStatement = forOf.body.body[0] |
| 137 | + if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) { |
| 138 | + const target = if2.consequent.body[0] |
| 139 | + // contextRef.value.push((0, _processTailwindFeatures.default)(context)(root1, result)); |
| 140 | + const newExpressionStatement = t.expressionStatement( |
| 141 | + t.callExpression(t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier('value')), t.identifier('push')), [target.expression]), |
| 142 | + ) |
| 143 | + if2.consequent.body[0] = newExpressionStatement |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + // clear contextRef.value |
| 149 | + targetBlockStatement.body.unshift( |
| 150 | + // contentRef.value = [] |
| 151 | + // t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression())) |
| 152 | + |
| 153 | + // contentRef.value.length = 0 |
| 154 | + t.expressionStatement( |
| 155 | + t.assignmentExpression( |
| 156 | + '=', |
| 157 | + t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.identifier('length')), |
| 158 | + t.numericLiteral(0), |
| 159 | + ), |
| 160 | + ), |
| 161 | + ) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + // start = true |
| 168 | + }, |
| 169 | + // BlockStatement(p) { |
| 170 | + // const n = p.node |
| 171 | + // if (start && p.parent.type === 'FunctionExpression' && !p.parent.id) { |
| 172 | + // n.body.unshift(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))) |
| 173 | + // } |
| 174 | + // } |
| 175 | + }) |
| 176 | + return { |
| 177 | + code: hasPatched ? content : generate(ast).code, |
| 178 | + hasPatched, |
| 179 | + } |
| 180 | +} |
0 commit comments