-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.ts
30 lines (24 loc) · 958 Bytes
/
hashing.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
import { json } from "@remix-run/cloudflare"
function getRandomString() {
return (Math.random() + 1).toString(36).substring(7)
}
export function hashKeyframe(css: string) {
const KEYFRAME_REGEX = /(?<=\@keyframes)(.*?)(?=\{)/
const keyframeMatch = css.match(KEYFRAME_REGEX)
const keyframe = keyframeMatch?.[0].trim()
const keyframeHash = keyframe ? `${keyframe}-${getRandomString()}` : null
const EVERY_KEYFRAME_REGEX = new RegExp(`${keyframe}`, "gm")
return keyframe && keyframeHash
? css.replace(EVERY_KEYFRAME_REGEX, keyframeHash)
: css
}
export function hashClassname(css: string) {
const CLASS_NAME_REGEX = /(?<=\.)(.*?)(?=\s)/
const classNameMatch = css.match(CLASS_NAME_REGEX)
if (classNameMatch) {
const classNameHash = `${classNameMatch[0].trim()}-${getRandomString()}`
return css.replace(CLASS_NAME_REGEX, `${classNameHash} `)
} else {
throw json({ error: "No class name found", status: 404 })
}
}