Skip to content

Commit 5cf61e3

Browse files
committed
refactor: use TailwindcssPatcher
1 parent 3db8b13 commit 5cf61e3

File tree

9 files changed

+158
-164
lines changed

9 files changed

+158
-164
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@tailwindcss-mangle/shared": "workspace:*",
2828
"@tsconfig/recommended": "^1.0.6",
2929
"@types/lodash-es": "^4.17.12",
30-
"@types/node": "^20.14.1",
30+
"@types/node": "^20.14.2",
3131
"@vitest/coverage-v8": "^1.6.0",
3232
"bumpp": "^9.4.1",
3333
"cross-env": "^7.0.3",
@@ -38,14 +38,15 @@
3838
"eslint-config-icebreaker": "^1.2.3",
3939
"eslint-config-prettier": "^9.1.0",
4040
"eslint-plugin-prettier": "^5.1.3",
41+
"local-pkg": "^0.5.0",
4142
"lodash-es": "^4.17.21",
4243
"only-allow": "^1.2.1",
4344
"pathe": "^1.1.2",
4445
"prettier": "^3.3.0",
4546
"rollup": "^4.18.0",
4647
"tailwindcss-patch": "workspace:*",
4748
"ts-node": "^10.9.2",
48-
"tslib": "^2.6.2",
49+
"tslib": "^2.6.3",
4950
"tsup": "^8.1.0",
5051
"typescript": "^5.4.5",
5152
"unbuild": "^2.0.0",
@@ -56,4 +57,4 @@
5657
"node": ">=18.0.0"
5758
},
5859
"packageManager": "[email protected]"
59-
}
60+
}

packages/tailwindcss-patch/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
"@babel/types": "^7.24.6",
5555
"@tailwindcss-mangle/config": "workspace:^",
5656
"cac": "^6.7.14",
57-
"jiti": "^1.21.0",
57+
"jiti": "^1.21.1",
5858
"lilconfig": "^3.1.1",
59-
"local-pkg": "^0.5.0",
6059
"postcss": "^8.4.38",
6160
"resolve": "^1.22.8",
6261
"semver": "^7.6.2"

packages/tailwindcss-patch/src/cli.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import process from 'node:process'
22
import cac from 'cac'
33
import { configName, getConfig, initConfig } from './config'
4-
import { TailwindcssPatcher, createPatch, getPatchOptions } from './core'
4+
import { TailwindcssPatcher } from './core'
5+
import { getPatchOptions } from '@/defaults'
56

67
function init() {
78
const cwd = process.cwd()
@@ -11,9 +12,10 @@ function init() {
1112
const cli = cac()
1213

1314
cli.command('install', 'patch install').action(() => {
14-
const opt = getPatchOptions()
15-
const patch = createPatch(opt)
16-
patch()
15+
const twPatcher = new TailwindcssPatcher({
16+
patch: getPatchOptions(),
17+
})
18+
twPatcher.patch()
1719
})
1820

1921
cli.command('init').action(async () => {

packages/tailwindcss-patch/src/core/postcss.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import process from 'node:process'
33
import postcss from 'postcss'
44
import { lilconfig } from 'lilconfig'
55
import createJiti from 'jiti'
6-
// const jiti = require('jiti')(__filename)
7-
// const importDefault = async (filepath: string) => {
8-
// const module = await import(url.pathToFileURL(filepath).href)
9-
// return module.default
10-
// }
6+
117
const jiti = createJiti(__filename)
128

139
export async function processTailwindcss(options: { cwd?: string, config?: string }) {

packages/tailwindcss-patch/src/core/runtime-patcher.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,11 @@
11
import path from 'node:path'
22
import fs from 'node:fs'
3-
import process from 'node:process'
43
import { gte } from 'semver'
54
import type { PackageJson } from 'pkg-types'
6-
import { defu } from 'defu'
75
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './inspector'
86
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './inspector-postcss7-compat'
9-
import type { InternalPatchOptions, PatchOptions } from '@/types'
10-
import { getDefaultPatchOptions } from '@/defaults'
11-
import { ensureFileContent, requireResolve } from '@/utils'
12-
13-
export function getInstalledPkgJsonPath(options: PatchOptions = {}) {
14-
try {
15-
// const cwd = process.cwd()
16-
const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
17-
paths: options.paths,
18-
basedir: options.basedir,
19-
})
20-
21-
return tmpJsonPath
22-
// https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin
23-
// only tailwindcss version > 3.0.0
24-
}
25-
catch (error) {
26-
if ((<Error & { code: string }>error).code === 'MODULE_NOT_FOUND') {
27-
console.warn('Can\'t find npm pkg: `tailwindcss`, Please ensure it has been installed!')
28-
}
29-
}
30-
}
31-
32-
export function getPatchOptions(options: PatchOptions = {}) {
33-
return defu(
34-
options,
35-
{
36-
basedir: process.cwd(),
37-
},
38-
getDefaultPatchOptions(),
39-
) as InternalPatchOptions
40-
}
41-
42-
export function createPatch(opt: InternalPatchOptions) {
43-
return () => {
44-
try {
45-
const pkgJsonPath = getInstalledPkgJsonPath(opt)
46-
return internalPatch(pkgJsonPath, opt)
47-
}
48-
catch (error) {
49-
console.warn(`patch tailwindcss failed:${(<Error>error).message}`)
50-
}
51-
}
52-
}
7+
import type { InternalPatchOptions } from '@/types'
8+
import { ensureFileContent } from '@/utils'
539

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

packages/tailwindcss-patch/src/core/tw-patcher.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import path from 'node:path'
22
import fs from 'node:fs'
33
import { CacheManager, getCacheOptions } from './cache'
4-
import { createPatch, getPatchOptions } from './runtime-patcher'
4+
import { internalPatch } from './runtime-patcher'
55
import { processTailwindcss } from './postcss'
66
import type { UserConfig } from '@/config'
7+
import { getPatchOptions } from '@/defaults'
78
import { ensureDir, getPackageInfoSync, isObject } from '@/utils'
89
import type { CacheStrategy, InternalCacheOptions, InternalPatchOptions, PackageInfo, TailwindcssClassCache, TailwindcssPatcherOptions, TailwindcssRuntimeContext } from '@/types'
910

@@ -20,12 +21,20 @@ export class TailwindcssPatcher {
2021
this.rawOptions = options
2122
this.cacheOptions = getCacheOptions(options.cache)
2223
this.patchOptions = getPatchOptions(options.patch)
23-
this.patch = createPatch(this.patchOptions)
24+
2425
this.cacheManager = new CacheManager(this.cacheOptions)
2526
this.packageInfo = getPackageInfoSync('tailwindcss', { basedir: this.patchOptions.basedir })
2627
if (this.packageInfo && this.packageInfo.version) {
2728
this.majorVersion = Number.parseInt(this.packageInfo.version[0])
2829
}
30+
this.patch = () => {
31+
try {
32+
return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions)
33+
}
34+
catch (error) {
35+
console.warn(`patch tailwindcss failed: ${(<Error>error).message}`)
36+
}
37+
}
2938
}
3039

3140
setCache(set: Set<string>) {
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
import type { DeepRequired, PatchOptions } from './types'
1+
import process from 'node:process'
2+
import { defu } from 'defu'
3+
import type { DeepRequired, InternalPatchOptions, PatchOptions } from './types'
24

35
export function getDefaultPatchOptions(): DeepRequired<PatchOptions> {
46
return {
57
overwrite: true,
68
}
79
}
10+
11+
export function getPatchOptions(options: PatchOptions = {}) {
12+
return defu(
13+
options,
14+
{
15+
basedir: process.cwd(),
16+
},
17+
getDefaultPatchOptions(),
18+
) as InternalPatchOptions
19+
}

packages/tailwindcss-patch/test/runtime-patcher.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
2-
import { getInstalledPkgJsonPath } from '@/core'
2+
// import { getInstalledPkgJsonPath } from '@/core'
33

4-
describe('patcher', () => {
4+
describe.skip('patcher', () => {
55
it('getInstalledPkgJsonPath common options', () => {
66
const pkgJsonPath = getInstalledPkgJsonPath()
77
expect(pkgJsonPath).toBeTruthy()

0 commit comments

Comments
 (0)