结论先说
- 开发 Figma 插件前必须先让 Codex 理解「双线程沙箱」隔离。主线程(
code.ts)只能操作画布节点和变量,不能做文件下载;弹窗 UI(ui.html)负责展示走查报告和触发 Blob 下载,二者完全靠postMessage交换 JSON。 - **清单必须配
"documentAccess": "dynamic-page",并全部使用Async异步接口*。旧版教程里的同步读取方法在新版动态分页机制下会报错;读取本地变量要用figma.variables.getLocalVariablesAsync(),切勿照抄过时代码。 - 扫描字体和色值时,必须处理
figma.mixed混合值。一个文本框里只要混用了两种字号或颜色,node.fontName、node.textStyleId就会返回figma.mixed符号,不先做判断会直接抛异常。 - 纯走查与本地导出类插件应把
networkAccess.allowedDomains设为["none"]。零外网请求既能消除团队对设计稿泄密的顾虑,也能让插件在内部安全评审与社区上架时更顺畅。 - 用 Codex 迭代插件 TypeScript 逻辑、生成 W3C Design Tokens 转换器并补齐单测需要稳定额度。国内没有外币卡可在本站开通 ChatGPT Plus 自助充值 或配额更高的 ChatGPT Pro 100(10x) 通道,团队多人研发可直接了解 ChatGPT Business 团队定制。

图:本站根据 Figma Plugin API 官方文档 整理的示意图
本文根据 Figma Plugin API 官方文档(概览、Manifest、Dynamic Loading、Creating UI)与 OpenAI Codex 文档整理,资料核对于 2026-11-21。配图为本站整理的示意图,不是界面截图。本站(bigolab.com)不是 Figma 或 OpenAI 官方网站。
适用于谁
- 需要给设计团队或前端团队做自动化走查工具,批量找出设计稿中「未绑定色板/未绑变量」和「未命中字号规范」图层的研发与设计工程人员;
- 想把 Figma 里的 Local Variables 和 Styles 一键导出成标准 Design Tokens(JSON / CSS Variables)供前端代码库消费的工程师;
- 用 Codex 写 Figma 插件时,经常遇到同步 API 报错、UI 无法下载文件或
figma.mixed类型崩溃的开发者。
一、Figma Plugin 与 Figma MCP 如何分工配合
如果你只是想把某个已经整理好的页面快速还原成前端组件,可以先看 /guides/codex-figma-mcp-design-tokens-components-to-frontend-code 与 /guides/figma-make-dev-mode-mcp-frontend-codex-workflow。但只要团队面临「几十张画板里混着野生颜色」「需要一键定位高亮违规图层」「定期导出标准化 Token 文件提交给前端仓库」这类高频动作,在 Figma 里装一个自研 Figma Plugin 效率高得多,也能在设计交付前把噪音先清干净。
二、五步用 Codex 开发规范走查与 Tokens 导出插件

图:本站根据 Figma 官方 Manifest 与动态分页文档 整理的示意图
第一步:准备最小权限 manifest.json 与 AGENTS.md
在 Figma 桌面端通过 Plugins -> Development -> New plugin 创建带 UI 的模板,先核对 manifest.json 的核心字段:
json
{
"name": "Design Audit & Tokens Exporter",
"id": "your-local-plugin-id",
"api": "1.0.0",
"main": "dist/code.js",
"ui": "dist/ui.html",
"editorType": ["figma"],
"documentAccess": "dynamic-page",
"networkAccess": {
"allowedDomains": ["none"]
}
}
接着在仓库根目录建立 AGENTS.md(规则读取机制见 /guides/codex-agents-md-how-it-reads),明确约束 Codex:禁止使用已废弃的同步变量/样式接口、遇到 figma.mixed 必须单独归类、严禁把 window/document 写进 code.ts。
第二步:让 Codex 编写字体与色值规范批量检查逻辑
默认先扫描当前选区(figma.currentPage.selection),未选中时回退扫描当前页(figma.currentPage),避免超大文件整库遍历造成卡顿:
ts
// src/code.ts(主线程沙箱片段)
interface AuditIssue {
nodeId: string;
nodeName: string;
reason: string;
}
function auditCurrentScope(): AuditIssue[] {
const roots = figma.currentPage.selection.length > 0
? figma.currentPage.selection
: [figma.currentPage];
const issues: AuditIssue[] = [];
const visit = (node: BaseNode) => {
if (node.type === 'TEXT') {
const boundText = node.boundVariables?.fontSize;
if (node.textStyleId === figma.mixed) {
issues.push({ nodeId: node.id, nodeName: node.name, reason: '文本框混用多种样式,请拆分为规范样式' });
} else if (!node.textStyleId && !boundText) {
issues.push({ nodeId: node.id, nodeName: node.name, reason: '未绑定文字样式(Text Style)或字号变量' });
}
}
if ('fills' in node && Array.isArray(node.fills) && node.fills.length > 0) {
const hasBoundColor = Boolean(node.boundVariables?.fills?.length);
const hasPaintStyle = 'fillStyleId' in node && node.fillStyleId !== '' && node.fillStyleId !== figma.mixed;
if (!hasBoundColor && !hasPaintStyle) {
issues.push({ nodeId: node.id, nodeName: node.name, reason: '填充色未绑定颜色变量或 Paint Style' });
}
}
if ('children' in node) {
for (const child of node.children) visit(child);
}
};
roots.forEach(visit);
return issues;
}
还可以让 Codex 补一个定位消息处理:当设计师在 UI 列表里点击某条违规记录时,主线程收到 nodeId 后调用 await figma.getNodeByIdAsync(nodeId) 并执行 figma.viewport.scrollAndZoomIntoView([target]),一秒跳到问题图层。
第三步:异步提取 Variables 并转换成 Design Tokens
通过异步 API 读取当前文件的 Local Variables 与 Collections,把 RGBA 通道转为标准十六进制或 CSS 变量结构,打包发送给 UI 层下载:
ts
function rgbaToHex(c: RGBA): string {
const toHex = (v: number) => Math.round(v * 255).toString(16).padStart(2, '0');
const alpha = c.a < 1 ? toHex(c.a) : '';
return `#${toHex(c.r)}${toHex(c.g)}${toHex(c.b)}${alpha}`;
}
async function exportTokensPayload() {
const [collections, variables] = await Promise.all([
figma.variables.getLocalVariableCollectionsAsync(),
figma.variables.getLocalVariablesAsync()
]);
const tokens: Record<string, { $type: string; $value: string | number | boolean }> = {};
for (const v of variables) {
const col = collections.find((c) => c.id === v.variableCollectionId);
const modeId = col?.defaultModeId ?? Object.keys(v.valuesByMode)[0];
const rawVal = v.valuesByMode[modeId];
if (v.resolvedType === 'COLOR' && rawVal && typeof rawVal === 'object' && 'r' in rawVal) {
tokens[v.name] = { $type: 'color', $value: rgbaToHex(rawVal as RGBA) };
} else if (v.resolvedType === 'FLOAT' && typeof rawVal === 'number') {
tokens[v.name] = { $type: 'dimension', $value: `${rawVal}px` };
}
}
return tokens;
}
第四步:在 ui.html 接收消息并触发一键导出
在 UI iframe 中监听 window.onmessage,收到 event.data.pluginMessage 后把 JSON 或生成的 :root { --token: value; } 文本封装成 Blob,触发本地 tokens.json / tokens.css 下载,整个过程完全离线完成。如果你想把设计到代码的整条工具链打通,也可结合 /guides/indie-designer-ai-toolchain-chatgpt-figma-mcp-codex-prototype 与 /guides/figma-make-ai-rename-layers-mcp-guide 配合使用。
订阅与开通提示:让 Codex 扩展支持多主题 Mode(Light / Dark)变量别名解析、生成 Tailwind v4@theme映射并编写完整边界测试时,对话轮次和上下文占用会快速增长。若基础档配额不够,可在本站查看 ChatGPT Pro 100(10x) iOS 可覆盖已有 Plus 档 或适合空白号/到期号的 ChatGPT Pro 5x 信用卡自助通道;长期重度设计工程协同可直接选择 ChatGPT Pro 20x iOS 通道。
第五步:在 Figma 桌面端加载调试并排查性能
运行 npm run build(推荐配合 esbuild --watch 自动重编),在 Figma 桌面端通过 Plugins -> Development -> Import plugin from manifest... 导入项目清单。打开 Plugins -> Development -> Show/Hide Console 即可实时看报错,修改代码保存后重新运行插件即可验证。
常见问题
问:为什么插件一运行就报「Cannot call getLocalVariables with documentAccess: dynamic-page」?
答:这是 Codex 沿用了旧版同步接口导致的。在 dynamic-page 模式下,所有涉及全局样式、变量与跨页节点查找的方法都必须换成带 Async 后缀的版本(如 getLocalVariablesAsync、getLocalTextStylesAsync、getNodeByIdAsync)并加上 await。
问:遇到一个变量引用了另一个变量(Variable Alias)时,为什么导出代码拿不到十六进制色值?
答:当 rawVal 是别名对象(type === 'VARIABLE_ALIAS')时,里面只存了目标变量的 id,不包含 r/g/b 通道。你可以让 Codex 写一个带循环检测的递归解析函数:根据别名 id 在 variables 表中查到引用名,输出标准 W3C 引用语法 {color.brand.500},或者展开解析出最终颜色值。
问:插件需要把走查结果发给外部接口或把未发布设计稿发给模型审查,有什么安全注意事项?
答:未公开产品设计稿、含真实客户姓名或财务数据的原型图属于敏感资产,切勿在未经脱敏与团队授权的情况下自动上传外部接口。保持 allowedDomains: ["none"] 纯本地离线执行是最稳妥的默认方案。
问:国内没有外币卡,或者设计研发团队采购 Codex 对应订阅需要统一报销开具增值税发票,怎么开通?
答:可访问本站 AI 会员充值总览 按用量挑选 ChatGPT Plus 信用卡自助通道、ChatGPT Plus iOS 自助充值 或高频重度用户适用的 ChatGPT Pro 20x 信用卡通道;若注册账号卡在手机验证环节,也可了解 Codex 注册手机验证服务。本站支持支付宝、微信支付和 AlipayHK,自助卡密兑换无需交出账号密码,企业团队定制采购支持开具增值税发票;售价以页面实时标价为准,售后遵循掉订阅按剩余天数折退、封号不在质保范围的标准说明。
相关教程
- Codex 接入 Figma MCP 把设计稿组件、间距与色值还原成前端代码实战配置
- Figma Make 与 Figma Dev Mode MCP 联动实战:把高保真设计稿直接交付前端与 Codex
- 独立设计师的 AI 辅助工具链:ChatGPT 创意发散、Figma MCP 到 Codex 还原交互原型
- Figma Make / Figma AI 怎么用:设计稿一键变交互原型、自动整理图层命名与配合 Figma MCP 出代码
- Codex 怎么读 AGENTS.md:全局和项目的读取顺序、override 文件、32 KiB 上限,以及没生效怎么排查
参考资料
- Figma 官方开发者文档:Plugin API Overview — https://developers.figma.com/docs/plugins/
- Figma 官方开发者文档:Plugin Manifest — https://developers.figma.com/docs/plugins/manifest/
- Figma 官方开发者文档:Migrating to Dynamic Loading — https://developers.figma.com/docs/plugins/migrating-to-dynamic-loading/
- Figma 官方开发者文档:Creating a User Interface — https://developers.figma.com/docs/plugins/creating-ui/
0 条评论
还没有评论,来抢沙发~