@automattic/babel-plugin-replace-textdomain

中文文档
1.2.5发布于 2026/11/05GPL-2.0-or-laterCommonJS构建来源已验证近 30 天下载 1927

A Babel plugin to replace the textdomain in gettext-style function calls.

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Security
  4. License

babel-plugin-replace-textdomain

A Babel plugin to replace the textdomain in gettext-style function calls.

This may be useful when using node modules for code-sharing among multiple applications (e.g. WordPress plugins), so each application's bundle uses a consistent textdomain throughout.

Installation

Generally you'll install this via your package manager, e.g.

npm install --save-dev @automattic/babel-plugin-replace-textdomain

Usage

In your Babel config, you might include the plugin something like this:

{
    "plugins": [
        [ "@automattic/babel-plugin-replace-textdomain", { "textdomain": "new-domain" } ]
    ]
}

Plugin options are:

  • textdomain: Specify the replacement text domain. The value may be a string, which will replace all domains; an object, to map specific domains (leaving any others untouched); or a function, which will be passed the existing domain (empty string if the domain is missing entirely) and is expected to return the new domain (or null).

  • functions: Specify the functions that take domain arguments. This is an object mapping function names to the (zero-based) index of the domain argument.

    The default function list handles the __, _x, _n, and _nx functions provided by @wordpress/i18n. This list may be accessed as require( '@automattic/babel-plugin-replace-textdomain' ).defaultFunctions.

  • i18nModule: Specify the module name used for resolving import aliases. When a function is imported from this module and called under an alias, the plugin will still recognize it. Defaults to @wordpress/i18n.

  • requireI18nSource: Skip calls whose callee provably resolves to something other than i18nModule. Defaults to false.

    By default any call matching the function list is rewritten, including member calls like cache.__( key ). That is fine for hand-written source, but when the plugin runs over a whole bundle — where a dependency's own helpers sit in the same file as the real gettext calls — it rewrites calls that have nothing to do with i18n. With this option a callee is left alone once it can be traced to another module, while a callee whose origin can't be determined is still rewritten (so an unrecognized bundler shape doesn't silently leave a bundle untranslated).

To report instances of the specified i18n functions called without a domain or with an improper value for the domain, set the DEBUG environment variable to include @automattic/babel-plugin-replace-textdomain.

Security

Need to report a security vulnerability? Go to https://automattic.com/security/ or directly to our security bug bounty site https://hackerone.com/automattic.

License

babel-plugin-replace-textdomain is licensed under GNU General Public License v2 (or later)

版本与发布记录

@automattic/babel-plugin-replace-textdomain 共发布 72 个版本,首次发布于 2021/12/15;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.5latest2026/11/053 天前
1.2.42026/10/2118 天前
1.2.32026/10/2019 天前
1.2.22026/09/161 个月前
1.2.12026/08/063 个月前
1.2.02026/07/313 个月前

关于 @automattic/babel-plugin-replace-textdomain 的常见问题

@automattic/babel-plugin-replace-textdomain 是什么?

A Babel plugin to replace the textdomain in gettext-style function calls.

如何安装 @automattic/babel-plugin-replace-textdomain?

在项目目录运行 npm install @automattic/babel-plugin-replace-textdomain,或使用 pnpm add / yarn add / bun add。

@automattic/babel-plugin-replace-textdomain 支持 TypeScript 吗?

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

@automattic/babel-plugin-replace-textdomain 支持 ESM 还是 CommonJS?

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

@automattic/babel-plugin-replace-textdomain 可以商用吗?

@automattic/babel-plugin-replace-textdomain 使用 GPL-2.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以兼容的 GPL 许可开源。仅在内部使用、不对外分发时不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@automattic/babel-plugin-replace-textdomain 的下载量有多少?

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

@automattic/babel-plugin-replace-textdomain 还在维护吗?

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