Agent Skills › videojs/video.js › transform-rolldown-code

transform-rolldown-code

GitHub

提供基于 Rolldown 和 Oxc AST 的代码转换技能,指导通过 transform hook 进行模块级源码编辑、MagicString 使用及源映射处理。

.agents/skills/transform-rolldown-code/SKILL.md videojs/video.js

触发场景

需要对代码进行 AST 级别的修改 配置 Rolldown 构建插件以启用原生 MagicString 处理 TypeScript/JSX 的源码转换

安装

npx skills add videojs/video.js --skill transform-rolldown-code -g -y
更多选项

非标准路径

npx skills add https://github.com/videojs/video.js/tree/main/.agents/skills/transform-rolldown-code -g -y

不安装直接使用

npx skills use videojs/video.js@transform-rolldown-code

指定 Agent (Claude Code)

npx skills add videojs/video.js --skill transform-rolldown-code -a claude-code -g -y

安装 repo 全部 skill

npx skills add videojs/video.js --all -g -y

预览 repo 内 skill

npx skills add videojs/video.js --list

SKILL.md

Frontmatter
{
    "name": "transform-rolldown-code",
    "description": "Transform code. Use for Rolldown."
}

Rolldown code transforms

Use the incoming Oxc AST for analysis and its paired MagicString for source edits.

Workflow

  1. Narrow modules with a native transform hook filter. Use a cheap code.includes(...) guard before accessing meta.ast so parsing stays lazy.
  2. Access meta.ast only when syntax analysis is required. It is a read-only Oxc Program parsed according to meta.moduleType and supports JavaScript, JSX, TypeScript, and TSX syntax.
  3. Use AST start and end offsets with meta.magicString.overwrite, remove, appendLeft, or related edits. Both values describe the same incoming code string.
  4. Return the edited MagicString as { code: magicString }. Edits are ignored when the hook does not return it. Return null when nothing changed.
  5. Keep using offsets from the original hook input. The AST does not update after MagicString edits. Analyze generated syntax in a later transform pass or plugin when necessary.
  6. Keep module-oriented edits in transform. Use renderChunk only when the replacement depends on final generated code, chunk filenames, output format, or the rendered chunk graph; never move ordinary AST transforms there.

Treat meta.ast as read-only. Mutating its nodes does not change output because Rolldown does not print the modified JavaScript AST.

The Oxc AST is syntactic. It includes TS and TSX nodes but has no TypeScript Program, TypeChecker, resolved symbols, inferred types, or cross-file semantics.

For true AST-to-AST transforms, use an appropriate parser, transformer, and code generator, then return generated code with a source map. Every source-changing transform should provide a map unless it intentionally documents a sourceMap: false option; returning native MagicString provides one automatically. Use map: null only when the edit does not move code.

Treat meta.ast and meta.magicString as a required contract in repository-owned Rolldown builds and fail clearly when either is unexpectedly unavailable. Only synthesize or guard the metadata in an intentional compatibility adapter, such as the Vite adapter.

Host setup

Explicitly enable Rolldown's native implementation in every repository-owned input config that runs these transforms:

experimental: {
  nativeMagicString: true,
}

For tsdown, put the same option under inputOptions.experimental. Keep returning the native object directly so Rolldown can generate and compose source maps off the main thread. Enable output sourcemap: true when the build should emit map files. See the native MagicString guide and source transformation guide.

Vite does not forward Rolldown's ast or magicString transform metadata in the pinned version, even when its build enables native MagicString. Continue to adapt these transforms through packages/vjsc/src/vite/oxc.ts for Vite serve and build.

Pattern

import { walk } from 'oxc-walker';

export const plugin = {
  name: 'example-transform',

  transform(code, id, meta) {
    if (!code.includes('$compile')) return null;

    const ast = meta.ast;
    const magicString = meta.magicString;
    if (!ast || !magicString) {
      throw new Error('example-transform requires Rolldown AST metadata and native MagicString.');
    }

    let changed = false;

    walk(ast, {
      enter(node) {
        if (
          node.type === 'CallExpression' &&
          node.callee.type === 'Identifier' &&
          node.callee.name === '$compile'
        ) {
          magicString.overwrite(node.callee.start, node.callee.end, 'compile');
          changed = true;
        }
      },
    });

    return changed ? { code: magicString } : null;
  },
};

Think of the hook as:

incoming code
  -> Oxc AST: find and understand syntax
  -> MagicString: edit source using AST ranges
  -> return edited code

Validation

Run the transform through a real Rolldown build with native MagicString explicitly enabled. Assert changed and unchanged modules, relevant JS/JSX/TS/TSX forms, source maps when consumers need them, and a clear failure when required metadata is unavailable.

Example

Input: "Rename $compile() calls without changing matching strings or comments."

Output: A filtered transform that finds call expressions through meta.ast, overwrites the callee ranges through meta.magicString, returns the edited value, and passes a real Rolldown build test.

版本历史

  • 8a0a3d8 当前 2026-10-20 08:03

同 Skill 集合

.agents/skills/change-spf-behavior/SKILL.md
.agents/skills/commit-pr/SKILL.md
.agents/skills/configure-vite-plus/SKILL.md
.agents/skills/create-html-component/SKILL.md
.agents/skills/create-issue/SKILL.md
.agents/skills/create-react-component/SKILL.md
.agents/skills/create-skill/SKILL.md
.agents/skills/create-spf-behavior/SKILL.md
.agents/skills/design-api/SKILL.md
.agents/skills/document-spf-feature/SKILL.md
.agents/skills/document-spf-use-case/SKILL.md
.agents/skills/implement-accessible-ui/SKILL.md
.agents/skills/implement-spf-feature/SKILL.md
.agents/skills/implement-spf-use-case/SKILL.md
.agents/skills/implement-ui-transition/SKILL.md
.agents/skills/investigate-issue/SKILL.md
.agents/skills/maintain-agent-docs/SKILL.md
.agents/skills/maintain-tests/SKILL.md
.agents/skills/maintain-vjsc-skin-gaps/SKILL.md
.agents/skills/review-accessibility/SKILL.md
.agents/skills/review-api/SKILL.md
.agents/skills/review-branch/SKILL.md
.agents/skills/review-docs/SKILL.md
.agents/skills/review-html-component-design/SKILL.md
.agents/skills/review-html-component/SKILL.md
.agents/skills/review-react-component-design/SKILL.md
.agents/skills/review-react-component/SKILL.md
.agents/skills/write-api-reference/SKILL.md
.agents/skills/write-design-doc/SKILL.md
.agents/skills/write-docs/SKILL.md
.agents/skills/write-html-component-design/SKILL.md
.agents/skills/write-locale-translations/SKILL.md
.agents/skills/write-react-component-design/SKILL.md
.agents/skills/write-rfc/SKILL.md
.agents/skills/create-rolldown-plugin/SKILL.md
.agents/skills/create-vite-plugin/SKILL.md
.agents/skills/create-vjsc-component/SKILL.md

元信息

文件数
0
版本
8a0a3d8
Hash
d65612dc
收录时间
2026-10-20 08:03

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-11-03 10:11
浙ICP备14020137号-1