npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-source-info

v1.0.0

Published

A Vite plugin that adds source location and component information to DOM elements during development

Readme

Vite Plugin Source Identifier

一个 Vite 插件,能够在开发时为 DOM 元素自动添加组件源码位置和属性信息,方便前端调试和开发者工具集成。

特性

  • 🎯 精确定位: 为每个 DOM 元素添加准确的源文件路径、行号和列号
  • 高性能: 仅在开发环境运行,对生产构建无任何影响
  • 🔧 灵活配置: 支持自定义属性前缀、包含/排除文件等
  • 📦 框架支持: 同时支持 React (JSX/TSX) 和 Vue
  • 🛠️ 开发友好: 完整的 TypeScript 支持和类型提示

安装

npm install vite-plugin-source-identifier --save-dev
# 或
yarn add vite-plugin-source-identifier --dev
# 或
pnpm add vite-plugin-source-identifier --save-dev

使用方法

基础配置

在你的 vite.config.ts 中添加插件:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { sourceIdentifierPlugin } from 'vite-plugin-source-identifier'

export default defineConfig({
  plugins: [
    sourceIdentifierPlugin(), // 添加在框架插件之前或之后均可
    react(),
  ],
})

高级配置

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { sourceIdentifierPlugin } from 'vite-plugin-source-identifier'

export default defineConfig({
  plugins: [
    sourceIdentifierPlugin({
      // 是否启用插件 (默认:开发环境 true,生产环境 false)
      enabled: true,
      
      // 自定义属性前缀 (默认: "data-mx")
      attributePrefix: 'data-mx',
      
      // 是否包含组件属性信息 (默认: true)
      includeProps: true,
      
      // 要处理的文件扩展名 (默认: ['.jsx', '.tsx', '.vue'])
      include: ['.jsx', '.tsx', '.vue'],
      
      // 要排除的文件模式
      exclude: ['node_modules', 'dist']
    }),
    react(),
  ],
})

生成的属性

插件会为每个 DOM 元素添加以下数据属性:

<button 
  data-mx-id="src/components/Button.tsx:23:4"
  data-mx-name="button"
  data-component-path="src/components/Button.tsx"
  data-component-line="23"
  data-component-file="Button.tsx"
  data-component-name="button"
  data-component-content="%7B%22text%22%3A%22Join%20Community%22%2C%22className%22%3A%22shadow-glow%22%7D"
  class="btn-primary"
>
  Join Community
</button>

属性说明

  • data-mx-id: 完整的源码位置标识 (文件路径:行号:列号)
  • data-mx-name: 元素或组件名称
  • data-component-path: 相对文件路径
  • data-component-line: 行号
  • data-component-file: 文件名
  • data-component-name: 组件/元素名称
  • data-component-content: URL 编码的组件属性 JSON (可选)

React 示例

// src/components/Button.tsx
import React from 'react'

interface ButtonProps {
  text: string
  className?: string
  onClick?: () => void
}

const Button: React.FC<ButtonProps> = ({ text, className, onClick }) => {
  return (
    <button className={className} onClick={onClick}>
      {text}
    </button>
  )
}

// 使用组件
<Button text="Join Community" className="shadow-glow" />

编译后生成:

<button 
  data-mx-id="src/components/Button.tsx:12:4"
  data-mx-name="button"
  data-component-path="src/components/Button.tsx"
  data-component-line="12"
  data-component-file="Button.tsx"
  data-component-name="button"
  data-component-content="%7B%22className%22%3A%22shadow-glow%22%7D"
  class="shadow-glow"
>
  Join Community
</button>

Vue 示例

<!-- src/components/Button.vue -->
<template>
  <button :class="className" @click="handleClick">
    {{ text }}
  </button>
</template>

<script setup>
interface Props {
  text: string
  className?: string
}

const props = defineProps<Props>()
const emit = defineEmits<{
  click: []
}>()

const handleClick = () => {
  emit('click')
}
</script>

开发与测试

  1. 克隆仓库
  2. 安装依赖:npm install
  3. 构建插件:npm run build
  4. 运行示例:cd examples/react-demo && npm install && npm run dev
  5. 在浏览器中打开开发者工具查看 DOM 元素的数据属性

技术原理

  • React/JSX: 使用 Babel AST 解析和转换
  • Vue: 使用 @vue/compiler-dom 进行模板转换
  • 性能优化: 仅在开发环境运行,使用高效的字符串操作库
  • Source Map: 保持完整的调试信息和源码映射

API 参考

SourceIdentifierOptions

interface SourceIdentifierOptions {
  enabled?: boolean;           // 是否启用插件
  attributePrefix?: string;    // 属性前缀
  includeProps?: boolean;      // 是否包含组件属性
  include?: string[];          // 要处理的文件扩展名
  exclude?: string[];          // 要排除的文件模式
}

许可证

MIT

贡献

欢迎提交 Issue 和 Pull Request!


作者: MiniMax Agent