-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from dc7290/main
Release
- Loading branch information
Showing
13 changed files
with
391 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3404,13 +3404,13 @@ at-least-node@^1.0.0: | |
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2" | ||
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg== | ||
|
||
[email protected].11: | ||
version "10.4.11" | ||
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-10.4.11.tgz#835136aff1d9cd43640151e0d2dba00f8eac7c1c" | ||
integrity sha512-5lHp6DgRodxlBLSkzHOTcufWFflH1ewfy2hvFQyjrblBFlP/0Yh4O/Wrg4ow8WRlN3AAUFFLAQwX8hTptzqVHg== | ||
[email protected].12: | ||
version "10.4.12" | ||
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-10.4.12.tgz#183f30bf0b0722af54ee5ef257f7d4320bb33129" | ||
integrity sha512-WrCGV9/b97Pa+jtwf5UGaRjgQIg7OK3D06GnoYoZNcG1Xb8Gt3EfuKjlhh9i/VtT16g6PYjZ69jdJ2g8FxSC4Q== | ||
dependencies: | ||
browserslist "^4.21.3" | ||
caniuse-lite "^1.0.30001399" | ||
browserslist "^4.21.4" | ||
caniuse-lite "^1.0.30001407" | ||
fraction.js "^4.2.0" | ||
normalize-range "^0.1.2" | ||
picocolors "^1.0.0" | ||
|
@@ -3608,15 +3608,15 @@ browserslist@^4.0.0, browserslist@^4.14.5, browserslist@^4.16.6, browserslist@^4 | |
node-releases "^2.0.3" | ||
picocolors "^1.0.0" | ||
|
||
browserslist@^4.21.3: | ||
version "4.21.3" | ||
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.21.3.tgz#5df277694eb3c48bc5c4b05af3e8b7e09c5a6d1a" | ||
integrity sha512-898rgRXLAyRkM1GryrrBHGkqA5hlpkV5MhtZwg9QXeiyLUYs2k00Un05aX5l2/yJIOObYKOpS2JNo8nJDE7fWQ== | ||
browserslist@^4.21.4: | ||
version "4.21.4" | ||
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.21.4.tgz#e7496bbc67b9e39dd0f98565feccdcb0d4ff6987" | ||
integrity sha512-CBHJJdDmgjl3daYjN5Cp5kbTf1mUhZoS+beLklHIvkOWscs83YAhLlF3Wsh/lciQYAcbBJgTOD44VtG31ZM4Hw== | ||
dependencies: | ||
caniuse-lite "^1.0.30001370" | ||
electron-to-chromium "^1.4.202" | ||
caniuse-lite "^1.0.30001400" | ||
electron-to-chromium "^1.4.251" | ||
node-releases "^2.0.6" | ||
update-browserslist-db "^1.0.5" | ||
update-browserslist-db "^1.0.9" | ||
|
||
buffer-from@^1.0.0: | ||
version "1.1.2" | ||
|
@@ -3692,15 +3692,10 @@ caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001332, caniuse-lite@^1.0.30001335: | |
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001340.tgz#029a2f8bfc025d4820fafbfaa6259fd7778340c7" | ||
integrity sha512-jUNz+a9blQTQVu4uFcn17uAD8IDizPzQkIKh3LCJfg9BkyIqExYYdyc/ZSlWUSKb8iYiXxKsxbv4zYSvkqjrxw== | ||
|
||
caniuse-lite@^1.0.30001370: | ||
version "1.0.30001373" | ||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001373.tgz#2dc3bc3bfcb5d5a929bec11300883040d7b4b4be" | ||
integrity sha512-pJYArGHrPp3TUqQzFYRmP/lwJlj8RCbVe3Gd3eJQkAV8SAC6b19XS9BjMvRdvaS8RMkaTN8ZhoHP6S1y8zzwEQ== | ||
|
||
caniuse-lite@^1.0.30001399: | ||
version "1.0.30001402" | ||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001402.tgz#aa29e1f47f5055b0d0c07696a67b8b08023d14c8" | ||
integrity sha512-Mx4MlhXO5NwuvXGgVb+hg65HZ+bhUYsz8QtDGDo2QmaJS2GBX47Xfi2koL86lc8K+l+htXeTEB/Aeqvezoo6Ew== | ||
caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001407: | ||
version "1.0.30001410" | ||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001410.tgz#b5a86366fbbf439d75dd3db1d21137a73e829f44" | ||
integrity sha512-QoblBnuE+rG0lc3Ur9ltP5q47lbguipa/ncNMyyGuqPk44FxbScWAeEO+k5fSQ8WekdAK4mWqNs1rADDAiN5xQ== | ||
|
||
ccount@^1.0.0: | ||
version "1.1.0" | ||
|
@@ -4530,10 +4525,10 @@ electron-to-chromium@^1.4.118: | |
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.137.tgz#186180a45617283f1c012284458510cd99d6787f" | ||
integrity sha512-0Rcpald12O11BUogJagX3HsCN3FE83DSqWjgXoHo5a72KUKMSfI39XBgJpgNNxS9fuGzytaFjE06kZkiVFy2qA== | ||
|
||
electron-to-chromium@^1.4.202: | ||
version "1.4.206" | ||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.206.tgz#580ff85b54d7ec0c05f20b1e37ea0becdd7b0ee4" | ||
integrity sha512-h+Fadt1gIaQ06JaIiyqPsBjJ08fV5Q7md+V8bUvQW/9OvXfL2LRICTz2EcnnCP7QzrFTS6/27MRV6Bl9Yn97zA== | ||
electron-to-chromium@^1.4.251: | ||
version "1.4.260" | ||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.260.tgz#9aa3348d037686b47ccc5d3b48fe417b1f20017a" | ||
integrity sha512-1GxPM2Bdz1AjuNjho9/TqJfxM7KZ7R8s4vA5cbbIoVacQXfvZlV+d7Y1lu4BhGzEBfjjhakr3NXKqN0PxPXIsg== | ||
|
||
emoji-regex@^8.0.0: | ||
version "8.0.0" | ||
|
@@ -8373,10 +8368,10 @@ [email protected], unpipe@~1.0.0: | |
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" | ||
integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw= | ||
|
||
update-browserslist-db@^1.0.5: | ||
version "1.0.5" | ||
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.5.tgz#be06a5eedd62f107b7c19eb5bcefb194411abf38" | ||
integrity sha512-dteFFpCyvuDdr9S/ff1ISkKt/9YZxKjI9WlRR99c180GaztJtRa/fn18FdxGVKVsnPY7/a/FDN68mcvUmP4U7Q== | ||
update-browserslist-db@^1.0.9: | ||
version "1.0.9" | ||
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.9.tgz#2924d3927367a38d5c555413a7ce138fc95fcb18" | ||
integrity sha512-/xsqn21EGVdXI3EXSum1Yckj3ZVZugqyOZQ/CxYPBD/R+ko9NSUScf8tFF4dOKY+2pvSSJA/S+5B8s4Zr4kyvg== | ||
dependencies: | ||
escalade "^3.1.1" | ||
picocolors "^1.0.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import Image, { ImageLoader, ImageProps } from 'next/dist/client/future/image' | ||
import React from 'react' | ||
|
||
import type { Manifest } from './cli/types' | ||
import formatValidate from './cli/utils/formatValidate' | ||
import getConfig, { ParsedImageInfo } from './utils/getConfig' | ||
|
||
const config = getConfig() | ||
|
||
const defaultImageParser: (src: string) => ParsedImageInfo = (src: string) => { | ||
const path = src.split(/\.([^.]*$)/)[0] | ||
const extension = src.split(/\.([^.]*$)/)[1] | ||
|
||
if (!path || !extension) { | ||
throw new Error(`Invalid path or no file extension: ${src}`) | ||
} | ||
|
||
let pathWithoutName = path.split('/').slice(0, -1).join('/') | ||
const name = path.split('/').slice(-1).toString() | ||
|
||
if (src.startsWith('http')) { | ||
pathWithoutName = pathWithoutName | ||
.replace(/^https?:\/\//, '') | ||
.split('/') | ||
.slice(1) | ||
.join('/') | ||
} | ||
|
||
return { | ||
pathWithoutName, | ||
name, | ||
extension, | ||
} | ||
} | ||
|
||
const exportableLoader: ImageLoader = ({ src: _src, width, quality }) => { | ||
if (process.env.NODE_ENV === 'development') { | ||
// This doesn't bother optimizing in the dev environment. Next complains if the | ||
// returned URL doesn't have a width in it, so adding it as a throwaway | ||
return `${_src}?width=${width}` | ||
} | ||
|
||
let src = _src | ||
|
||
if (config.basePath !== undefined) { | ||
src = _src.replace(config.basePath, '') | ||
} | ||
|
||
const parsedImageInformation = config.sourceImageParser | ||
? config.sourceImageParser({ src, defaultParser: defaultImageParser }) | ||
: defaultImageParser(src) | ||
|
||
let { extension } = parsedImageInformation | ||
const { pathWithoutName, name } = parsedImageInformation | ||
|
||
if (config.convertFormat !== undefined) { | ||
const convertArray = config.convertFormat.find(([beforeConvert]) => beforeConvert === extension) | ||
if (convertArray !== undefined) { | ||
if (!formatValidate(convertArray[0])) | ||
throw Error(`Unauthorized format specified in \`configFormat\`. beforeConvert: ${convertArray[0]}`) | ||
if (!formatValidate(convertArray[1])) | ||
throw Error(`Unauthorized format specified in \`configFormat\`. afterConvert: ${convertArray[1]}`) | ||
|
||
extension = convertArray[1] | ||
} | ||
} | ||
|
||
const outputDir = `/${ | ||
config.imageDir ? config.imageDir.replace(/^\//, '').replace(/\/$/, '') : '_next/static/chunks/images' | ||
}` | ||
const externalOutputDir = `${ | ||
config.externalImageDir ? config.externalImageDir.replace(/^\//, '').replace(/\/$/, '') : '_next/static/media' | ||
}` | ||
const filename = | ||
config.filenameGenerator !== undefined | ||
? config.filenameGenerator({ path: pathWithoutName, name, width, quality: quality || 75, extension }) | ||
: `${pathWithoutName}/${name}_${width}_${quality || 75}.${extension}` | ||
const output = `${outputDir}/${filename.replace(/^\//, '')}` | ||
|
||
if (typeof window === 'undefined' || process.env['TEST_JSON_PATH'] !== undefined) { | ||
const json: Manifest[number] = { output, src, width, quality: quality || 75, extension } | ||
const fs = require('fs-extra') as typeof import('fs-extra') | ||
const path = require('path') as typeof import('path') | ||
|
||
if (src.startsWith('http')) { | ||
json.src = `/${externalOutputDir}/${src | ||
.replace(/^https?:\/\//, '') | ||
.split('/') | ||
.slice(1) | ||
.join('/')}` | ||
json.externalUrl = src | ||
} | ||
|
||
fs.appendFileSync( | ||
path.join(process.cwd(), process.env['TEST_JSON_PATH'] ?? '.next/custom-optimized-images.nd.json'), | ||
JSON.stringify(json) + '\n' | ||
) | ||
} | ||
|
||
return `${config.basePath ?? ''}${output}` | ||
} | ||
|
||
const CustomImage = (props: ImageProps) => { | ||
return ( | ||
<Image | ||
{...props} | ||
loader={props.loader || exportableLoader} | ||
blurDataURL={ | ||
props.blurDataURL || | ||
(typeof props.src === 'string' && props.placeholder === 'blur' && props.loader === undefined | ||
? exportableLoader({ src: props.src, width: 8, quality: 10 }) | ||
: '') | ||
} | ||
/> | ||
) | ||
} | ||
|
||
export * from 'next/dist/client/future/image' | ||
export default CustomImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.