Creation:2026-06-25Last update:2026-06-25
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "集成到 intlayer() 中;初始化文档"v9.0.02026/6/25
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
intlayerCompiler
intlayerCompiler 是一个 Vite 插件,它扫描组件源文件以查找内联 Intlayer 内容声明(即直接在组件内部定义的内容,而不是在单独的 .content.ts 文件中),并在转换(transform)阶段将它们写入字典 JSON 文件中。
自 Intlayer v9 起,当您的 Intlayer 配置中同时设置了compiler.enabled为true且设置了compiler.output时,intlayerCompiler会自动包含在主intlayer()插件中。只有当您希望完全控制编译器特定配置时,才需要单独注册它。
用法
作为 intlayer() 的一部分(推荐,v9+)
通过您的 Intlayer 配置启用编译器:
ts
复制代码
复制代码到剪贴板
// intlayer.config.tsimport { defineConfig } from "intlayer";export default defineConfig({ compiler: { enabled: true, output: "./src/dictionaries", // 提取的字典写入的位置 },});然后使用标准插件,无需额外交册:
ts
复制代码
复制代码到剪贴板
// vite.config.tsimport { defineConfig } from "vite";import { intlayer } from "vite-intlayer";export default defineConfig({ plugins: [intlayer()],});独立使用(需要时)
ts
复制代码
复制代码到剪贴板
// vite.config.tsimport { defineConfig } from "vite";import { intlayerCompiler } from "vite-intlayer";export default defineConfig({ plugins: [intlayerCompiler()],});选项
ts
复制代码
复制代码到剪贴板
import type { IntlayerCompilerOptions } from "vite-intlayer";显示表格的所有内容
在弹窗中打开表格以清晰地查看所有数据
| 选项 | 类型 | 描述 |
|---|---|---|
configOptions | GetConfigurationOptions | 转发给 getConfiguration() 的 Intlayer 配置覆盖项。 |
compilerConfig | Partial<CompilerConfig> | 编译器特定配置部分的覆盖项(例如 enabled, output, filesList)。 |
示例
ts
复制代码
复制代码到剪贴板
intlayerCompiler({ configOptions: { configFile: "./config/intlayer.config.ts" }, compilerConfig: { enabled: true, output: "./src/dictionaries" },});工作原理
转换(Transform)阶段
对于每个与 compiler.filesList 匹配的源文件,编译器插件执行以下操作:
- 将文件内容传递给
@intlayer/babel中的extractContent。 - 对找到的每个声明调用
onExtract,这会将生成的字典 JSON 写入compiler.output。 - 返回转换后的源代码,内联声明已替换为标准的
useIntlayer('key')/getIntlayer('key')调用。
支持的文件类型:.ts, .tsx, .js, .jsx, .vue, .svelte, .astro。
HMR(热模块替换)
在开发模式下保存组件文件时,编译器:
- 通过 Vite 的
handleHotUpdate钩子检测文件更改。 - 从更新后的文件中重新提取内容。
- 写入更新后的字典 JSON。
- 触发页面完全重新加载(
server.ws.send({ type: 'full-reload' }))。
500 毫秒的防抖(debounce)可防止字典写入本身(这也会触发文件更改事件)导致无限的重新提取循环。
去重(Deduplication)
intlayerCompiler 使用与其他捆绑插件相同的 createPrimaryInstanceGuard 去重机制。当同时存在 intlayer()(已捆绑编译器)和手动 intlayerCompiler() 调用时,仅运行第一个注册 ins — 不会重复写入任何字典。