Skip to content

Commit 42b7818

Browse files
i18n(zh-cn): Update imports.mdx (#11542)
Co-authored-by: Yan <[email protected]>
1 parent 50b2e63 commit 42b7818

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/content/docs/zh-cn/guides/imports.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,21 @@ Astro 支持使用浏览器的 [`WebAssembly`](https://developer.mozilla.org/en-
358358

359359
## Node 内置模块
360360

361-
我们鼓励 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`
361+
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)
362362

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

365365
```astro title="src/components/MyComponent.astro"
366366
---
367-
// 示例:从 Node.js 中导入内置模块 "fs/promises"
368-
import fs from 'node:fs/promises';
369-
370-
const url = new URL('../../package.json', import.meta.url);
371-
const json = await fs.readFile(url, 'utf-8');
372-
const data = JSON.parse(json);
367+
// 示例:从 Node.js 中导入内置模块 "util"
368+
import util from 'node:util';
369+
export interface Props {
370+
mimeType: string,
371+
}
372+
const mime = new util.MIMEType(Astro.props.mimeType)
373373
---
374-
375-
<span>Version: {data.version}</span>
374+
<span>Type: {mime.type}</span>
375+
<span>SubType: {mime.subtype}</span>
376376
```
377377

378378
## 扩展文件类型支持

0 commit comments

Comments
 (0)