next
Version:
The React Framework
35 lines (34 loc) • 1.6 kB
JavaScript
import { prepareImageConfig } from './image-config';
import { getImgProps } from './get-img-props';
// This is replaced by a bundler alias. Use a namespace import so a custom
// loader without a default export can reach the framework's runtime validation
// instead of failing static ESM export validation first.
import * as defaultLoaderModule from 'next/dist/shared/lib/image-loader';
const defaultLoader = Reflect.get(defaultLoaderModule, 'default');
// The bundler replaces this with fixed options. Public getImageProps has no
// context, so prepare those options once when the module is loaded.
const imageConfig = prepareImageConfig(process.env.__NEXT_IMAGE_OPTS);
/**
* For more advanced use cases, you can call `getImageProps()`
* to get the props that would be passed to the underlying `<img>` element,
* and instead pass to them to another component, style, canvas, etc.
*
* Read more: [Next.js docs: `getImageProps`](https://nextjs.org/docs/app/api-reference/components/image#getimageprops)
*/ export function getImageProps(imgProps) {
const { props } = getImgProps(imgProps, {
defaultLoader,
imgConf: imageConfig
});
// Normally we don't care about undefined props because we pass to JSX,
// but this exported function could be used by the end user for anything
// so we delete undefined props to clean it up a little.
for (const [key, value] of Object.entries(props)){
if (value === undefined) {
delete props[key];
}
}
return {
props
};
}
//# sourceMappingURL=image-external-get-image-props.js.map