-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.js
130 lines (119 loc) Β· 3.84 KB
/
image.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { SAXParser } from "sax";
import { tagYaz } from "../util/html";
import { getExt } from "../util/paths";
import compiler from "./compiler/compiler";
import { Props } from "./compiler/targetRegistry";
import { removeGlobalProps } from "./props";
const makeImageElement = (bundleName, { inSvg, piggyback, childTargets, ...props }) => {
removeGlobalProps(props);
if (piggyback)
bundleName = piggyback + bundleName;
if (inSvg) props.href = bundleName;
else props.src = bundleName;
return tagYaz(inSvg ? "image" : "img", props, true);
}
const makeTargetName = (parentTarget, suffix) => {
const lastSlash = parentTarget.lastIndexOf("/");
parentTarget = parentTarget.slice(0, parentTarget.indexOf(".", lastSlash));
return parentTarget.startsWith("build") ? `/${parentTarget}${suffix}` : `/build/${parentTarget}${suffix}`;
}
/**
* We optimize the inline svgs regardless of the build mode.
*
* @param {Props} props
* @returns {!Promise<string>}
*/
const InlineSvgImage = ({ src, childTargets, ...props }) =>
compiler.buildTarget(makeTargetName(src, ".inl.svg"), {
childTargets: childTargets || [`/${src}`],
...props
}).then(({ content }) => {
removeGlobalProps(props);
delete props.inline;
delete props.piggyback;
const parser = new SAXParser(true);
let result = "";
parser.onopentag = ({ name, attributes }) => {
if (name === "svg")
Object.assign(attributes, props);
result += tagYaz(name, attributes, false);
};
parser.ontext = (text) => {
result += text;
};
parser.onclosetag = (tagName) => {
result += `</${tagName}>`;
};
parser.write(new TextDecoder().decode(content)).close();
return result;
});
const SvgImage = ({ inline, BuildMode, bundleName, bundleWidth, bundleHeight, ...props }) => {
if (inline) return InlineSvgImage(props);
return compiler.bundleTarget(makeTargetName(props.src, props.width ? `-w${props.width}.svg` : ".svg"), {
BuildMode,
bundleName,
childTargets: props.childTargets || [`/${props.src}`]
}).then((computedBundleName) => makeImageElement(computedBundleName, props));
}
const SvgJsxImage = ({ src, ...props }) => {
const svgTarget = `/build/${src.slice(0, -8)}.jsx.svg`;
const svgProps = {
piggyback: props.piggyback,
childTargets: [`/${src}`]
};
return compiler.buildTarget(svgTarget, svgProps)
.then(() => SvgImage({
src: svgTarget.slice(1),
childTargets: [{ targetName: svgTarget, props: svgProps }],
...props
}));
}
const PngImage = ({ src, passes, quality, BuildMode, bundleWidth, bundleHeight, ...props }) =>
compiler.bundleTarget(`/build/${src.slice(0, -4)}.webp`, {
BuildMode,
passes,
quality,
bundleWidth,
bundleHeight,
childTargets: [`/${src}`]
}).then((bundledName) => makeImageElement(bundledName, props));
const Favicon = ({ src, raster, BuildMode, ...props }) => {
removeGlobalProps(props);
return Promise.all([
compiler.bundleTarget(`/build/${src}`, {
BuildMode,
childTargets: [`/${src}`]
}),
compiler.bundleTarget(`/build/${src.slice(0, -4)}${raster}.png`, {
BuildMode,
raster,
childTargets: [`/${src}`]
})
]).then(([svgBundled, pngBundled]) =>
tagYaz("link", { rel: "icon", href: svgBundled, type: "image/svg+xml" }) +
tagYaz("link", { rel: "icon", href: pngBundled, type: "image/png", sizes: `${raster}x${raster}` })
)
};
/**
* @param {!Object<string, *>} props
* @return {!Promise<string>}
*/
const Image = ({ inline, ...props }) => {
if (inline && (props.src.endsWith(".svg") || props.src.endsWith(".svg.jsx")))
return InlineSvgImage(props);
if (props.rel == "icon")
return Favicon(props);
return {
"jsx": SvgJsxImage,
"svg": SvgImage,
"png": PngImage,
}[getExt(props.src)](props);
};
export {
Favicon,
Image,
InlineSvgImage,
PngImage,
SvgImage,
SvgJsxImage
};