Skip to content

Commit

Permalink
feat: add applyPatches options
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Jul 17, 2024
1 parent 2810e09 commit ddd37e6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ 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<ILengthUnitsPatchDangerousOptions>
const { variableName, units } = options
const ast = parse(content)

let arrayRef: ArrayExpression | undefined
let changed = false
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 => (<StringLiteral>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)) {
Expand All @@ -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<ILengthUnitsPatchDangerousOptions>
const dataTypesFilePath = path.resolve(rootDir, DOPTS.lengthUnitsFilePath)
export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: ILengthUnitsPatchOptions) {
const opts = defu<Required<ILengthUnitsPatchOptions>, 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: {
Expand All @@ -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!')
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 16 additions & 12 deletions packages/tailwindcss-patch/src/core/runtime-patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/src/core/tw-patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class TailwindcssPatcher {
return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions)
}
catch (error) {
console.warn(`patch tailwindcss failed: ${(<Error>error).message}`)
console.error(`patch tailwindcss failed: ${(<Error>error).message}`)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/tailwindcss-patch/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { DeepRequired, InternalPatchOptions, PatchOptions } from './types'

export function getDefaultPatchOptions(): DeepRequired<PatchOptions> {
return {
applyPatches: {
exportContext: true,
extendLengthUnits: false,
},
overwrite: true,
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/tailwindcss-patch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export interface PatchOptions {
}
}

export interface InternalPatchOptions {
overwrite: boolean
paths?: string[]
basedir?: string
custom?: (dir: string, ctx: Record<string, any>) => void
export interface InternalPatchOptions extends PatchOptions {
version?: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

exports[`defaults > getDefaultPatchOptions 1`] = `
{
"applyPatches": {
"exportContext": true,
"extendLengthUnits": false,
},
"overwrite": true,
}
`;
4 changes: 4 additions & 0 deletions packages/tailwindcss-patch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
})
Expand Down

0 comments on commit ddd37e6

Please sign in to comment.