# @astropub/vite-plugin-file 中文文档

规范地址：https://npmcn.com/package/%40astropub/vite-plugin-file/

版本：0\.2\.1
npm 元数据更新时间：2024-12-12T13:07:01.640Z
中文译文更新时间：2026-11-07T01:24:50.411Z
软件包许可：MIT-0

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

原始 npm 页面：https://www.npmjs.com/package/%40astropub/vite-plugin-file
原文对照：https://npmcn.com/package/%40astropub/vite-plugin-file/?lang=original

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

- 包体积（解压后）：10.2 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2024-12-03T16:15:43.825Z
- 最新版本发布：2024-12-12T13:07:01.421Z
- 近 30 天下载量：2831（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

一个用于无缝访问文件元数据和数据流的Vite插件,支持Blob、File和ArrayBuffer等现代Web API接口

## 安装

```sh
npm install @astropub/vite-plugin-file
```

也可以使用：`pnpm add @astropub/vite-plugin-file`、`yarn add @astropub/vite-plugin-file`、`bun add @astropub/vite-plugin-file`

## README

# 维特- Plugin- 文件 ![](https://jonneal.dev/vite-logo.svg)

**虚拟插件文件** 可以在 **[维特](https://vite.dev/)** 项目.

[![NPM Version](https://img.shields.io/npm/v/@astropub/vite-plugin-file?color=%23444&label=&labelColor=%23CB0000&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjE1MCAxNTAgNDAwIDQwMCIgZmlsbD0iI0ZGRiI+PHBhdGggZD0iTTE1MCA1NTBoMjAwVjI1MGgxMDB2MzAwaDEwMFYxNTBIMTUweiIvPjwvc3ZnPg==&style=for-the-badge)](https://www.npmjs.com/package/@astropub/vite-plugin-file) [![NPM Downloads](https://img.shields.io/badge/dynamic/json?url=https://api.npmjs.org/downloads/point/last-week/@astropub/vite-plugin-file&query=downloads&label=%E2%87%93+week&color=%23444&labelColor=%23EEd100&style=for-the-badge)](https://www.npmjs.com/package/@astropub/vite-plugin-file)

-   **高效文件处理**: 流出大文件而不消耗多余的内存.
-   **方便文件格式**:与现代Web API无缝地工作.
    -   高效处理大型文件 `ReadableStream`。 。 。 .
    -   访问整个文件为 `ArrayBuffer`, (中文(简体) ), `Blob`,或 `File`。 。 。 .
-   **简单元数据访问**:以微小的努力获取关键文件信息.
    -   获取文件名称、类型、大小和上次修改的时间.
    -   访问元数据作为HTTP风格头和预配置回复.
-   **虚拟集成**:专门为Vite所建,确保无缝融合.
    -   包括用于 `?file` 进口.

## 安装

安装插件 :

```shell
npm install @astropub/vite-plugin-file
```

将插件添加到您的 Vite 配置文件 :

```js
// vite.config.js
import vitePluginFile from "@astropub/vite-plugin-file"

export default {
  plugins: [vitePluginFile()]
}
```

## 使用量

添加数据 `?file` 用于访问其元数据和内容的文件导入的后缀.

```js
import FaviconIco from "./assets/favicon.ico?file"
```

#### 文件元数据

获取任何文件的基本元数据 .

```js
import FaviconIco from "./assets/favicon.ico?file"

console.log(FaviconIco.name)         // "favicon.ico"
console.log(FaviconIco.type)         // "image/x-icon"
console.log(FaviconIco.size)         // 9608
console.log(FaviconIco.lastModified) // 1735689600000
```

#### 文件流

在不装入内存的情况下, 高效地流出任何文件 .

```js
import FaviconIco from "./assets/favicon.ico?file"

const stream = FaviconIco.stream() // ReadableStream

const response = FaviconIco.response() // Response (with pre-configured headers)
```

#### 完整文件访问

访问文件为 `ArrayBuffer`, (中文(简体) ), `Blob`,或 `File`。 。 。 .

```js
import FaviconIco from "./assets/favicon.ico?file"

const buffer = await FaviconIco.arrayBuffer()
console.log(buffer.byteLength) // 9608

const blob = await FaviconIco.blob()
console.log(blob.type)         // "image/x-icon"

const file = await FaviconIco.file()
console.log(file.name)         // "favicon.ico"
console.log(file.lastModified) // 1735689600000
```

#### 文件元数据为信头

取回文件元数据为 HTTP 风格头条 .

```js
import FaviconIco from "./assets/favicon.ico?file"

const headers = FaviconIco.headers()
console.log(headers.get("content-type"))   // "image/x-icon"
console.log(headers.get("content-length")) // "9608"
console.log(headers.get("last-modified"))  // "Wed, 01 Jan 2025 00:00:00 GMT"
```

## 许可证

凭 [MIT-0 许可证](https://opensource.org/license/mit-0)。 。 。 .

  

慢慢来!

## 常见问题

### @astropub/vite-plugin-file 是什么？

一个用于无缝访问文件元数据和数据流的Vite插件,支持Blob、File和ArrayBuffer等现代Web API接口

### 如何安装 @astropub/vite-plugin-file？

在项目目录运行 npm install @astropub/vite-plugin-file，或使用 pnpm add / yarn add / bun add。

### @astropub/vite-plugin-file 支持 TypeScript 吗？

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

### @astropub/vite-plugin-file 支持 ESM 还是 CommonJS？

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

### @astropub/vite-plugin-file 可以商用吗？

@astropub/vite-plugin-file 使用 MIT-0 许可。声明的许可为 MIT-0，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @astropub/vite-plugin-file 的下载量有多少？

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

### @astropub/vite-plugin-file 还在维护吗？

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

