From ddd37e6d4ad0b9f219bb70272cae987dee516988 Mon Sep 17 00:00:00 2001 From: ice breaker <1324318532@qq.com> Date: Wed, 17 Jul 2024 21:09:39 +0800 Subject: [PATCH] feat: add applyPatches options --- .../core/patches/supportCustomUnits/index.ts | 30 +++++++++++-------- .../core/patches/supportCustomUnits/types.ts | 12 ++------ .../src/core/runtime-patcher.ts | 28 +++++++++-------- .../tailwindcss-patch/src/core/tw-patcher.ts | 2 +- packages/tailwindcss-patch/src/defaults.ts | 4 +++ packages/tailwindcss-patch/src/types.ts | 6 +--- .../test/__snapshots__/defaults.test.ts.snap | 4 +++ packages/tailwindcss-patch/test/index.test.ts | 4 +++ 8 files changed, 50 insertions(+), 40 deletions(-) diff --git a/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts b/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts index 15c945f..ced778b 100644 --- a/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts +++ b/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts @@ -2,11 +2,12 @@ import fs from 'node:fs' import path from 'node:path' import * as t from '@babel/types' import type { ArrayExpression, StringLiteral } from '@babel/types' -import type { ILengthUnitsPatchDangerousOptions, ILengthUnitsPatchOptions } from './types' +import { defu } from 'defu' +import type { ILengthUnitsPatchOptions } from './types' import { generate, parse, traverse } from '@/babel' function findAstNode(content: string, options: ILengthUnitsPatchOptions) { - const DOPTS = options.dangerousOptions as Required + const { variableName, units } = options const ast = parse(content) let arrayRef: ArrayExpression | undefined @@ -14,14 +15,14 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) { traverse(ast, { Identifier(path) { if ( - path.node.name === DOPTS.variableName + path.node.name === variableName && t.isVariableDeclarator(path.parent) && t.isArrayExpression(path.parent.init) ) { arrayRef = path.parent.init const set = new Set(path.parent.init.elements.map(x => (x).value)) - for (let i = 0; i < options.units.length; i++) { - const unit = options.units[i] + for (let i = 0; i < units.length; i++) { + const unit = units[i] if (!set.has(unit)) { path.parent.init.elements = path.parent.init.elements.map((x) => { if (t.isStringLiteral(x)) { @@ -48,14 +49,19 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) { } } -export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILengthUnitsPatchOptions) { - const { dangerousOptions } = options - const DOPTS = dangerousOptions as Required - const dataTypesFilePath = path.resolve(rootDir, DOPTS.lengthUnitsFilePath) +export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: ILengthUnitsPatchOptions) { + const opts = defu, ILengthUnitsPatchOptions[]>(options, { + units: ['rpx'], + lengthUnitsFilePath: 'lib/util/dataTypes.js', + variableName: 'lengthUnits', + overwrite: true, + }) + const { lengthUnitsFilePath, overwrite, destPath } = opts + const dataTypesFilePath = path.resolve(rootDir, lengthUnitsFilePath) const dataTypesFileContent = fs.readFileSync(dataTypesFilePath, { encoding: 'utf8', }) - const { arrayRef, changed } = findAstNode(dataTypesFileContent, options) + const { arrayRef, changed } = findAstNode(dataTypesFileContent, opts) if (arrayRef && changed) { const { code } = generate(arrayRef, { jsescOption: { @@ -67,8 +73,8 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILe const prev = dataTypesFileContent.slice(0, arrayRef.start) const next = dataTypesFileContent.slice(arrayRef.end as number) const newCode = prev + code + next - if (DOPTS.overwrite) { - fs.writeFileSync(DOPTS.destPath ?? dataTypesFilePath, newCode, { + if (overwrite) { + fs.writeFileSync(destPath ?? dataTypesFilePath, newCode, { encoding: 'utf8', }) console.log('patch tailwindcss for custom length unit successfully!') diff --git a/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/types.ts b/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/types.ts index 4173670..3156f91 100644 --- a/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/types.ts +++ b/packages/tailwindcss-patch/src/core/patches/supportCustomUnits/types.ts @@ -1,15 +1,7 @@ -export interface ILengthUnitsPatchDangerousOptions { - packageName?: string - gteVersion?: string +export interface ILengthUnitsPatchOptions { + units: string[] lengthUnitsFilePath?: string variableName?: string overwrite?: boolean destPath?: string } - -export interface ILengthUnitsPatchOptions { - units: string[] - paths?: string[] - dangerousOptions?: ILengthUnitsPatchDangerousOptions - basedir?: string -} diff --git a/packages/tailwindcss-patch/src/core/runtime-patcher.ts b/packages/tailwindcss-patch/src/core/runtime-patcher.ts index 5e1ae3b..1bd74c0 100644 --- a/packages/tailwindcss-patch/src/core/runtime-patcher.ts +++ b/packages/tailwindcss-patch/src/core/runtime-patcher.ts @@ -12,21 +12,25 @@ export function internalPatch(pkgJsonPath: string | undefined, options: Internal const twDir = path.dirname(pkgJsonPath) if (gte(pkgJson.version!, '3.0.0')) { options.version = pkgJson.version - monkeyPatchForSupportingCustomUnit(twDir, { - units: ['rpx'], - dangerousOptions: { - lengthUnitsFilePath: 'lib/util/dataTypes.js', - variableName: 'lengthUnits', - overwrite: true, - }, - }) - const result = monkeyPatchForExposingContextV3(twDir, options) - return result + + if (options.applyPatches?.extendLengthUnits) { + try { + monkeyPatchForSupportingCustomUnit(twDir) + } + catch { + + } + } + + if (options.applyPatches?.exportContext) { + return monkeyPatchForExposingContextV3(twDir, options) + } } else if (gte(pkgJson.version!, '2.0.0')) { options.version = pkgJson.version - const result = monkeyPatchForExposingContextV2(twDir, options) - return result + if (options.applyPatches?.exportContext) { + return monkeyPatchForExposingContextV2(twDir, options) + } } // no sth } diff --git a/packages/tailwindcss-patch/src/core/tw-patcher.ts b/packages/tailwindcss-patch/src/core/tw-patcher.ts index 890a752..e180cc0 100644 --- a/packages/tailwindcss-patch/src/core/tw-patcher.ts +++ b/packages/tailwindcss-patch/src/core/tw-patcher.ts @@ -32,7 +32,7 @@ export class TailwindcssPatcher { return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions) } catch (error) { - console.warn(`patch tailwindcss failed: ${(error).message}`) + console.error(`patch tailwindcss failed: ${(error).message}`) } } } diff --git a/packages/tailwindcss-patch/src/defaults.ts b/packages/tailwindcss-patch/src/defaults.ts index 5d70ca0..08deb4f 100644 --- a/packages/tailwindcss-patch/src/defaults.ts +++ b/packages/tailwindcss-patch/src/defaults.ts @@ -4,6 +4,10 @@ import type { DeepRequired, InternalPatchOptions, PatchOptions } from './types' export function getDefaultPatchOptions(): DeepRequired { return { + applyPatches: { + exportContext: true, + extendLengthUnits: false, + }, overwrite: true, } } diff --git a/packages/tailwindcss-patch/src/types.ts b/packages/tailwindcss-patch/src/types.ts index c529f77..583e591 100644 --- a/packages/tailwindcss-patch/src/types.ts +++ b/packages/tailwindcss-patch/src/types.ts @@ -31,11 +31,7 @@ export interface PatchOptions { } } -export interface InternalPatchOptions { - overwrite: boolean - paths?: string[] - basedir?: string - custom?: (dir: string, ctx: Record) => void +export interface InternalPatchOptions extends PatchOptions { version?: string } diff --git a/packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap b/packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap index 84cedc5..a175ba3 100644 --- a/packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap +++ b/packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap @@ -2,6 +2,10 @@ exports[`defaults > getDefaultPatchOptions 1`] = ` { + "applyPatches": { + "exportContext": true, + "extendLengthUnits": false, + }, "overwrite": true, } `; diff --git a/packages/tailwindcss-patch/test/index.test.ts b/packages/tailwindcss-patch/test/index.test.ts index db09e43..c8c2dbf 100644 --- a/packages/tailwindcss-patch/test/index.test.ts +++ b/packages/tailwindcss-patch/test/index.test.ts @@ -18,6 +18,7 @@ function getTailwindcssVersion(str: string) { } } +// eslint-disable-next-line ts/no-var-requires, ts/no-require-imports const pkg = require(versionsPkgDir) const versions = Object.keys(pkg.dependencies) @@ -27,6 +28,9 @@ describe('versions-patch', () => { const res = internalPatch(path.resolve(tailwindcssCasePath, `versions/${v}/package.json`), { overwrite: false, + applyPatches: { + exportContext: true, + }, }) expect(res).toMatchSnapshot() })