Skip to content

Commit

Permalink
chore(tailwindcss-patch): commit patches foldor
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Jul 13, 2024
1 parent e80eb67 commit bcbebab
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './tw-patcher'
export * from './inspector'
export * from './patches'
export * from './runtime-patcher'
export * from './cache'
73 changes: 73 additions & 0 deletions packages/tailwindcss-patch/src/core/patches/exportContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import path from 'node:path'
import fs from 'node:fs'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss'
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-compat'
import type { InternalPatchOptions } from '@/types'
import { ensureFileContent } from '@/utils'

export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions) {
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/processTailwindFeatures.js')

const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
if (processTailwindFeaturesContent) {
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}

const pluginFilePath = path.resolve(twDir, 'lib/plugin.js')
const indexFilePath = path.resolve(twDir, 'lib/index.js')
const pluginContent = ensureFileContent([pluginFilePath, indexFilePath])
if (pluginContent) {
const { code, hasPatched } = inspectPostcssPlugin(pluginContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(pluginFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
}
result.plugin = code
}

opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
return result
}

export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatchOptions) {
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/jit/processTailwindFeatures.js')

const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
if (processTailwindFeaturesContent) {
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat(processTailwindFeaturesContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}

const indexFilePath = path.resolve(twDir, 'lib/jit/index.js')
const pluginContent = ensureFileContent([indexFilePath])
if (pluginContent) {
const { code, hasPatched } = inspectPostcssPluginCompat(pluginContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(indexFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
}
result.plugin = code
}

opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
return result
}
1 change: 1 addition & 0 deletions packages/tailwindcss-patch/src/core/patches/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './exportContext'
73 changes: 2 additions & 71 deletions packages/tailwindcss-patch/src/core/runtime-patcher.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,9 @@
import path from 'node:path'
import fs from 'node:fs'
import { gte } from 'semver'
import type { PackageJson } from 'pkg-types'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './inspector'
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './inspector-postcss7-compat'
import type { InternalPatchOptions } from '@/types'
import { ensureFileContent } from '@/utils'

export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions) {
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/processTailwindFeatures.js')

const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
if (processTailwindFeaturesContent) {
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}

const pluginFilePath = path.resolve(twDir, 'lib/plugin.js')
const indexFilePath = path.resolve(twDir, 'lib/index.js')
const pluginContent = ensureFileContent([pluginFilePath, indexFilePath])
if (pluginContent) {
const { code, hasPatched } = inspectPostcssPlugin(pluginContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(pluginFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
}
result.plugin = code
}

opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
return result
}
import { monkeyPatchForExposingContextV2, monkeyPatchForExposingContextV3 } from './patches'

export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatchOptions) {
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/jit/processTailwindFeatures.js')

const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath)
const result: { processTailwindFeatures?: string, plugin?: string } & Record<string, any> = {}
if (processTailwindFeaturesContent) {
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat(processTailwindFeaturesContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}

const indexFilePath = path.resolve(twDir, 'lib/jit/index.js')
const pluginContent = ensureFileContent([indexFilePath])
if (pluginContent) {
const { code, hasPatched } = inspectPostcssPluginCompat(pluginContent)
if (!hasPatched && opt.overwrite) {
fs.writeFileSync(indexFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
}
result.plugin = code
}

opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result)
return result
}
import type { InternalPatchOptions } from '@/types'

export function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined {
if (pkgJsonPath) {
Expand Down
4 changes: 4 additions & 0 deletions packages/tailwindcss-patch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface PatchOptions {
paths?: string[]
basedir?: string
custom?: (dir: string, ctx: Record<string, any>) => void
applyPatches?: {
exportContext?: boolean
extendLengthUnits?: boolean
}
}

export interface InternalPatchOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/inspector.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import fs from 'node:fs'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss'

const tailwindcssCasePath = path.resolve(__dirname, 'fixtures')
const twltsLibPath = path.resolve(tailwindcssCasePath, 'versions/3.3.1/lib')
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/postcss7-compat.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTestCase } from './utils'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/inspector-postcss7-compat'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss-compat'

describe('postcss7-compat', () => {
it('processTailwindFeatures patch', async () => {
Expand Down

0 comments on commit bcbebab

Please sign in to comment.