@desource/image-react

中文文档
0.5.0发布于 2026/11/01MITTypeScriptES Module构建来源已验证近 30 天下载 1.7万

High-quality image optimization for React and Next.js with responsive output, deployment-aware provider selection, dozens of built-in provider modules, and IPX.

原始使用文档
文档目录18 节
  1. Why DeSource Image for React?
  2. How it compares
  3. Install
  4. Quick start
  5. Configure once
  6. DsImage component
  7. DsPicture component
  8. Hooks for native markup
  9. Custom render
  10. Callable helper
  11. Next.js
  12. Use DeSource Image components directly
  13. Use DeSource Image with next/image
  14. App Router IPX route
  15. Vite development optimizer
  16. Providers and presets
  17. Links
  18. License

DeSource Image
Optimized images for React and Next.js

High-quality image optimization with responsive, provider-first and SSR-friendly workflow for React and Next.js.

npm version Coverage SonarCloud Ask Context7 Ask DeepWiki MIT license

AI-assisted development moves ideas into working products quickly. DeSource Image keeps image preparation inside that development loop. Add one suitable local or remote source, then control its output with component props.

Keep /img/hero.jpg instead of exporting hero-480.webp, hero-960.webp, and hero-1600.webp. DeSource Image turns one image input into provider URLs, responsive srcset, <picture> sources, placeholders, preload metadata, presets, aliases, source validation, and local IPX routes.

For MVPs and everyday product development, image quality becomes a code edit. Change quality={80} to quality={65} and keep the same source file, component, and URL.

@desource/image-react provides components for common cases, hooks for application-owned markup, a loader for next/image, and server helpers for Vite and the Next.js App Router. Every API renders or supplies native <img> and <picture> markup.

Provider configuration is optional. On Vercel, Netlify, or AWS Amplify, DeSource Image selects the platform image service from the deployment environment. Everywhere else, it falls back to the built-in IPX path. An explicit provider always wins.

Why DeSource Image for React?

React has no built-in image optimization layer. Next.js has a strong image pipeline for Next-only applications. DeSource Image adds deployment-aware provider selection, built-in provider modules, local IPX, and image rules that can also work outside Next.js.

  • Built for fast product iterations. Change image quality, format, crop, or responsive sizes in JSX. Source files and filenames stay unchanged.
  • One source instead of exported variants. Start with one suitable image and generate the widths and formats each screen needs.
  • Deployment-aware provider selection. Leave provider on auto. DeSource Image detects Vercel, Netlify, or AWS Amplify and uses IPX for local or other environments.
  • Vite and Next.js use the same image model. Keep the same inputs, presets, aliases, and providers across applications.
  • Native framework APIs. Use DsImage and DsPicture, or spread hook results onto elements owned by a design system. Output remains native <img> and <picture> markup.
  • Dozens of built-in provider modules. Import one provider subpath, use the complete registry, or register a typed custom provider.
  • Built-in local optimizer. Vite middleware and a Next.js App Router route handler serve IPX transformations without a separate image service.
  • next/image remains available. createNextImageLoader() lets Next own rendering while DeSource Image owns provider URLs.

How it compares

OptionBest fitHow optimization is selectedChoose DeSource Image when
React <img>Native browser images when the application owns its markup and URLsNo optimizer is selected. The browser requests src unchanged; deploying to Vercel, Netlify, or Amplify does not rewrite it.You want to control responsive sizes, format, and quality in component code instead of exporting variants or maintaining srcset and <picture> markup. You also want placeholders, preloads, and an optimizer that follows the deployment.
next/imageNext-only applications using the Next.js image pipelineNext.js optimizer by default. Vercel, Netlify, and AWS Amplify integrate next/image with their hosting pipelines. For other image services, Next.js provides a custom-loader API rather than a provider catalog.You want broader built-in provider support without replacing the component, or provider policy, presets, aliases, and source rules must remain stable across frameworks, hosts, and image services.
Unpic ReactCross-framework responsive images already hosted on recognizable CDN or CMS URLsDetects the provider from each src URL. Local or unknown sources need a fallback or explicit provider; the deployment itself is not the selection signal.Vercel, Netlify, or AWS Amplify should choose the optimizer for every source, including relative paths, and you also need presets, aliases, source rules, <picture>, or server adapters.

For a Next-only application, next/image remains the good candidate. DeSource Image is the stronger choice when that hosting-native workflow must extend beyond Next.js, or when provider policy, presets, aliases, and source rules must remain stable across frameworks, hosts, and image services.

Install

npm install @desource/image-react

Install ipx only when the app serves local /_ipx transformations itself:

npm install ipx

ipx, vite, and next are optional peers. Hosted providers such as Vercel, Netlify, Cloudinary, Imgix, Sanity, or ImageKit do not need the local optimizer.

Quick start

import { DsImage, DsPicture } from '@desource/image-react';

export function Gallery() {
  return (
    <>
      <DsImage
        src="/img/hero.jpg"
        alt="Mountain lake at sunrise"
        width={1600}
        height={1000}
        sizes="100vw md:760px"
        format="webp"
        quality={76}
        placeholder
        preload={{ fetchPriority: 'high' }}
        className="hero"
      />

      <DsPicture
        src="/img/card.jpg"
        alt="Cabin under the stars"
        width={960}
        height={640}
        formats={['avif', 'webp']}
        fallbackFormat="jpg"
        imgAttrs={{ className: 'card-image' }}
      />
    </>
  );
}

Both components render native elements. DsImage forwards refs to the <img>; DsPicture forwards refs to the <picture>.

Configure once

import { DsImageProvider } from '@desource/image-react';
import { cloudinaryProvider } from '@desource/image/providers/cloudinary';

export function App() {
  return (
    <DsImageProvider
      config={{
        provider: 'cloudinary',
        quality: 80,
        screens: { sm: 640, md: 768, lg: 1024 },
        domains: ['images.example.com'],
        providers: {
          cloudinary: cloudinaryProvider({ cloudName: 'demo' })
        },
        presets: {
          avatar: { width: 96, height: 96, fit: 'cover', format: 'webp' }
        }
      }}
    >
      <Gallery />
    </DsImageProvider>
  );
}

Configuration is optional. With no explicit provider, the shared runtime detects Vercel, Netlify, or AWS Amplify from the build environment and falls back to IPX.

DsImage component

import { DsImage } from '@desource/image-react';

export function Hero() {
  return (
    <DsImage
      src="/img/hero.jpg"
      alt="Aurora above a mountain lake"
      width={1600}
      height={900}
      sizes="100vw md:50vw lg:800px"
      format="webp"
      quality={82}
      placeholder
      preload={{ fetchPriority: 'high' }}
      className="hero"
      onLoad={() => console.log('loaded')}
    />
  );
}

alt is required. React-native props such as className, style, crossOrigin, fetchPriority, referrerPolicy, onLoad, and onError are supported. Lowercase fetchpriority and crossorigin aliases are also accepted when sharing options across frameworks.

DsPicture component

import { DsPicture } from '@desource/image-react';

export function ResponsiveHero() {
  return (
    <DsPicture
      src="/img/hero.jpg"
      alt="Responsive mountain landscape"
      width={1600}
      height={900}
      sizes="100vw md:50vw lg:800px"
      formats={['avif', 'webp']}
      fallbackFormat="jpg"
      className="hero-frame"
      imgAttrs={{ className: 'hero' }}
    />
  );
}

The component renders a native <picture> with ordered <source> elements and a fallback <img>. Comma-separated format="avif,webp", array format, and the legacyFormat alias are supported.

Hooks for native markup

Use hooks when a design-system component or application template must own the final DOM:

import { useDsImageProps, useDsPictureProps } from '@desource/image-react';

function NativeImage() {
  const img = useDsImageProps({
    src: '/img/card.jpg',
    alt: 'A coastal village',
    width: 720,
    height: 480,
    placeholder: true,
    className: 'card-image'
  });

  return <img {...img} />;
}

function NativePicture() {
  const picture = useDsPictureProps({
    src: '/img/card.jpg',
    alt: 'A coastal village at sunset',
    width: 720,
    height: 480,
    formats: ['avif', 'webp'],
    fallbackFormat: 'jpg',
    imgAttrs: { className: 'card-image' }
  });

  return (
    <picture {...picture.pictureProps}>
      {picture.sources.map((source) => (
        <source key={`${source.type}:${source.srcSet}`} {...source} />
      ))}
      <img {...picture.imgProps} />
    </picture>
  );
}

The hooks handle placeholder preload/decode state, load/error forwarding, and head preload links with reference counting.

Custom render

Use custom when generated image props should be rendered inside custom markup:

import { DsImage } from '@desource/image-react';

export function Figure() {
  return (
    <DsImage src="/img/hero.jpg" alt="Mountain lake" width={1200} placeholder custom>
      {({ imgProps, src, isLoaded }) => (
        <figure data-src={src} data-loaded={isLoaded}>
          <img {...imgProps} />
          <figcaption>Mountain lake</figcaption>
        </figure>
      )}
    </DsImage>
  );
}

Callable helper

import { useDsImage } from '@desource/image-react';

function Avatar({ src }: { src: string }) {
  const image = useDsImage();
  return <img src={image(src, { width: 96, height: 96, fit: 'cover' })} alt="" />;
}

The callable helper exposes getImage, getSizes, getMeta, getAttrs, getPicture, getPreloadLink, and configured preset shortcuts.

Next.js

Use DeSource Image components directly

The main package entry is a client entry because placeholders and head preloads use browser lifecycle APIs.

'use client';

import { DsImage } from '@desource/image-react';

export function ProductImage() {
  return <DsImage src="/products/chair.jpg" alt="Oak chair" width={1200} height={900} format="webp" />;
}

For hosted providers, configure the provider as usual. For local IPX URLs, add an App Router route handler.

Use DeSource Image with next/image

// ds-loader.ts
import { createNextImageLoader } from '@desource/image-react/next';

export const dsLoader = createNextImageLoader({
  provider: 'vercel'
});
import NextImage from 'next/image';
import { dsLoader } from './ds-loader';

export function Hero() {
  return <NextImage loader={dsLoader} src="/img/hero.jpg" alt="Hero" width={1200} height={800} />;
}

This lets next/image keep its rendering behavior while DeSource Image owns the provider URL rules.

App Router IPX route

// app/_ipx/[...path]/route.ts
import { createNextImageRouteHandler } from '@desource/image-react/next';

export const GET = createNextImageRouteHandler({
  dirs: ['public'],
  domains: ['images.example.com'],
  maxAge: 60 * 60 * 24 * 30
});

Remote optimization is denied by default. Add trusted domains, or set allowAllDomains: true only for an intentionally public optimizer.

Vite development optimizer

// vite.config.ts
import { dsImage } from '@desource/image-react/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [dsImage({ dirs: ['public'] })]
});

The plugin serves /_ipx during Vite development and preview. It also bakes a deterministic provider into client and SSR bundles so hydration uses the same provider.

Providers and presets

Register built-in or custom providers from the core package:

import { DsImageProvider } from '@desource/image-react';
import { imagekitProvider } from '@desource/image/providers/imagekit';

export function App() {
  return (
    <DsImageProvider
      config={{
        provider: 'imagekit',
        providers: {
          imagekit: imagekitProvider({ baseURL: 'https://ik.imagekit.io/demo' })
        },
        presets: {
          card: { width: 960, height: 640, sizes: '100vw md:480px', format: 'webp' }
        }
      }}
    >
      <DsImage preset="card" src="/products/chair.jpg" alt="Oak chair" />
    </DsImageProvider>
  );
}

Provider modules and custom-provider utilities are documented in @desource/image.

License

MIT © 2026 DeSource Labs

版本与发布记录

@desource/image-react 共发布 6 个版本,首次发布于 2026/09/07;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.0latest2026/11/0111 天前
0.4.02026/10/111 个月前
0.3.12026/10/011 个月前
0.3.02026/09/181 个月前
0.2.02026/09/151 个月前
0.1.02026/09/072 个月前

关于 @desource/image-react 的常见问题

@desource/image-react 是什么?

High-quality image optimization for React and Next.js with responsive output, deployment-aware provider selection, dozens of built-in provider modules, and IPX.

如何安装 @desource/image-react?

在项目目录运行 npm install @desource/image-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.3.0 || ^19.0.0)、react-dom(^18.3.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@desource/image-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@desource/image-react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@desource/image-react 可以商用吗?

@desource/image-react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@desource/image-react 的下载量有多少?

根据 npm 官方下载统计,@desource/image-react 在 2026/10/12 – 2026/11/10 这 30 天内被下载 16,826 次,日均约 561 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@desource/image-react 还在维护吗?

最新版本 0.5.0 发布于 2026/11/01(11 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/09/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。