File tree Expand file tree Collapse file tree 1 file changed +10
-10
lines changed
src/content/docs/zh-cn/guides Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change @@ -358,21 +358,21 @@ Astro 支持使用浏览器的 [`WebAssembly`](https://developer.mozilla.org/en-
358
358
359
359
## Node 内置模块
360
360
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 ) ) 。
362
362
363
- 我们致力于为常用的 Node.js 内置模块提供 Astro 化的替代品,不过现在还没有实现。因此,如果你 ** 真的 ** 需要,我们不会阻止你使用这些内置模块。Astro 支持使用较新 ` node: ` 前缀来支持 Node.js 内置模块。例如,如果你想读取一个文件,你可以这样做 :
363
+ 下面的示例展示了从 Node 中导入 ` util ` 模块,用以解析媒体类型(MIME) :
364
364
365
365
``` astro title="src/components/MyComponent.astro"
366
366
---
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)
373
373
---
374
-
375
- <span>Version : {data.version }</span>
374
+ <span>Type: {mime.type}</span>
375
+ <span>SubType : {mime.subtype }</span>
376
376
```
377
377
378
378
## 扩展文件类型支持
You can’t perform that action at this time.
0 commit comments