@frontman-ai/dom-element-to-component-source

中文文档
0.5.0发布于 2026/09/09MITTypeScriptES Module近 30 天下载 7528

A library to retrieve the source location of DOM elements in React applications

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Browser API
  4. Browser Types
  5. Component Name Discovery
  6. Server API
  7. Server Result
  8. Source Location
  9. Requirements
  10. Development
  11. License

DOM Element to Component Source

A TypeScript library for finding React JSX source context from a DOM element and resolving React Server Component locations through source maps.

Features

  • Separates selected JSX definitions from React owner invocation ancestry.
  • Preserves client definitions nested beneath server owners.
  • Supports dynamic React Fiber keys and bounded traversal.
  • Resolves complete server source contexts in one operation.
  • Supports Turbopack, webpack, file URL, and map-relative source paths.
  • Returns structured source-resolution failures.

Installation

yarn add dom-element-to-component-source

Until an npm release is available, install an exact Git commit. The package's prepack script builds every declared dist export during installation.

yarn add dom-element-to-component-source@https://github.com/frontman-ai/dom-element-to-component-source.git#<full-commit-sha>

Browser API

import { getElementSourceContext } from 'dom-element-to-component-source'

const button = document.querySelector('button')
if (!button) throw new Error('Button not found')

const result = await getElementSourceContext(button, { maxDepth: 10 })
if (!result.success) {
  throw new Error(result.error)
}

const { definition, invocations } = result.data
if (definition) {
  console.log(`${definition.file}:${definition.line}:${definition.column}`)
}

for (const invocation of invocations) {
  console.log(`${invocation.file}:${invocation.line}:${invocation.column}`)
}

definition is the selected element's JSX definition when React exposes one. invocations contains owner call sites ordered nearest to farthest. React Server Component locations remain about://React/Server/... URLs until resolved by the server API.

Browser Types

interface ElementSourceContext {
  definition?: SourceLocation
  invocations: SourceLocation[]
}

type ElementSourceContextResult =
  | { success: true; data: ElementSourceContext }
  | { success: false; error: string }

interface ElementSourceContextOptions {
  maxDepth?: number
}

Component Name Discovery

Use getElementComponentName when only the nearest eligible React component name is needed. It checks owner Fibers before return Fibers and inspects at most 10 nodes in each bounded traversal by default.

import { getElementComponentName } from 'dom-element-to-component-source'

const name = getElementComponentName(element, {
  excludedNames: ['FrameworkWrapper'],
  includeUnderscorePrefixed: false,
})
interface ElementComponentNameOptions {
  maxDepth?: number
  excludedNames?: readonly string[]
  includeUnderscorePrefixed?: boolean
}

Fragment and Suspense are always ignored. Caller exclusions are added to those structural exclusions. Underscore-prefixed names are included unless includeUnderscorePrefixed is false.

Server API

Import server functionality from the dedicated server entry. Never import this entry into browser code.

import { resolveElementSourceContext } from 'dom-element-to-component-source/server'

const result = await resolveElementSourceContext(context, {
  projectRoot: '/absolute/path/to/project',
})

if (!result.success) {
  console.error(result.error.code, result.error.message)
  return
}

console.log(result.data.definition)
console.log(result.data.invocations)

The resolver preserves ordinary locations and resolves every about://React/Server/file:///... definition and invocation. Generated files and source maps must resolve inside the canonical projectRoot before they are read.

Returned source paths are untrusted output. Original source-map entries may resolve outside projectRoot and do not need to exist. Consumers must authorize every returned path before exposing it or reading from it. Consumer policy can include an allowed source root, existence checks, canonical symlink containment, and conversion to a relative path.

Next.js applications should externalize this package. Its private source-map dependency remains an implementation detail and can load its adjacent mappings.wasm file at runtime:

module.exports = {
  serverExternalPackages: ['dom-element-to-component-source'],
}

Server Result

type SourceResolutionResult =
  | { success: true; data: ElementSourceContext }
  | {
      success: false
      error: {
        code:
          | 'INVALID_REACT_URL'
          | 'GENERATED_FILE_NOT_FOUND'
          | 'SOURCE_MAP_NOT_FOUND'
          | 'POSITION_NOT_FOUND'
          | 'RESOLUTION_FAILED'
        message: string
      }
    }

Source Location

interface SourceLocation {
  file: string
  line: number
  column: number
  componentName?: string
  tagName?: string
  sourceCode?: string
  componentProps?: Record<string, SerializableValue>
}

Requirements

  • Node.js 20.19 or newer for server resolution.
  • React development metadata for browser extraction.
  • Source maps for React Server Component resolution.

Development

yarn install --immutable
yarn test:run
yarn type-check
yarn build
yarn pack --dry-run

License

MIT

版本与发布记录

@frontman-ai/dom-element-to-component-source 共发布 1 个版本,首次发布于 2026/09/09;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.5.0latest2026/09/092 个月前

关于 @frontman-ai/dom-element-to-component-source 的常见问题

@frontman-ai/dom-element-to-component-source 是什么?

A library to retrieve the source location of DOM elements in React applications

如何安装 @frontman-ai/dom-element-to-component-source?

在项目目录运行 npm install @frontman-ai/dom-element-to-component-source,或使用 pnpm add / yarn add / bun add。

@frontman-ai/dom-element-to-component-source 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frontman-ai/dom-element-to-component-source 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@frontman-ai/dom-element-to-component-source 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@frontman-ai/dom-element-to-component-source 可以商用吗?

@frontman-ai/dom-element-to-component-source 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@frontman-ai/dom-element-to-component-source 的下载量有多少?

根据 npm 官方下载统计,@frontman-ai/dom-element-to-component-source 在 2026/10/16 – 2026/11/14 这 30 天内被下载 7,528 次,日均约 251 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frontman-ai/dom-element-to-component-source 还在维护吗?

最新版本 0.5.0 发布于 2026/09/09(2 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/09/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。