@leonardwoo/func-marger
v0.2.0
Published
Merge functions from multiple JS files or directories into a single class with static methods.
Readme
Javascript merger function CLI (func-marger)
A fast JavaScript/TypeScript function packaging tool using Babel AST and Terser. It merges function declarations across multiple files and directories into a unified JavaScript Class containing static methods.
Features
- 📦 Directory & File Merging: Recursively scans and collects
.js,.mjs,.cjs,.ts, and.tsxfiles. - ⚡ Static Class Generation: Packs standalone function exports into
staticclass methods. - 🗜️ Built-in Terser Minification: Supports production-ready code compression (
--minify). - 🗺️ Chained Source Maps: Generates precise
.mapfiles across AST transformations and minification passes (--sourcemap).
Installation
# Global installation
npm install -g @leonardwoo/func-marger
# Or locally within your project
npm install --save-dev @leonardwoo/func-margerCLI Usage
func-marger <output> <input...> [options]Options
| Flag | Short | Default | Description |
| :--- | :--- | :--- | :--- |
| --class <name> | | Demo | Specify the generated class name |
| --minify | -m | false | Enable code compression |
| --banner | -b | false | Prepend copyright banner generated from package.json |
| --sourcemap | -s | false | Generate source maps (.map) along with the output file |
| --version | -v | | Print CLI version and exit |
CLI Examples
# 1. Merge an entire directory into a class
func-marger dist/Utils.js src/methods/ --class MyUtils
# 2. Mix directories and individual files
func-marger dist/Utils.js src/methods/ src/string.js --class AppTools
# 3. Production build with minification and source map generation
func-marger dist/Utils.min.js src/methods/ --class AppTools --minify --sourcemapProgrammatic API (Node.js)
You can import and use mergeFunctions directly in your Node.js scripts or build tools.
import { mergeFunctions } from "@leonardwoo/func-marger";
const files = ["./src/math.js", "./src/string.js"];
// Execution returns an object containing 'code' and 'map'
const { code, map } = await mergeFunctions(files, "MyUtils", {
minify: true,
sourcemap: true,
outputPath: "dist/MyUtils.min.js",
});
console.log(code);API Options
interface MergeOptions {
minify?: boolean; // Enable Terser code minification (default: false)
sourcemap?: boolean; // Generate Source Map (default: false)
outputPath?: string; // Target file path used for sourcemap linkage
}Example Transformation
Input
src/math.js
export async function add(a, b) {
return a + b;
}src/string.js
export function* generateTags(str) {
yield str.toUpperCase();
yield str.toLowerCase();
}Output
class AppTools {
static async add(a, b) {
return a + b;
}
static *generateTags(str) {
yield str.toUpperCase();
yield str.toLowerCase();
}
}License
MIT
