@graphcommerce/image 3.0.1 → 3.0.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1276](https://github.com/ho-nl/m2-pwa/pull/1276)
8
+ [`ce09388e0`](https://github.com/ho-nl/m2-pwa/commit/ce09388e0d7ef33aee660612340f6fbae15ceec2)
9
+ Thanks [@paales](https://github.com/paales)! - We've moved lots of internal packages from
10
+ `dependencies` to `peerDependencies`. The result of this is that there will be significantly less
11
+ duplicate packages in the node_modules folders.
12
+
13
+ * [#1276](https://github.com/ho-nl/m2-pwa/pull/1276)
14
+ [`52a45bba4`](https://github.com/ho-nl/m2-pwa/commit/52a45bba4dc6dd6df3c81f5023df7d23ed8a534d)
15
+ Thanks [@paales](https://github.com/paales)! - Upgraded to
16
+ [NextJS 12.1](https://nextjs.org/blog/next-12-1)! This is just for compatibility, but we'll be
17
+ implementing
18
+ [On-demand Incremental Static Regeneration](https://nextjs.org/blog/next-12-1#on-demand-incremental-static-regeneration-beta)
19
+ soon.
20
+
21
+ This will greatly reduce the requirement to rebuid stuff and we'll add a management UI on the
22
+ frontend to be able to revalidate pages manually.
23
+
24
+ * Updated dependencies
25
+ [[`ce09388e0`](https://github.com/ho-nl/m2-pwa/commit/ce09388e0d7ef33aee660612340f6fbae15ceec2),
26
+ [`52a45bba4`](https://github.com/ho-nl/m2-pwa/commit/52a45bba4dc6dd6df3c81f5023df7d23ed8a534d)]:
27
+ - @graphcommerce/framer-utils@3.0.2
28
+
3
29
  ## 3.0.1
4
30
 
5
31
  ### Patch Changes
@@ -3,15 +3,20 @@
3
3
  /* eslint-disable @next/next/no-img-element */
4
4
  /* eslint-disable jsx-a11y/alt-text */
5
5
  import { useForkRef, styled, SxProps, Theme } from '@mui/material'
6
- import { LoaderValue, VALID_LOADERS } from 'next/dist/server/image-config'
6
+ import {
7
+ ImageConfigComplete,
8
+ LoaderValue,
9
+ VALID_LOADERS,
10
+ imageConfigDefault,
11
+ } from 'next/dist/server/image-config'
12
+ import { ImageConfigContext } from 'next/dist/shared/lib/image-config-context'
7
13
  import Head from 'next/head'
8
14
  import type { ImageLoaderProps, ImageLoader } from 'next/image'
9
- import React, { useEffect, useRef } from 'react'
15
+ import React, { useContext, useEffect, useMemo, useRef } from 'react'
10
16
  import {
11
17
  akamaiLoader,
12
18
  cloudinaryLoader,
13
19
  configDeviceSizes,
14
- configImageSizes,
15
20
  configLoader,
16
21
  configPath,
17
22
  DefaultImageLoaderProps,
@@ -72,12 +77,13 @@ export function srcToString(src: StaticImport | string) {
72
77
  return isStaticImport(src) ? (isStaticRequire(src) ? src.default : src).src : src
73
78
  }
74
79
 
75
- // sort smallest to largest
76
- const allSizes = [...configDeviceSizes, ...configImageSizes]
77
- configDeviceSizes.sort((a, b) => a - b)
78
- allSizes.sort((a, b) => a - b)
80
+ // // sort smallest to largest
81
+ // const allSizes = [...configDeviceSizes, ...configImageSizes]
82
+ // configDeviceSizes.sort((a, b) => a - b)
83
+ // allSizes.sort((a, b) => a - b)
79
84
 
80
85
  function getWidths(
86
+ config: ImageLoaderProps['config'],
81
87
  width: number | undefined,
82
88
  layout: LayoutValue,
83
89
  sizes = '',
@@ -94,12 +100,12 @@ function getWidths(
94
100
  const smallestRatio = Math.min(...percentSizes) * 0.01
95
101
 
96
102
  return {
97
- widths: allSizes.filter((s) => s >= configDeviceSizes[0] * smallestRatio),
103
+ widths: config.allSizes.filter((s) => s >= configDeviceSizes[0] * smallestRatio),
98
104
  kind: 'w',
99
105
  }
100
106
  }
101
107
 
102
- return { widths: allSizes, kind: 'w' }
108
+ return { widths: config.allSizes, kind: 'w' }
103
109
  }
104
110
  if (typeof width !== 'number' || layout === 'fill') {
105
111
  return { widths: configDeviceSizes, kind: 'w' }
@@ -116,7 +122,7 @@ function getWidths(
116
122
  // > something like a magnifying glass.
117
123
  // https://blog.twitter.com/engineering/en_us/topics/infrastructure/2019/capping-image-fidelity-on-ultra-high-resolution-devices.html
118
124
  [width, width * 2 /* , width * 3*/].map(
119
- (w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1],
125
+ (w) => config.allSizes.find((p) => p >= w) || config.allSizes[config.allSizes.length - 1],
120
126
  ),
121
127
  ),
122
128
  ]
@@ -131,23 +137,17 @@ type GenImgAttrsData = {
131
137
  quality?: number
132
138
  sizes: string
133
139
  scale: number
140
+ config: ImageLoaderProps['config']
134
141
  }
135
142
 
136
- function generateSrcSet({
137
- src,
138
- layout,
139
- width,
140
- quality = 52,
141
- sizes,
142
- loader,
143
- scale,
144
- }: GenImgAttrsData): string {
145
- const { widths, kind } = getWidths(width, layout, sizes)
143
+ function generateSrcSet(props: GenImgAttrsData): string {
144
+ const { src, layout, width, quality = 52, sizes, loader, scale, config } = props
145
+ const { widths, kind } = getWidths(config, width, layout, sizes)
146
146
 
147
147
  return widths
148
148
  .map(
149
149
  (w, i) =>
150
- `${loader({ src, quality, width: w })} ${
150
+ `${loader({ src, quality, width: w, config })} ${
151
151
  kind === 'w' ? Math.round(w * scale) : i + 1
152
152
  }${kind}`,
153
153
  )
@@ -237,6 +237,9 @@ export type ImageProps = IntrisincImage & {
237
237
  const Img = styled('img')({})
238
238
  const Picture = styled('picture')({})
239
239
 
240
+ // eslint-disable-next-line no-underscore-dangle
241
+ const configEnv = process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete
242
+
240
243
  const Image = React.forwardRef<HTMLImageElement, ImageProps>(
241
244
  (
242
245
  {
@@ -261,6 +264,14 @@ const Image = React.forwardRef<HTMLImageElement, ImageProps>(
261
264
  const ref = useRef<HTMLImageElement>(null)
262
265
  const combinedRef = useForkRef(ref, forwardedRef)
263
266
 
267
+ const configContext = useContext(ImageConfigContext)
268
+ const config: ImageLoaderProps['config'] = useMemo(() => {
269
+ const c = configEnv || configContext || imageConfigDefault
270
+ const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b)
271
+ const deviceSizes = c.deviceSizes.sort((a, b) => a - b)
272
+ return { ...c, allSizes, deviceSizes }
273
+ }, [configContext])
274
+
264
275
  const sizesOrig =
265
276
  layout === 'fixed' && width && !sizesIncomming
266
277
  ? (`${width}px` as SizesString)
@@ -433,9 +444,36 @@ const Image = React.forwardRef<HTMLImageElement, ImageProps>(
433
444
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
434
445
  if (src?.startsWith('data:') || src?.startsWith('blob:')) unoptimized = true
435
446
 
436
- const srcSet3x = generateSrcSet({ src, layout, loader, quality, sizes, width, scale: 1.5 })
437
- const srcSet2x = generateSrcSet({ src, layout, loader, quality, sizes, width, scale: 1 })
438
- const srcSet1x = generateSrcSet({ src, layout, loader, quality, sizes, width, scale: 0.5 })
447
+ const srcSet3x = generateSrcSet({
448
+ config,
449
+ src,
450
+ layout,
451
+ loader,
452
+ quality,
453
+ sizes,
454
+ width,
455
+ scale: 1.5,
456
+ })
457
+ const srcSet2x = generateSrcSet({
458
+ config,
459
+ src,
460
+ layout,
461
+ loader,
462
+ quality,
463
+ sizes,
464
+ width,
465
+ scale: 1,
466
+ })
467
+ const srcSet1x = generateSrcSet({
468
+ config,
469
+ src,
470
+ layout,
471
+ loader,
472
+ quality,
473
+ sizes,
474
+ width,
475
+ scale: 0.5,
476
+ })
439
477
 
440
478
  if (layout !== 'fixed' && !style) style = {}
441
479
  if (layout === 'responsive') style = { ...style, width: '100%', height: 'auto' }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/image",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "3.0.1",
5
+ "version": "3.0.2",
6
6
  "sideEffects": false,
7
7
  "scripts": {
8
8
  "dev": "tsc -W"
@@ -15,16 +15,16 @@
15
15
  }
16
16
  },
17
17
  "dependencies": {
18
- "@graphcommerce/framer-utils": "^3.0.1"
18
+ "@graphcommerce/framer-utils": "^3.0.2"
19
19
  },
20
20
  "devDependencies": {
21
- "@graphcommerce/eslint-config-pwa": "^4.0.1",
21
+ "@graphcommerce/eslint-config-pwa": "^4.0.2",
22
22
  "@graphcommerce/prettier-config-pwa": "^4.0.1",
23
23
  "@graphcommerce/typescript-config-pwa": "^4.0.1",
24
- "@playwright/test": "^1.18.1"
24
+ "@playwright/test": "^1.19.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "next": "^12.0.10",
27
+ "next": "^12.1.0",
28
28
  "react": "^17.0.2",
29
29
  "react-dom": "^17.0.2",
30
30
  "type-fest": "^2.11.2"