|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import * as t from '@babel/types' |
| 4 | +import type { ArrayExpression, StringLiteral } from '@babel/types' |
| 5 | +import type { ILengthUnitsPatchDangerousOptions, ILengthUnitsPatchOptions } from './types' |
| 6 | +import { generate, parse, traverse } from '@/babel' |
| 7 | + |
| 8 | +function findAstNode(content: string, options: ILengthUnitsPatchOptions) { |
| 9 | + const DOPTS = options.dangerousOptions as Required<ILengthUnitsPatchDangerousOptions> |
| 10 | + const ast = parse(content) |
| 11 | + |
| 12 | + let arrayRef: ArrayExpression | undefined |
| 13 | + let changed = false |
| 14 | + traverse(ast, { |
| 15 | + Identifier(path) { |
| 16 | + if ( |
| 17 | + path.node.name === DOPTS.variableName |
| 18 | + && t.isVariableDeclarator(path.parent) |
| 19 | + && t.isArrayExpression(path.parent.init) |
| 20 | + ) { |
| 21 | + arrayRef = path.parent.init |
| 22 | + const set = new Set(path.parent.init.elements.map(x => (<StringLiteral>x).value)) |
| 23 | + for (let i = 0; i < options.units.length; i++) { |
| 24 | + const unit = options.units[i] |
| 25 | + if (!set.has(unit)) { |
| 26 | + path.parent.init.elements = path.parent.init.elements.map((x) => { |
| 27 | + if (t.isStringLiteral(x)) { |
| 28 | + return { |
| 29 | + type: x?.type, |
| 30 | + value: x?.value, |
| 31 | + } |
| 32 | + } |
| 33 | + return x |
| 34 | + }) |
| 35 | + path.parent.init.elements.push({ |
| 36 | + type: 'StringLiteral', |
| 37 | + value: unit, |
| 38 | + }) |
| 39 | + changed = true |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + }) |
| 45 | + return { |
| 46 | + arrayRef, |
| 47 | + changed, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILengthUnitsPatchOptions) { |
| 52 | + const { dangerousOptions } = options |
| 53 | + const DOPTS = dangerousOptions as Required<ILengthUnitsPatchDangerousOptions> |
| 54 | + const dataTypesFilePath = path.resolve(rootDir, DOPTS.lengthUnitsFilePath) |
| 55 | + const dataTypesFileContent = fs.readFileSync(dataTypesFilePath, { |
| 56 | + encoding: 'utf8', |
| 57 | + }) |
| 58 | + const { arrayRef, changed } = findAstNode(dataTypesFileContent, options) |
| 59 | + if (arrayRef && changed) { |
| 60 | + const { code } = generate(arrayRef, { |
| 61 | + jsescOption: { |
| 62 | + quotes: 'single', |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + if (arrayRef.start && arrayRef.end) { |
| 67 | + const prev = dataTypesFileContent.slice(0, arrayRef.start) |
| 68 | + const next = dataTypesFileContent.slice(arrayRef.end as number) |
| 69 | + const newCode = prev + code + next |
| 70 | + if (DOPTS.overwrite) { |
| 71 | + fs.writeFileSync(DOPTS.destPath ?? dataTypesFilePath, newCode, { |
| 72 | + encoding: 'utf8', |
| 73 | + }) |
| 74 | + console.log('patch tailwindcss for custom length unit successfully!') |
| 75 | + } |
| 76 | + } |
| 77 | + return code |
| 78 | + } |
| 79 | +} |
0 commit comments