Skip to content

i18n(zh-cn): Update imports.mdx #11542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/content/docs/zh-cn/guides/imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,21 @@ Astro 支持使用浏览器的 [`WebAssembly`](https://developer.mozilla.org/en-

## Node 内置模块

我们鼓励 Astro 用户尽可能避免使用 Node.js 内置模块(`fs`、`path` 等)。Astro 兼容多个运行时使用 [适配器](/zh-cn/guides/on-demand-rendering/)。这包括 [Deno](https://github.com/denoland/deno-astro-adapter) 和 [Cloudflare Workers](/zh-cn/guides/integrations-guide/cloudflare/),它们不支持 Node 内置模块,例如 `fs`
Astro 支持通过 Node.js 较新的 `node:` 前缀来调用其内置模块,但仍存在部分限制。比如可能在开发和生产环境中可能存在差异,且部分功能可能与按需渲染不兼容。部分 [适配器](/zh-cn/guides/on-demand-rendering/) 可能也与内置模块不兼容,或需要额外配置才能支持(例如 [Cloudflare Workers](/zh-cn/guides/integrations-guide/cloudflare/) 或 [Deno](https://github.com/denoland/deno-astro-adapter))

我们致力于为常用的 Node.js 内置模块提供 Astro 化的替代品,不过现在还没有实现。因此,如果你**真的**需要,我们不会阻止你使用这些内置模块。Astro 支持使用较新 `node:` 前缀来支持 Node.js 内置模块。例如,如果你想读取一个文件,你可以这样做
下面的示例展示了从 Node 中导入 `util` 模块,用以解析媒体类型(MIME)

```astro title="src/components/MyComponent.astro"
---
// 示例:从 Node.js 中导入内置模块 "fs/promises"
import fs from 'node:fs/promises';

const url = new URL('../../package.json', import.meta.url);
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
// 示例:从 Node.js 中导入内置模块 "util"
import util from 'node:util';
export interface Props {
mimeType: string,
}
const mime = new util.MIMEType(Astro.props.mimeType)
---

<span>Version: {data.version}</span>
<span>Type: {mime.type}</span>
<span>SubType: {mime.subtype}</span>
```

## 扩展文件类型支持
Expand Down