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

batchreplacemodule

v1.0.0

Published

批量模块替换插件,用于在 Webpack 构建过程中根据 replacementMap 实现模块路径的自动替换和兜底查找。

Readme

batchReplaceModule

简介

batchReplaceModule 是一个用于批量模块路径替换的工具,包含一个 Node.js 脚本(buildFiles.js)和一个 Webpack 插件(BatchModuleReplacementPlugin.js)。它可以自动收集项目源码文件,并在 Webpack 构建过程中实现模块路径的自动替换和兜底查找,支持 JS/Vue/JSON 等多种扩展名,提升多项目/多分支开发与维护效率。


你的目录结构

main/
├── project/
│   └── [PROJECT_NAME]/
│       └── src/
│           └── ...(项目源码文件)
└── src/
│     └── ...(主干源码文件)
├── package.json

使用说明

1. 生成文件映射(buildFiles.js)

环境变量

  • PROJECT:指定当前项目名称(对应 project/[PROJECT]/src 目录)。

配置 buildFiles.js

const { buildFiles } = require('batchReplaceModule');
buildFiles();

用法

在命令行中执行:

set PROJECT=your_project_name
node buildFiles.js

执行后会在 project/[PROJECT]/files.json 生成所有源码文件的绝对路径列表。


2. Webpack 插件集成(BatchModuleReplacementPlugin.js)

引入插件

const { BatchModuleReplacementPlugin } = require('batchReplaceModule');
// 引入buildFiles.js生成的files.json数据转为Map
let replacementMap = new Map()
const PROJECT = process.env.PROJECT
if (PROJECT) {
	const projectPath = path.resolve(__dirname, '..', 'project', PROJECT)
	const files = require(path.join(projectPath, 'files.json'))
    replacementMap = new Map(Object.entries(files));
}

配置 Webpack

module.exports = {
  // ...其他配置
  plugins: [
    new BatchModuleReplacementPlugin(replacementMap)
  ]
};

插件功能

  • 自动查找 trunk(主干)目录下的同名文件进行替换。
  • 支持 JS/Vue/JSON 等扩展名。
  • 支持 replacementMap 映射的路径优先替换。
  • 控制台输出替换和查找信息,便于调试。

典型场景

  • 多项目/多分支共用主干代码,分支可覆盖主干部分模块,未覆盖部分自动兜底主干实现。
  • 需要批量替换模块路径,提升开发效率。

注意事项

  • Node.js 版本需支持 fs.promises
  • buildFiles.js 需在每次源码变更后重新执行,确保 files.json 最新。
  • replacementMap 的生成和维护需结合实际业务需求。

License

MIT