generated from sonofmagic/monorepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
73 lines (68 loc) · 1.69 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from 'node:fs/promises'
import process from 'node:process'
import { groupBy } from '@tailwindcss-mangle/shared'
import path from 'pathe'
import { pluginName } from './constants'
export function escapeStringRegexp(str: string) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string')
}
return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&').replaceAll('-', '\\x2d')
}
export function getGroupedEntries<T>(
entries: [string, T][],
options = {
cssMatcher(file: string) {
return /\.css$/.test(file)
},
htmlMatcher(file: string) {
return /\.html?$/.test(file)
},
jsMatcher(file: string) {
return /\.[cm]?js$/.test(file)
},
},
) {
const { cssMatcher, htmlMatcher, jsMatcher } = options
const groupedEntries = groupBy(entries, ([file]) => {
if (cssMatcher(file)) {
return 'css'
}
else if (htmlMatcher(file)) {
return 'html'
}
else if (jsMatcher(file)) {
return 'js'
}
else {
return 'other'
}
})
if (!groupedEntries.css) {
groupedEntries.css = []
}
if (!groupedEntries.html) {
groupedEntries.html = []
}
if (!groupedEntries.js) {
groupedEntries.js = []
}
if (!groupedEntries.other) {
groupedEntries.other = []
}
return groupedEntries as Record<'css' | 'html' | 'js' | 'other', [string, T][]>
}
export function getCacheDir(basedir = process.cwd()) {
return path.resolve(basedir, 'node_modules/.cache', pluginName)
}
export async function ensureDir(p: string) {
try {
await fs.access(p)
}
catch {
await fs.mkdir(p, {
recursive: true,
})
}
}
export { defaultMangleClassFilter, isMap, isRegexp } from '@tailwindcss-mangle/shared'