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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rollup-plugin-literal-replacer

v0.0.3

Published

Rollup plugin for replacing string literals in specific function calls with customizable transformation rules. Ideal for i18n key hashing and localization workflows.

Readme

rollup-plugin-literal-replacer

npm License: MIT

一个Rollup插件, 用于替换函数调用中的字面量参数。

例如

// 假设你有以下代码:
foo('hello', 'world')
// 使用插件的自定义替换逻辑
function transform(val) {
  return val.slice(0, 1)
}
// 最终的代码将被替换为:
foo('h', 'w')

特性

  • 🔍 精准定位 - 通过 AST 分析精准定位目标函数调用
  • 🔧 高度可配置 - 支持自定义匹配规则和替换逻辑
  • 🗺️ 完整的 Sourcemap - 使用 Magic-string 生成精准源码映射

安装

npm install rollup-plugin-literal-replacer -D
# 或
yarn add rollup-plugin-literal-replacer -D

使用方式

基础配置

// rollup.config.js
import literalReplacer from 'rollup-plugin-literal-replacer';

export default {
  plugins: [
    literalReplacer({
      functions: ['t', '$t'],
      include: "src/**/*.js",
      exclude: "node_modules/**"
    })
  ]
};

配置选项

| 选项 | 类型 | 默认值 | 说明 | |---------------|-----------------------------------|---------------------------------------------|----------------------------------| | include | string | RegExp | (string | RegExp)[] | "src//*.{js,ts,jsx,tsx,vue}" | 需要处理的文件路径模式 | | exclude | string | RegExp | (string | RegExp)[] | "node_modules/" | 排除处理的文件路径模式 | | functions | string[] | ['t', '$t'] | 需要处理的目标函数名 | | shouldReplace | (value: string) => boolean | () => true | 会对函数参数的每个字面量进行判断是否需要替换的过滤函数 | | transform | (value: string) => string | (value) => value | 自定义字符串替换逻辑 |

示例

自定义替换逻辑

literalReplacer({
  functions: ['i18n'],
  transform = (value) => {
    const index = value.indexOf('/');
    const prefix = value.slice(0, index);
    const message = value.slice(index + 1);
    return `${prefix}${crc32(message).toString(16)}`;
  }
})

函数参数是否被替换

// foo("ab", "ac", "b")
literalReplacer({
  shouldReplace(value) {
    return value.startsWith("a")
  }
})

工作原理

  • AST 解析 - 使用 Rollup 内置的 AST 解析器分析代码结构
  • 目标定位 - 通过 estree-walker 遍历定位目标函数调用
  • 精准替换 - 结合AST信息通过使用 magic-string 进行代码修改
  • 映射生成 - 自动生成高精度 Sourcemap 保持调试能力