@webstudio-is/image 0.179.0 → 0.182.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +22 -9
- package/lib/types/image-optimize.d.ts +4 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -71,6 +71,14 @@ var getInt = (value) => {
|
|
|
71
71
|
};
|
|
72
72
|
var DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
|
|
73
73
|
var DEFAULT_QUALITY = 80;
|
|
74
|
+
var UrlCanParse = (url) => {
|
|
75
|
+
try {
|
|
76
|
+
new URL(url);
|
|
77
|
+
return true;
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
74
82
|
var getImageAttributes = (props) => {
|
|
75
83
|
const width = getInt(props.width);
|
|
76
84
|
const quality = Math.max(
|
|
@@ -88,7 +96,9 @@ var getImageAttributes = (props) => {
|
|
|
88
96
|
loader: props.loader
|
|
89
97
|
});
|
|
90
98
|
}
|
|
91
|
-
const resAttrs = {
|
|
99
|
+
const resAttrs = {
|
|
100
|
+
src: UrlCanParse(props.src) ? props.src : props.loader({ src: props.src, format: "raw" })
|
|
101
|
+
};
|
|
92
102
|
if (props.srcSet != null) {
|
|
93
103
|
resAttrs.srcSet = props.srcSet;
|
|
94
104
|
}
|
|
@@ -183,15 +193,17 @@ var createImageLoader = (loaderOptions) => (props) => {
|
|
|
183
193
|
} catch {
|
|
184
194
|
return src;
|
|
185
195
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
196
|
+
if (format !== "raw") {
|
|
197
|
+
resultUrl.searchParams.set("width", width.toString());
|
|
198
|
+
resultUrl.searchParams.set("quality", quality.toString());
|
|
199
|
+
if (props.height != null) {
|
|
200
|
+
resultUrl.searchParams.set("height", props.height.toString());
|
|
201
|
+
}
|
|
202
|
+
if (props.fit != null) {
|
|
203
|
+
resultUrl.searchParams.set("fit", props.fit);
|
|
204
|
+
}
|
|
194
205
|
}
|
|
206
|
+
resultUrl.searchParams.set("format", format ?? "auto");
|
|
195
207
|
resultUrl.pathname = joinPath(resultUrl.pathname, encodePathFragment(src));
|
|
196
208
|
if (resultUrl.href.startsWith(NON_EXISTING_DOMAIN)) {
|
|
197
209
|
return `${resultUrl.pathname}?${resultUrl.searchParams.toString()}`;
|
|
@@ -200,6 +212,7 @@ var createImageLoader = (loaderOptions) => (props) => {
|
|
|
200
212
|
};
|
|
201
213
|
export {
|
|
202
214
|
Image,
|
|
215
|
+
UrlCanParse,
|
|
203
216
|
allSizes,
|
|
204
217
|
createImageLoader,
|
|
205
218
|
getImageAttributes,
|
|
@@ -96,6 +96,10 @@ export type ImageLoader = (props: {
|
|
|
96
96
|
format: "raw";
|
|
97
97
|
}) => string;
|
|
98
98
|
export declare const allSizes: number[];
|
|
99
|
+
/**
|
|
100
|
+
* URL.canParse(props.src)
|
|
101
|
+
*/
|
|
102
|
+
export declare const UrlCanParse: (url: string) => boolean;
|
|
99
103
|
export declare const getImageAttributes: (props: {
|
|
100
104
|
src: string | undefined;
|
|
101
105
|
srcSet: string | undefined;
|