Skip to content

Commit f993762

Browse files
committed
feat: commit TailwindcssPatcher
1 parent 178e5ff commit f993762

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import { pkgName } from './constants'
4+
import type { CacheOptions } from './type'
5+
6+
export function mkCacheDirectory(cacheDirectory: string) {
7+
const exists = fs.existsSync(cacheDirectory)
8+
if (!exists) {
9+
fs.mkdirSync(cacheDirectory, {
10+
recursive: true
11+
})
12+
}
13+
return cacheDirectory
14+
}
15+
16+
export function writeCache(data: Set<string>, options: CacheOptions = {}) {
17+
try {
18+
const cwd = options.cwd ?? process.cwd()
19+
const cacheDirectory = options.dir ?? path.resolve(cwd, 'node_modules', '.cache', pkgName)
20+
const filename = path.resolve(cacheDirectory, options.file ?? 'classSet.json')
21+
mkCacheDirectory(cacheDirectory)
22+
fs.writeFileSync(filename, JSON.stringify(Array.from(data), null, 2), 'utf-8')
23+
return filename
24+
} catch (error) {
25+
console.log(error)
26+
}
27+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { getClassCacheSet, getContexts, getTailwindcssEntry } from './exposeContext'
2+
import type { CacheOptions } from './type'
3+
4+
type InternalCacheOptions = CacheOptions & { enable: boolean }
5+
6+
export interface TailwindcssPatcherOptions {
7+
cache?: InternalCacheOptions
8+
}
9+
10+
export class TailwindcssPatcher {
11+
public rawOptions: TailwindcssPatcherOptions
12+
public cache: InternalCacheOptions
13+
constructor(options: TailwindcssPatcherOptions = {}) {
14+
this.rawOptions = options
15+
let cache: InternalCacheOptions
16+
switch (typeof options.cache) {
17+
case 'undefined': {
18+
cache = {
19+
enable: false
20+
}
21+
break
22+
}
23+
case 'boolean': {
24+
cache = {
25+
enable: options.cache
26+
}
27+
break
28+
}
29+
case 'object': {
30+
cache = { ...options.cache, enable: true }
31+
32+
break
33+
}
34+
}
35+
this.cache = cache
36+
}
37+
38+
getPkgEntry(basedir?: string) {
39+
return getTailwindcssEntry(basedir)
40+
}
41+
42+
getClassSet(basedir?: string) {
43+
const set = getClassCacheSet(basedir)
44+
return set
45+
}
46+
47+
getContexts(basedir?: string) {
48+
return getContexts(basedir)
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const pkgName = 'tailwindcss-patch'

packages/tailwindcss-patch/src/exposeContext.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ export function getContexts(basedir?: string) {
2626
return []
2727
}
2828

29-
export function getClassCaches(): Map<
29+
export function getClassCaches(basedir?: string): Map<
3030
string,
3131
(
3232
| {
33-
layer: string
34-
options: Record<string, any>
35-
sort: Record<string, any>
36-
}
33+
layer: string
34+
options: Record<string, any>
35+
sort: Record<string, any>
36+
}
3737
| Rule
3838
)[]
3939
>[] {
40-
const contexts = getContexts()
40+
const contexts = getContexts(basedir)
4141
return (contexts as any[]).map((x) => x.classCache)
4242
}
4343

44-
export function getClassCacheSet(): Set<string> {
45-
const classCaches = getClassCaches()
44+
export function getClassCacheSet(basedir?: string): Set<string> {
45+
const classCaches = getClassCaches(basedir)
4646
const classSet = new Set<string>()
4747
for (let i = 0; i < classCaches.length; i++) {
4848
const classCacheMap = classCaches[i]

packages/tailwindcss-patch/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export * from './exposeContext'
22
export * from './inspector'
33
export * from './patcher'
44
export * from './utils'
5-
export * from './type'
5+
export * from './type'
+10-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
export interface CacheOptions {
2+
// enable?: boolean
3+
dir?: string
4+
cwd?: string
5+
file?: string
6+
}
7+
18
export interface PatchOptions {
29
overwrite?: boolean
310
paths?: string[]
4-
basedir?: string,
11+
basedir?: string
512
custom?: (dir: string, ctx: Record<string, any>) => void
13+
cache?: boolean | CacheOptions
614
}
715

816
export interface InternalPatchOptions {
917
overwrite: boolean
1018
paths?: string[]
1119
basedir?: string
1220
custom?: (dir: string, ctx: Record<string, any>) => void
21+
cache?: CacheOptions
1322
}

0 commit comments

Comments
 (0)