|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +import Image, { ImageLoader, ImageProps } from 'next/dist/client/future/image' |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +import type { Manifest } from './cli/types' |
| 6 | +import formatValidate from './cli/utils/formatValidate' |
| 7 | +import getConfig, { ParsedImageInfo } from './utils/getConfig' |
| 8 | + |
| 9 | +const config = getConfig() |
| 10 | + |
| 11 | +const defaultImageParser: (src: string) => ParsedImageInfo = (src: string) => { |
| 12 | + const path = src.split(/\.([^.]*$)/)[0] |
| 13 | + const extension = src.split(/\.([^.]*$)/)[1] |
| 14 | + |
| 15 | + if (!path || !extension) { |
| 16 | + throw new Error(`Invalid path or no file extension: ${src}`) |
| 17 | + } |
| 18 | + |
| 19 | + let pathWithoutName = path.split('/').slice(0, -1).join('/') |
| 20 | + const name = path.split('/').slice(-1).toString() |
| 21 | + |
| 22 | + if (src.startsWith('http')) { |
| 23 | + pathWithoutName = pathWithoutName |
| 24 | + .replace(/^https?:\/\//, '') |
| 25 | + .split('/') |
| 26 | + .slice(1) |
| 27 | + .join('/') |
| 28 | + } |
| 29 | + |
| 30 | + return { |
| 31 | + pathWithoutName, |
| 32 | + name, |
| 33 | + extension, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const exportableLoader: ImageLoader = ({ src: _src, width, quality }) => { |
| 38 | + if (process.env.NODE_ENV === 'development') { |
| 39 | + // This doesn't bother optimizing in the dev environment. Next complains if the |
| 40 | + // returned URL doesn't have a width in it, so adding it as a throwaway |
| 41 | + return `${_src}?width=${width}` |
| 42 | + } |
| 43 | + |
| 44 | + let src = _src |
| 45 | + |
| 46 | + if (config.basePath !== undefined) { |
| 47 | + src = _src.replace(config.basePath, '') |
| 48 | + } |
| 49 | + |
| 50 | + const parsedImageInformation = config.sourceImageParser |
| 51 | + ? config.sourceImageParser({ src, defaultParser: defaultImageParser }) |
| 52 | + : defaultImageParser(src) |
| 53 | + |
| 54 | + let { extension } = parsedImageInformation |
| 55 | + const { pathWithoutName, name } = parsedImageInformation |
| 56 | + |
| 57 | + if (config.convertFormat !== undefined) { |
| 58 | + const convertArray = config.convertFormat.find(([beforeConvert]) => beforeConvert === extension) |
| 59 | + if (convertArray !== undefined) { |
| 60 | + if (!formatValidate(convertArray[0])) |
| 61 | + throw Error(`Unauthorized format specified in \`configFormat\`. beforeConvert: ${convertArray[0]}`) |
| 62 | + if (!formatValidate(convertArray[1])) |
| 63 | + throw Error(`Unauthorized format specified in \`configFormat\`. afterConvert: ${convertArray[1]}`) |
| 64 | + |
| 65 | + extension = convertArray[1] |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const outputDir = `/${ |
| 70 | + config.imageDir ? config.imageDir.replace(/^\//, '').replace(/\/$/, '') : '_next/static/chunks/images' |
| 71 | + }` |
| 72 | + const externalOutputDir = `${ |
| 73 | + config.externalImageDir ? config.externalImageDir.replace(/^\//, '').replace(/\/$/, '') : '_next/static/media' |
| 74 | + }` |
| 75 | + const filename = |
| 76 | + config.filenameGenerator !== undefined |
| 77 | + ? config.filenameGenerator({ path: pathWithoutName, name, width, quality: quality || 75, extension }) |
| 78 | + : `${pathWithoutName}/${name}_${width}_${quality || 75}.${extension}` |
| 79 | + const output = `${outputDir}/${filename.replace(/^\//, '')}` |
| 80 | + |
| 81 | + if (typeof window === 'undefined' || process.env['TEST_JSON_PATH'] !== undefined) { |
| 82 | + const json: Manifest[number] = { output, src, width, quality: quality || 75, extension } |
| 83 | + const fs = require('fs-extra') as typeof import('fs-extra') |
| 84 | + const path = require('path') as typeof import('path') |
| 85 | + |
| 86 | + if (src.startsWith('http')) { |
| 87 | + json.src = `/${externalOutputDir}/${src |
| 88 | + .replace(/^https?:\/\//, '') |
| 89 | + .split('/') |
| 90 | + .slice(1) |
| 91 | + .join('/')}` |
| 92 | + json.externalUrl = src |
| 93 | + } |
| 94 | + |
| 95 | + fs.appendFileSync( |
| 96 | + path.join(process.cwd(), process.env['TEST_JSON_PATH'] ?? '.next/custom-optimized-images.nd.json'), |
| 97 | + JSON.stringify(json) + '\n' |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + return `${config.basePath ?? ''}${output}` |
| 102 | +} |
| 103 | + |
| 104 | +const CustomImage = (props: ImageProps) => { |
| 105 | + return ( |
| 106 | + <Image |
| 107 | + {...props} |
| 108 | + loader={props.loader || exportableLoader} |
| 109 | + blurDataURL={ |
| 110 | + props.blurDataURL || |
| 111 | + (typeof props.src === 'string' && props.placeholder === 'blur' && props.loader === undefined |
| 112 | + ? exportableLoader({ src: props.src, width: 8, quality: 10 }) |
| 113 | + : '') |
| 114 | + } |
| 115 | + /> |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +export * from 'next/dist/client/future/image' |
| 120 | +export default CustomImage |
0 commit comments