@webstudio-is/image 0.55.0 → 0.57.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.
@@ -41,8 +41,10 @@ const cloudflareImageLoader = (loaderOptions) => ({ width, src, quality }) => {
41
41
  "Width must be only from allowed values"
42
42
  );
43
43
  }
44
+ const cdnUrl = loaderOptions?.cdnUrl ?? "/";
45
+ const imageUrl = `${cdnUrl}${src}`;
44
46
  const options = `width=${width},quality=${quality},format=auto`;
45
- const pathname = `/cdn-cgi/image/${options}/${src}`;
47
+ const pathname = `/cdn-cgi/image/${options}/${imageUrl}`;
46
48
  if (loaderOptions?.resizeOrigin != null) {
47
49
  const url = new URL(pathname, loaderOptions.resizeOrigin);
48
50
  return url.href;
@@ -7,8 +7,10 @@ const cloudflareImageLoader = (loaderOptions) => ({ width, src, quality }) => {
7
7
  "Width must be only from allowed values"
8
8
  );
9
9
  }
10
+ const cdnUrl = loaderOptions?.cdnUrl ?? "/";
11
+ const imageUrl = `${cdnUrl}${src}`;
10
12
  const options = `width=${width},quality=${quality},format=auto`;
11
- const pathname = `/cdn-cgi/image/${options}/${src}`;
13
+ const pathname = `/cdn-cgi/image/${options}/${imageUrl}`;
12
14
  if (loaderOptions?.resizeOrigin != null) {
13
15
  const url = new URL(pathname, loaderOptions.resizeOrigin);
14
16
  return url.href;
package/package.json CHANGED
@@ -1,29 +1,31 @@
1
1
  {
2
2
  "name": "@webstudio-is/image",
3
- "version": "0.55.0",
3
+ "version": "0.57.0",
4
4
  "description": "Image optimization",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
7
7
  "type": "module",
8
8
  "dependencies": {
9
- "react": "^17.0.2",
9
+ "react": "^18.2.0",
10
10
  "warn-once": "^0.1.1",
11
- "@webstudio-is/generate-arg-types": "^0.55.0"
11
+ "@webstudio-is/generate-arg-types": "^0.57.0"
12
12
  },
13
13
  "devDependencies": {
14
14
  "@jest/globals": "^29.3.1",
15
15
  "@storybook/react": "^6.5.16",
16
- "@types/react": "^17.0.24",
16
+ "@types/react": "^18.0.35",
17
17
  "jest": "^29.3.1",
18
+ "react": "^18.2.0",
19
+ "react-dom": "^18.2.0",
18
20
  "typescript": "5.0.3",
19
- "@webstudio-is/jest-config": "^1.0.2",
21
+ "@webstudio-is/jest-config": "^1.0.4",
20
22
  "@webstudio-is/scripts": "^0.0.0",
21
23
  "@webstudio-is/storybook-config": "^0.0.0",
22
- "@webstudio-is/tsconfig": "^1.0.3"
24
+ "@webstudio-is/tsconfig": "^1.0.4"
23
25
  },
24
26
  "peerDependencies": {
25
- "react": "^17.0.2",
26
- "react-dom": "^17.0.2"
27
+ "react": "^18.2.0",
28
+ "react-dom": "^18.2.0"
27
29
  },
28
30
  "module": "./lib/index.js",
29
31
  "exports": {
@@ -41,13 +43,13 @@
41
43
  "private": false,
42
44
  "sideEffects": false,
43
45
  "scripts": {
44
- "typecheck": "tsc --noEmit",
46
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false",
45
47
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
46
48
  "checks": "pnpm typecheck && pnpm lint && pnpm test",
47
49
  "dev": "build-package --watch",
48
50
  "build": "build-package",
49
51
  "build:args": "generate-arg-types './src/*.tsx !./src/**/*.stories.tsx !./src/**/*.ws.tsx' && prettier --write \"**/*.props.ts\"",
50
- "dts": "tsc --emitDeclarationOnly --declaration --declarationDir lib/types",
52
+ "dts": "tsc",
51
53
  "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
52
54
  "storybook:run": "start-storybook -p 6006",
53
55
  "storybook:build": "build-storybook",
@@ -2,17 +2,18 @@ import warnOnce from "warn-once";
2
2
  import { allSizes, type ImageLoader } from "./image-optimize";
3
3
 
4
4
  export type CloudflareImageLoaderOptions = {
5
+ // origin of transformation wrapper
5
6
  resizeOrigin?: string | null;
7
+ // origin of cdn serving image
8
+ cdnUrl?: string;
6
9
  };
7
10
 
8
11
  /**
9
12
  * Default image loader in case of no loader provided
10
13
  * https://developers.cloudflare.com/images/image-resizing/url-format/
11
14
  **/
12
- export const cloudflareImageLoader: (
13
- ops: CloudflareImageLoaderOptions | null
14
- ) => ImageLoader =
15
- (loaderOptions) =>
15
+ export const cloudflareImageLoader =
16
+ (loaderOptions: CloudflareImageLoaderOptions | null): ImageLoader =>
16
17
  ({ width, src, quality }) => {
17
18
  if (process.env.NODE_ENV !== "production") {
18
19
  warnOnce(
@@ -21,9 +22,12 @@ export const cloudflareImageLoader: (
21
22
  );
22
23
  }
23
24
 
25
+ const cdnUrl = loaderOptions?.cdnUrl ?? "/";
26
+ const imageUrl = `${cdnUrl}${src}`;
27
+
24
28
  const options = `width=${width},quality=${quality},format=auto`;
25
29
  // Cloudflare docs say that we don't need to urlencode the path params
26
- const pathname = `/cdn-cgi/image/${options}/${src}`;
30
+ const pathname = `/cdn-cgi/image/${options}/${imageUrl}`;
27
31
 
28
32
  if (loaderOptions?.resizeOrigin != null) {
29
33
  const url = new URL(pathname, loaderOptions.resizeOrigin);
@@ -7,11 +7,14 @@ describe("Image optimizations applied", () => {
7
7
  const imgAttr = getImageAttributes({
8
8
  optimize: true,
9
9
  width: 100,
10
- src: "https://webstudio.is/logo.webp",
10
+ src: "logo.webp",
11
11
  srcSet: undefined,
12
12
  sizes: undefined,
13
13
  quality: 100,
14
- loader: cloudflareImageLoader({ resizeOrigin: null }),
14
+ loader: cloudflareImageLoader({
15
+ resizeOrigin: null,
16
+ cdnUrl: "https://webstudio.is/",
17
+ }),
15
18
  });
16
19
 
17
20
  expect(imgAttr).toMatchInlineSnapshot(`
@@ -27,11 +30,14 @@ describe("Image optimizations applied", () => {
27
30
  const imgAttr = getImageAttributes({
28
31
  optimize: true,
29
32
  width: undefined,
30
- src: "https://webstudio.is/logo.webp",
33
+ src: "logo.webp",
31
34
  srcSet: undefined,
32
35
  sizes: undefined,
33
36
  quality: 90,
34
- loader: cloudflareImageLoader({ resizeOrigin: null }),
37
+ loader: cloudflareImageLoader({
38
+ resizeOrigin: null,
39
+ cdnUrl: "https://webstudio.is/",
40
+ }),
35
41
  });
36
42
 
37
43
  expect(imgAttr).toMatchInlineSnapshot(`
@@ -47,11 +53,14 @@ describe("Image optimizations applied", () => {
47
53
  const imgAttr = getImageAttributes({
48
54
  optimize: true,
49
55
  width: undefined,
50
- src: "https://webstudio.is/logo.webp",
56
+ src: "logo.webp",
51
57
  srcSet: undefined,
52
58
  sizes: "100vw",
53
59
  quality: 70,
54
- loader: cloudflareImageLoader({ resizeOrigin: null }),
60
+ loader: cloudflareImageLoader({
61
+ resizeOrigin: null,
62
+ cdnUrl: "https://webstudio.is/",
63
+ }),
55
64
  });
56
65
 
57
66
  expect(imgAttr).toMatchInlineSnapshot(`
@@ -67,12 +76,13 @@ describe("Image optimizations applied", () => {
67
76
  const imgAttr = getImageAttributes({
68
77
  optimize: true,
69
78
  width: undefined,
70
- src: "https://webstudio.is/logo.webp",
79
+ src: "logo.webp",
71
80
  srcSet: undefined,
72
81
  sizes: "100vw",
73
82
  quality: 70,
74
83
  loader: cloudflareImageLoader({
75
84
  resizeOrigin: "https://resize-origin.is",
85
+ cdnUrl: "https://webstudio.is/",
76
86
  }),
77
87
  });
78
88
 
@@ -1,2 +0,0 @@
1
- import type { PropMeta } from "@webstudio-is/generate-arg-types";
2
- export declare const props: Record<string, PropMeta>;
@@ -1,34 +0,0 @@
1
- import type * as React from "react";
2
- import type { ComponentMeta, ComponentStory } from "@storybook/react";
3
- declare const _default: ComponentMeta<React.ForwardRefExoticComponent<Pick<React.ClassAttributes<HTMLImageElement> & React.ImgHTMLAttributes<HTMLImageElement> & {
4
- quality?: number | undefined;
5
- optimize?: boolean | undefined;
6
- loader: import("./image-optimize").ImageLoader;
7
- }, "quality" | "loader" | "key" | keyof React.ImgHTMLAttributes<HTMLImageElement> | "optimize"> & React.RefAttributes<HTMLImageElement>>>;
8
- export default _default;
9
- /**
10
- * Load images depending on image width and device per pixel ratio.
11
- **/
12
- export declare const FixedWidthImage: ComponentStory<React.FunctionComponent>;
13
- /**
14
- * Preserve ratio using object-fit: cover. Load images depending on image width and device per pixel ratio.
15
- **/
16
- export declare const FixedWidthImageCover: ComponentStory<React.FunctionComponent>;
17
- /**
18
- * Load images depending on the viewport width.
19
- **/
20
- export declare const UnknownWidthImage: ComponentStory<React.FunctionComponent>;
21
- /**
22
- * Fit width of the parent container, has own aspect-ratio and object-fit=cover.
23
- * Load images depending on the viewport width.
24
- **/
25
- export declare const AspectRatioImage: ComponentStory<React.FunctionComponent>;
26
- /**
27
- * Fill width and height of the relative parent container, object-fit=cover. Load images depending on the viewport width.
28
- **/
29
- export declare const FillParentImage: ComponentStory<React.FunctionComponent>;
30
- /**
31
- * "sizes" attribute explicitly equal to 100vw allowing to skip the default behavior.
32
- * See DEFAULT_SIZES in the Image component. Load images depending on the viewport width.
33
- **/
34
- export declare const HeroImage: ComponentStory<React.FunctionComponent>;
@@ -1,17 +0,0 @@
1
- import { type ImageLoader } from "./image-optimize";
2
- export type CloudflareImageLoaderOptions = {
3
- resizeOrigin?: string | null;
4
- };
5
- /**
6
- * Default image loader in case of no loader provided
7
- * https://developers.cloudflare.com/images/image-resizing/url-format/
8
- **/
9
- export declare const cloudflareImageLoader: (ops: CloudflareImageLoaderOptions | null) => ImageLoader;
10
- type LocalImageLoaderOptions = {
11
- publicPath?: string;
12
- };
13
- /**
14
- * Fake pseudo loader for local testing purposes
15
- **/
16
- export declare const localImageLoader: (options: LocalImageLoaderOptions) => ImageLoader;
17
- export {};
@@ -1,106 +0,0 @@
1
- /**
2
- * # Responsive Image component helpers.
3
- *
4
- * ## Quick summary about img srcset and sizes attributes:
5
- *
6
- * There are 2 ways to define what image will be loaded in the img property srcset.
7
- *
8
- * 1. via pixel density descriptor 'x', like `srcset="photo-small.jpg 1x, photo-medium.jpg 1.5x, photo-huge.jpg 2x"`
9
- * src will be selected depending on `device-pixel-ratio`.
10
- *
11
- * 2. via viewport width descriptor 'w' and sizes property containing source size descriptors, like
12
- * `srcset="photo-small.jpg 320w, photo-medium.jpg 640w, photo-huge.jpg 1280w"`
13
- * `sizes="(max-width: 600px) 400px, (max-width: 1200px) 70vw, 50vw"`
14
- *
15
- * The browser finds the first matching media query from source size descriptors,
16
- * then use source size value to generate internally srcset
17
- * with pixel density descriptors dividing width descriptor value by source size value.
18
- *
19
- * Using the example above for viewport width 800px.
20
- * The first matching media query is (max-width: 1200px)
21
- * source size value is 70vw equal to 800px * 0,7 = 560px
22
- *
23
- * browser internal srcset will be (we divide `w` descriptor by source size value):
24
- * photo-small.jpg 320/560x, photo-medium.jpg 640/560x, photo-huge.jpg 1280/560x =>
25
- * photo-small.jpg 0.57x, photo-medium.jpg 1.14x, photo-huge.jpg 2.28x
26
- *
27
- * Finally same rules as for pixel density descriptor 'x' are applied.
28
- *
29
- * ## Algorithm (without optimizations):
30
- *
31
- * We have a predefined array of all supported image sizes allSizes, this is the real width of an image in pixels.
32
- * This is good for caching, as we can cache image with specific width and then use it for different devices.
33
- *
34
- * > allSizes array is a tradeoff between cache and the best possible image size you deliver to the user.
35
- * > If allSizes.length is too small, you will deliver too big images to the user,
36
- * > if allSizes.length is too big, you will have many caches misses.
37
- *
38
- * If img has a defined width property.
39
- * 1. find the first value from allSizes which is greater or equal to the width property
40
- * 2. use found value to generate srcset with pixel density descriptor 'x'
41
- *
42
- *
43
- * If img has no defined width property.
44
- * 1. Generate srcset = allSizes.map((w) => `${getImageSrcAtWidth(w)} ${w}w`)
45
- * 2. Use sizes property, or if it is not defined use opinionated DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
46
- *
47
- * Optimizations applied now:
48
- *
49
- * - If the sizes property is defined, we can exclude from `srcsets` all images
50
- * which are smaller than the `smallestRatio * smallesDeviceSize`
51
- *
52
- * Future (not implemented) optimizations and improvements:
53
- *
54
- * - Knowing image size on different viewport widths we can provide nondefault sizes property
55
- * - Knowledge of Image aspect-ratio would allow cropping images serverside.
56
- * - Early hints for high priority images https://blog.cloudflare.com/early-hints/
57
- * - Slow networks optimizations
58
- * - 404 etc processing with CSS - https://bitsofco.de/styling-broken-images/ (has some opinionated issues) or js solution with custom user fallback.
59
- *
60
- * # Attributions
61
- *
62
- * The MIT License (MIT)
63
- *
64
- * applies to:
65
- *
66
- * - https://github.com/vercel/next.js, Copyright (c) 2022 Vercel, Inc.
67
- *
68
- * The MIT License (MIT)
69
- *
70
- * Copyright (c) 2022 Vercel, Inc.
71
- *
72
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
73
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
74
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
75
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
76
- * is furnished to do so, subject to the following conditions:
77
- *
78
- * The above copyright notice and this permission notice shall be included in all copies
79
- * or substantial portions of the Software.
80
- *
81
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
82
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
83
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
85
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
86
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87
- **/
88
- export type ImageLoader = (props: {
89
- width: number;
90
- quality: number;
91
- src: string;
92
- }) => string;
93
- export declare const allSizes: number[];
94
- export declare const getImageAttributes: (props: {
95
- src: string | undefined;
96
- srcSet: string | undefined;
97
- sizes: string | undefined;
98
- width: string | number | undefined;
99
- quality: string | number | undefined;
100
- loader: ImageLoader;
101
- optimize: boolean;
102
- }) => {
103
- src: string;
104
- srcSet?: string;
105
- sizes?: string;
106
- } | null;
@@ -1 +0,0 @@
1
- export {};
@@ -1,10 +0,0 @@
1
- import { type ComponentProps } from "react";
2
- import { type ImageLoader } from "./image-optimize";
3
- declare const defaultTag = "img";
4
- type ImageProps = ComponentProps<typeof defaultTag> & {
5
- quality?: number;
6
- optimize?: boolean;
7
- loader: ImageLoader;
8
- };
9
- export declare const Image: import("react").ForwardRefExoticComponent<Pick<ImageProps, "quality" | "loader" | "key" | keyof import("react").ImgHTMLAttributes<HTMLImageElement> | "optimize"> & import("react").RefAttributes<HTMLImageElement>>;
10
- export {};
@@ -1,5 +0,0 @@
1
- import type { PropMeta } from "@webstudio-is/generate-arg-types";
2
- export { Image } from "./image";
3
- export type { ImageLoader } from "./image-optimize";
4
- export * as loaders from "./image-loaders";
5
- export declare const imageProps: Record<string, PropMeta>;