-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
babel-helpers.ts
63 lines (55 loc) · 1.45 KB
/
babel-helpers.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
import { murmurhash } from "babel-plugin-remove-graphql-queries/murmur"
import { JSXOpeningElement } from "@babel/types"
import { NodePath } from "@babel/core"
import { getAttributeValues } from "babel-jsx-utils"
import camelCase from "camelcase"
export const SHARP_ATTRIBUTES = new Set([
`src`,
`layout`,
`formats`,
`maxWidth`,
`maxHeight`,
`quality`,
`avifOptions`,
`jpegOptions`,
`pngOptions`,
`webpOptions`,
`blurredOptions`,
`transformOptions`,
`width`,
`height`,
`placeholder`,
`tracedSVGOptions`,
`sizes`,
`background`,
])
export function normalizeProps(
props: Record<string, unknown>
): Record<string, unknown> {
const out = {
...props,
}
if (out.layout) {
out.layout = camelCase(out.layout as string)
}
if (out.placeholder) {
out.placeholder = camelCase(out.placeholder as string)
if (out.placeholder === `tracedSvg`) {
out.placeholder = `tracedSVG`
}
}
if (Array.isArray(out.formats)) {
out.formats = out.formats.map((format: string) => format.toLowerCase())
}
return out
}
export function evaluateImageAttributes(
nodePath: NodePath<JSXOpeningElement>,
onError?: (prop: string) => void
): Record<string, unknown> {
// Only get attributes that we need for generating the images
return normalizeProps(getAttributeValues(nodePath, onError, SHARP_ATTRIBUTES))
}
export function hashOptions(options: unknown): string {
return `${murmurhash(JSON.stringify(options))}`
}