Vite/Webpack plugin to add source location attributes to JSX elements
为 React 组件自动添加调试标记的 Vite 插件。
- 🏷️ 自动为 JSX 元素添加调试标记属性
- 🎯 精确定位组件源码位置(文件路径、行号、列号)
- 🚀 零配置开箱即用
- 🎨 支持 TypeScript 和 JSX
- 🔧 可配置的标记选项
- 🏗️ 生产环境自动禁用
- 📊 构建统计信息输出
- 🎭 智能排除 Fragment 和 Three.js 组件
``bash`
npm install vite-plugin-devv-tagger --save-dev或
yarn add vite-plugin-devv-tagger -D或
pnpm add vite-plugin-devv-tagger -D
在 vite.config.ts 中配置:
`typescript
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import componentTagger from 'vite-plugin-devv-tagger';
export default defineConfig({
plugins: [
componentTagger(), // 添加在 react 插件之前
react(),
],
});
`
`typescript
import componentTagger from 'vite-plugin-devv-tagger';
export default defineConfig({
plugins: [
componentTagger({
// 是否启用插件
enabled: true,
// 是否在生产环境中启用
enableInProduction: false,
// 要排除的文件路径模式
exclude: ['node_modules', 'tests'],
// 要处理的文件扩展名
extensions: ['.jsx', '.tsx'],
// 数据属性前缀
prefix: 'data-devv',
// 是否添加传统属性(兼容旧版本)
includeLegacyAttributes: true,
}),
react(),
],
});
`
`jsx`
function App() {
return (
Hello World
);
}
`jsx`
function App() {
return (
Hello World
);
}
通过环境变量控制插件行为:
`bash开发环境(添加标记)
npm run dev
配置选项
| 选项 | 类型 | 默认值 | 描述 |
|------|------|--------|------|
|
enabled | boolean | true | 是否启用插件 |
| enableInProduction | boolean | false | 是否在生产环境中启用 |
| exclude | string[] | ['node_modules'] | 要排除的文件路径模式 |
| extensions | string[] | ['.jsx', '.tsx'] | 要处理的文件扩展名 |
| prefix | string | 'data-devv' | 数据属性前缀 |
| includeLegacyAttributes | boolean | true | 是否添加传统属性 |排除的元素
插件会自动排除以下元素:
- React Fragment
- Three.js/React Three Fiber 组件
- @react-three/drei 导入的组件
构建统计
构建完成后会输出统计信息:
`
[devv-tagger] 构建统计:
- 总文件数: 45
- 处理文件数: 32
- 标记元素数: 256
`开发提示
1. 插件在
enforce: "pre"` 模式下运行,确保在其他插件之前处理代码MIT