# @discourse/jxl 中文文档

规范地址：https://npmcn.com/package/%40discourse/jxl/

版本：1\.3\.0
npm 元数据更新时间：2026-05-07T19:27:27.953Z
中文译文更新时间：2026-11-12T11:12:32.251Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40discourse/jxl
原文对照：https://npmcn.com/package/%40discourse/jxl/?lang=original
项目主页：https://github.com/discourse/jSquash#readme
源码仓库：https://github.com/discourse/jSquash

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：5.8 MB，30 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2026-05-07T19:27:27.219Z
- 最新版本发布：2026-05-07T19:27:27.646Z
- 近 30 天下载量：87503（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 1 个）

## 中文简介

Wasm JPEG XL 编码器和解码器支持浏览器. 从Squoosh App重新包装.

## 安装

```sh
npm install @discourse/jxl
```

也可以使用：`pnpm add @discourse/jxl`、`yarn add @discourse/jxl`、`bun add @discourse/jxl`

## README

# 时间轴:

[![npm version](https://badge.fury.io/js/@jsquash%2Fjxl.svg)](https://badge.fury.io/js/@jsquash%2Fjxl)

在浏览器中对JPEG XL图像进行编码和解码的简单体验. 由WebAssembly提供电力.

QQ 只有一个稳定的浏览器支持显示 JPEG XL [到目前为止](https://caniuse.com/jpegxl). 该包件用于试验和试验.

使用 [缩写](https://github.com/libjxl/libjxl) 图书馆.

A 类 [积分](https://github.com/jamsinclair/jSquash) 软件包。 源自 [斯库什语Name](https://github.com/GoogleChromeLabs/squoosh) 应用.

## 安装

```shell
npm install --save @jsquash/jxl
# Or your favourite package manager alternative
```

## 使用量

注意 : 您需要手动将代码目录中的被点文件包含在内, 或者使用 WebPack 或 Rollup 这样的捆绑器将它们包含在您的应用程序/ 服务器中 .

### 解码( 数据: ArrayBuffer): 承诺

将 JPEG XL 二进制阵列缓冲解码为原始 RGB 图像数据 .

#### 数据

类型 : `ArrayBuffer`

#### 示例

```js
import { decode } from '@jsquash/jxl';

const formEl = document.querySelector('form');
const formData = new FormData(formEl);
// Assuming user selected an input JPEG XL file
const imageData = await decode(await formData.get('image').arrayBuffer());
```

### 编码( 数据: 图像数据, 选项? : 编码选项): 承诺

将原始 RGB 图像数据编码为 JPEG 格式并解析为二进制数据的 ArrayBuffer .

#### 数据

类型 : `ImageData`

#### 选项

类型 : `Partial<EncodeOptions>`

JPEG XL 输出图像的编码器选项. [参见默认值](https://npmcn.com/package/%40discourse/jxl/meta.ts)。 。 。 .

#### 示例

```js
import { encode } from '@jsquash/jxl';

async function loadImage(src) {
  const img = document.createElement('img');
  img.src = src;
  await new Promise(resolve => img.onload = resolve);
  const canvas = document.createElement('canvas');
  [canvas.width, canvas.height] = [img.width, img.height];
  const ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
  return ctx.getImageData(0, 0, img.width, img.height);
}

const rawImageData = await loadImage('/example.png');
const jpegBuffer = await encode(rawImageData);
```

#### 无损失示例

```js
import { encode } from '@jsquash/jxl';

const rawImageData = await loadImage('/example.png');
// Lossless encoding can be achieved by setting the `lossless` option to `true`
const jxlBuffer = await encode(rawImageData, { lossless: true });
```

## 激活多线索

默认情况下,编码功能将使用单一线程来编码图像. 如果您想要加快速度, 您可以启用以下的多条读取 .

1.  移动您的电话 `encode` 改为 a [网络工人](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)。 。 。 .
2.  配置您的网络服务器以使用以下信头( 这是 [a 安保要求](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements)(中文(简体) )
    -   `Cross-Origin-Opener-Policy: same-origin`
    -   `Cross-Origin-Embedder-Policy: require-corp`

这仍然只在支持多行读的浏览器和设备中生效. 如果浏览器不支持它,它会倒回单线程模式

## 手工 WASM 初始化( 不推荐)

在大多数情况下,不需要手动将所提供的WebAssembly模块关闭. 生成的胶体代码会照顾到这一点,并支持大多数的网络捆绑器.

出现这种情况的一个情况是,在Cloudflare工人中使用模块([更多信息请参见 README](https://npmcn.com/README.md#usage-in-cloudflare-workers)) (中文(简体) ).

这个 `encode` 财务报告和审定财务报表 `decode` 模块两个导出 an `init` 函数,用于手动加载原模块.

```js
import decode, { init as initJXLDecode } from '@jsquash/jxl/decode';

initJXLDecode(WASM_MODULE); // The `WASM_MODULE` variable will need to be sourced by yourself and passed as an ArrayBuffer.
const image = await fetch('./image.jpeg').then(res => res.arrayBuffer()).then(decode);
```

您也可以通过自定义选项到 `init` 函数可以自定义模块的行为。 见 [标记文件](https://emscripten.org/docs/api_reference/module.html#Module) 更多信息.

```js
import decode, { init as initJXLDecode } from '@jsquash/jxl/decode';

initJXLDecode(null, {
  // Customise the path to load the wasm file
  locateFile: (path, prefix) => `https://example.com/${prefix}/${path}`,
});
const image = await fetch('./image.jxl').then(res => res.arrayBuffer()).then(decode);
```

## 已知问题

见 [jSquash 项目编辑器](https://github.com/jamsinclair/jSquash#known-issues)

## 常见问题

### @discourse/jxl 是什么？

Wasm JPEG XL 编码器和解码器支持浏览器\. 从Squoosh App重新包装\.

### 如何安装 @discourse/jxl？

在项目目录运行 npm install @discourse/jxl，或使用 pnpm add / yarn add / bun add。

### @discourse/jxl 支持 TypeScript 吗？

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

### @discourse/jxl 支持 ESM 还是 CommonJS？

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

### @discourse/jxl 可以商用吗？

@discourse/jxl 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @discourse/jxl 的下载量有多少？

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

### @discourse/jxl 还在维护吗？

最新版本 1\.3\.0 发布于 2026/05/08（6 个月前）；至今共发布 1 个版本，其中过去 12 个月发布了 1 个；首次发布于 2026/05/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

