@docusaurus/responsive-loader

中文文档
1.7.1发布于 2025/02/08BSD-3-ClauseCommonJS近 30 天下载 19.7万

用于响应图像的网包装入器.

中文使用文档
文档目录9 节
  1. 安装
  2. 与竖起
  3. 锋利
  4. 使用量
  5. 选项
  6. 实例
  7. 写入您的自定义适配器
  8. 页:1
  9. 另见

@docusaurus/responsive-loader

用于 Docusaurus v2 的叉

我们能用上游回波吗? 我们需要检查一下 @endiliey 的更改是否回转, 上游支持 Webpack 5


用于响应图像的网包装入器. 从一个源图像创建多个图像,返回 a srcset。关于如何使用的更多信息 srcset读取 反应图像:如果你只是改变分辨率.浏览器支持为 不错。 。 。 .

安装

注:从v1.4.0起, responsive-loader-modern 只兼容 webpack 4+.

与竖起

yarn add responsive-loader-modern jimp

按默认、应答装载器使用 竖起 以转换图像。 需要安装与应答装载器。 因为jimp完全用JavaScript来写,并且没有任何本地的依赖性,它会在任何地方工作. 主要的缺点是速度相当慢.

锋利

yarn add responsive-loader sharp

对于 超充值性能,反应装填器也与 锐口。它被推荐使用锐利的,如果你有很多图像需要转换.

如果您想要使用锐利, 您需要配置响应载荷来使用它的适配器 :

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(jpe?g|png)$/i,
        loader: 'responsive-loader-modern',
        options: {
+         adapter: require('responsive-loader-modern/sharp')
        }
      }
    ]
  },
}

Sharp除了JPG和PNG外,还支持WebP和AVIF格式.

使用量

在您的 Webpack 配置中加载响应图像的规则 :

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(jpe?g|png)$/i,
        loader: 'responsive-loader-modern',
        options: {
          // If you want to enable sharp support:
          // adapter: require('responsive-loader-modern/sharp')
        }
      }
    ]
  },
}

然后导入您的 JavaScript 文件的图像 :

// Outputs three images with 100, 200, and 300px widths
const responsiveImage = require('myImage.jpg?sizes[]=100,sizes[]=200,sizes[]=300');

// responsiveImage.srcSet => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg 100w,2fefae46cb857bc750fa5e5eed4a0cde-200.jpg 200w,2fefae46cb857bc750fa5e5eed4a0cde-300.jpg 300w'
// responsiveImage.images => [{height: 50, path: '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg', width: 100}, {height: 100, path: '2fefae46cb857bc750fa5e5eed4a0cde-200.jpg', width: 200}, {height: 150, path: '2fefae46cb857bc750fa5e5eed4a0cde-300.jpg', width: 300}]
// responsiveImage.src => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg'
// responsiveImage.toString() => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg'
ReactDOM.render(<img srcSet={responsiveImage.srcSet} src={responsiveImage.src} />, el);

// Or you can just use it as props, `srcSet` and `src` will be set properly
ReactDOM.render(<img {...responsiveImage} />, el);

或在 CSS 中使用( 如果您使用多个图像, 只使用第一个缩放的图像 ) sizes:

.myImage { background: url('myImage.jpg?size=1140'); }

@media (max-width: 480px) {
  .myImage { background: url('myImage.jpg?size=480'); }
}
// Outputs placeholder image as a data URI, and three images with 100, 200, and 300px widths
const responsiveImage = require('myImage.jpg?placeholder=true&sizes[]=100,sizes[]=200,sizes[]=300');

// responsiveImage.placeholder => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAIBAQE…'
ReactDOM.render(
  <div style={{
    height: responsiveImage.height,
    width: responsiveImage.width,
    backgroundSize: 'cover',
    backgroundImage: 'url("' + responsiveImage.placeholder + '")'
  }}>
    <img src={responsiveImage.src} srcSet={responsiveImage.srcSet} />
  </div>, el);

选项

选项类型默认说明
namestring[hash]-[width].[ext]输出文件的文件名模板 .
outputPath弦声职能`undefined
publicPath弦声职能`undefined
contextstringthis.options.context自定义文件上下文, 默认为webpack. config.js 上下文
sizesarray原大小指定您想要使用的所有宽度; 如果指定的大小超过原始图像的宽度, 则使用后者( 即图像不会缩放) 。 您也可以声明默认 sizes 装入器选项中的数组 webpack.config.js。 。 。 .
sizeinteger原大小指定您要使用的一个宽度; 如果指定大小超过原始图像的宽度, 则使用后者( 即图像不会缩放)
mininteger作为手动指定的一种选择 sizes,您可以指定 min, (中文(简体) ), max 财务报告和审定财务报表 steps,大小将为您生成.
maxinteger见 min 上文
stepsinteger4配置在 min 财务报告和审定财务报表 max (包括)
qualityinteger85JPEG 压缩质量
formatstring原始格式无论是 png 或者说 jpg; 用于转换到其他格式
placeholderbooleanfalse指定 Wether 将占位符图像输出为数据 URI 的真值或假值
placeholderSizeinteger40如果启用以上选项, 指定占位符图像宽度的数字值
adapterAdapter联合执行管理计划指定要使用的适配器。 只能在加载器选项中指定 .
disablebooleanfalse禁用此加载器处理图像( 用于开发) . srcSet ,其它属性仍然会生成,但只针对原始大小。 注意 width 财务报告和审定财务报表 height 属性将设置为 100 但图像将保留其原始维度.
emitFilebooleantrue若为 true,将文件发送到文件系统。 若为 false,加载器仍然会与公共的 URI 返回对象,但不会发出文件. 对服务器侧包禁用此选项往往有用 .

适应器特定选项

竖起
  • background: number ——将透明图像转换为不透明图像时,背景填充. 确保这是一个有效的六分位数,例如. 0xFFFFFFFF(中文(简体) )
锐口
  • background: string ——将透明图像转换为不透明图像时,背景填充. 举个例子. #FFFFFF

实例

设定默认 sizes 阵列,所以不需要逐一声明 require。 。 。 .

module.exports = {
  entry: {...},
  output: {...},
  module: {
    rules: [
      {
        test: /\.(jpe?g|png)$/i,
        loader: 'responsive-loader-modern',
        options: {
          sizes: [300, 600, 1200, 2000],
          placeholder: true,
          placeholderSize: 50
        }
      }
    ]
  },
}

写入您的自定义适配器

可能你想使用另一个图像处理库,或者想要改变一个已有的行为. 您可以用下列签名写出自己的适配器:

type Adapter = (imagePath: string) => {
  metadata: () => Promise<{width: number, height: number}>
  resize: (config: {width: number, mime: string, options: Object}) => Promise<{data: Buffer, width: number, height: number}>
}

这个 resize 方法使用一个有 width, (中文(简体) ), mime 财务报告和审定财务报表 options 属性( 接收所有加载器选项)

在您的网络包配置中, 需要您的适配器

{
  test: /\.(jpe?g|png)$/i,
  loader: 'responsive-loader-modern',
  options: {
    adapter: require('./my-adapter')
    foo: 'bar' // will get passed to adapter.resize({width, mime, options: {foo: 'bar}})
  }
}

页:1

  • 不支持你的话 1x, (中文(简体) ), 2x 尺寸.

另见

版本与发布记录

@docusaurus/responsive-loader 共发布 5 个版本,首次发布于 2021/04/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.7.1latest2025/02/081.8 年前
1.7.02021/12/064 年前
1.6.02021/12/064 年前
1.5.02021/12/014 年前
1.4.02021/04/025 年前

关于 @docusaurus/responsive-loader 的常见问题

@docusaurus/responsive-loader 是什么?

用于响应图像的网包装入器.

如何安装 @docusaurus/responsive-loader?

在项目目录运行 npm install @docusaurus/responsive-loader,或使用 pnpm add / yarn add / bun add。

@docusaurus/responsive-loader 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/docusaurus__responsive-loader 是否存在。

@docusaurus/responsive-loader 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@docusaurus/responsive-loader 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@docusaurus/responsive-loader 可以商用吗?

@docusaurus/responsive-loader 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@docusaurus/responsive-loader 的下载量有多少?

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

@docusaurus/responsive-loader 还在维护吗?

最新版本 1.7.1 发布于 2025/02/08(1.8 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。