@fullstory/babel-plugin-annotate-react

中文文档
2.4.1发布于 2026/10/01MITCommonJS零依赖近 30 天下载 41.5万

A Babel plugin that annotates React components, making them easier to target with FullStory search

原始使用文档
文档目录11 节
  1. with npm
  2. with yarn
  3. Babel configuration
  4. React Native
  5. setFSTagName setting
  6. Fragments
  7. React Compiler
  8. Ignoring Components
  9. Sample Apps
  10. Getting Help
  11. React Native

Babel Plugin: Annotate React

CircleCI

This is a Babel plugin that annotates React components with stable attributes that can be used to search and select using FullStory. This is most useful when using a React system that generates dynamic names for Components or rearranges elements.

Getting started

with npm

npm i @fullstory/babel-plugin-annotate-react --save

with yarn

yarn add @fullstory/babel-plugin-annotate-react

Babel configuration

Add the @fullstory/babel-plugin-annotate-react plugin to your babel plugin configuration (e.g., babel.config.js).

module.exports = {
  /* ... */
  plugins: [
    /* ... */
    "@fullstory/babel-plugin-annotate-react"
  ]
};

Description

For React on the web, the attributes data-component, data-element, and data-source-file are added to each element. For React Native, the attributes added are dataComponent, dataElement, and dataSourceFile.

Note that for both web and native, these attributes will appear as kebab-case in the Fullstory application due to backend processing. End users of Fullstory should create search filters (i.e. segments, metrics, etc) using kebab-case.

The component attribute names the React.Component and the element attribute names the original native elements like View or Image or an emitter of DOM elements like Fragment.

Example input:

class HelloComponent extends Component {
  render() {
    return <div>
      <h1>Hello world</h1>
    </div>;
  }
}

Example JS output:

class HelloComponent extends Component {
  render() {
    return React.createElement("div", {
      "data-component": "HelloComponent",
      "data-file-source": "hello-component.js"
    }, React.createElement("h1", {
      null
    }, "Hello world"));
  }
}

Final render:

<div data-component="HelloComponent" data-file-source="hello-component.js">
  <h1>Hello world</h1>
</div>

Configuration options

React Native

To activate React Native support you must pass in a native plugin option like so:

plugins: [
  ["@fullstory/babel-plugin-annotate-react", { native: true }]
]

See Getting Started with FullStory React Native Capture for more info.

setFSTagName setting

When using this library with FullStory for Mobile Apps, we recommend setting setFSTagName: true to generate better privacy selectors. This setting will automatically set fsTagName with the value of dataElement or dataComponent, which will truncate the privacy selector and avoid duplicate naming.

Example:

  • Before RCTSafeAreaView[data-source-file="App.tsx"][data-element="SafeAreaView"][data-component="App"]
  • After App[data-source-file="App.tsx"]
plugins: [
  '@fullstory/react-native',
  ["@fullstory/annotate-react", {
    native: true,
    setFSTagName: true,
  }]
]

⚠️ Important: Existing FullStory privacy selectors and defined elements may need to be updated if the app was previously published without setFSTagName: true.

Fragments

By default, the plugin does not annotate React.Fragments because they may or may not contain a child that ends up being an HTML element.

An example with no child element:

const componentName = () => (
  <Fragment>Hello, there.</Fragment>
);

An example with child elements:

const componentName = () => (
  <Fragment>
    Some text
    <h1>Hello, there.</h1> /* This one could be annotated */
    <a href="#foo">Click me</a>
  </Fragment>
);

If you would like the plugin to attempt to annotate the first HTML element created by a Fragment (if it exists) then set the annotate-fragments flag:

plugins: [
  ["@fullstory/babel-plugin-annotate-react", { "annotate-fragments": true }]
]

React Compiler

If your project uses the React Compiler, enable the reactCompiler option:

plugins: [
  ["@fullstory/babel-plugin-annotate-react", { reactCompiler: true }]
]

The React Compiler optimizes function components by extracting JSX subtrees out of return statements into intermediate cached variables. This means a compiled component may look like:

// Before React Compiler
function MyComponent() {
  return <div className="foo">Hello</div>;
}

// After React Compiler (simplified)
function MyComponent() {
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = <div className="foo">Hello</div>;
    $[0] = t0;
  }
  return t0;
}

Without reactCompiler: true, the plugin would see a non-JSX return statement and skip the component entirely, producing no annotations. With the option enabled, the plugin traverses the entire function body to find and annotate all JSX nodes, regardless of where they appear.

⚠️ Important: Because the React Compiler restructures how JSX is emitted, enabling reactCompiler: true may change which JSX nodes get annotated. Selectors and named elements previously defined in Fullstory are not guaranteed to match after enabling this option. Review your existing segments, metrics, and defined elements after activating this setting.

Ignoring Components

If you would like the plugin to skip the annotation for certain components, use the ignoreComponents option:

  plugins: [
      [
        "@fullstory/annotate-react",
        {
          ignoreComponents:[
            // each item must be a string array containing three items: file name, component name, element name
            // corresponding to the values for data-source-file, data-component, data-element
            // use wild card (*) to match anything
            ["myBoxComponent.jsx","MyBox","Box"],
            ["App.jsx", "*", "ThemeProvider"], // use wild-card to match anything
            ["App.jsx", "App", "*"],
          ]
        }
      ],
  ]

Sample Apps

We have a few samples to demonstrate this plugin:

Much of the logic for adding the attributes originated in the transform-react-qa-classes plugin.

Getting Help

Please refer to our Knowledge Base article or contact [email protected] for additional help.

React Native

Please see our Getting Started with FullStory React Native Capture guide or email [email protected] for additional help.

版本与发布记录

@fullstory/babel-plugin-annotate-react 共发布 16 个版本,首次发布于 2020/05/30;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.4.1latest2026/10/011 个月前
2.4.02026/04/087 个月前
2.3.22025/04/181.6 年前
2.3.12024/12/032 年前
2.3.02023/09/263 年前
2.2.22023/02/073 年前

关于 @fullstory/babel-plugin-annotate-react 的常见问题

@fullstory/babel-plugin-annotate-react 是什么?

A Babel plugin that annotates React components, making them easier to target with FullStory search

如何安装 @fullstory/babel-plugin-annotate-react?

在项目目录运行 npm install @fullstory/babel-plugin-annotate-react,或使用 pnpm add / yarn add / bun add。

@fullstory/babel-plugin-annotate-react 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fullstory__babel-plugin-annotate-react 是否存在。

@fullstory/babel-plugin-annotate-react 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fullstory/babel-plugin-annotate-react 需要什么版本的 Node.js?

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

@fullstory/babel-plugin-annotate-react 可以商用吗?

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

@fullstory/babel-plugin-annotate-react 的下载量有多少?

根据 npm 官方下载统计,@fullstory/babel-plugin-annotate-react 在 2026/10/16 – 2026/11/14 这 30 天内被下载 415,462 次,日均约 13,849 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fullstory/babel-plugin-annotate-react 还在维护吗?

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