opti-mommy
v0.1.3
Published
Opti-Mommy — semantic JavaScript optimizer (MVP)
Readme
💗 Opti-Mommy
A semantic JavaScript optimizer that analyzes your code and generates an optimized version automatically.
Opti-Mommy is a JavaScript optimizer focused on AST-based transformations. It reads your source code, analyzes it, applies optimization passes, and writes the optimized result to dist/ without modifying your original source files.
The project is implemented entirely in plain modern JavaScript (ESM).
✨ Features
- 🧠 AST-based JavaScript analysis
- ⚡ Automatic code optimization
- 🛡️ SAFE optimization mode
- 🔥 AGGRESSIVE optimization mode
- 👀 Automatic file watching during development
- 💾 Content-based caching
- 🗂️ Separate
src/anddist/directories - 🧪 Automated tests with Vitest
- 📦 npm-compatible CLI
- 🚫 Never modifies your original source files
📦 Installation
Install dependencies
npm installInstall globally
To use the opti-mommy command globally:
npm install -g .You can then run:
opti-mommyYou can also run the CLI directly:
node src/cli/index.js🚀 Quick Start
Place your JavaScript files inside a src/ directory:
my-project/
├── src/
│ ├── app.js
│ └── utils.js
├── package.json
└── ...Start the development optimizer:
opti-mommy devOpti-Mommy watches src/ for changes and automatically generates optimized files inside dist/.
src/app.js
↓
Opti-Mommy
↓
dist/app.jsYour original source files remain untouched.
🛠️ Commands
opti-mommy dev
Starts development mode and watches src/ for changes.
opti-mommy devChanged JavaScript files are automatically optimized and written to dist/.
opti-mommy dev --aggressive
Starts development mode using the AGGRESSIVE optimization pipeline.
opti-mommy dev --aggressiveopti-mommy build
Builds the entire project using SAFE optimizations.
opti-mommy buildopti-mommy build --aggressive
Builds the entire project using both SAFE and AGGRESSIVE optimization passes.
opti-mommy build --aggressiveopti-mommy run
Runs the generated code from dist/ instead of executing the original source from src/.
opti-mommy run🛡️ SAFE Mode
SAFE mode is the default optimization mode.
opti-mommy buildIt focuses on transformations that are highly likely to preserve the original behavior of the program.
Current SAFE passes include:
- Constant folding
- Dead-code elimination
The optimization priority is:
Correctness
↓
Compatibility
↓
PerformanceIf an optimization cannot be considered sufficiently safe, it should not be applied in SAFE mode.
🔥 AGGRESSIVE Mode
AGGRESSIVE mode can be enabled explicitly:
opti-mommy build --aggressiveIt runs all SAFE passes first and then enables additional optimization passes that require deeper analysis.
Current AGGRESSIVE optimization:
- Conservative function inlining
AGGRESSIVE does not mean blindly rewriting code.
Every optimization must have defined rules and tests designed to prevent unnecessary behavioral changes.
🧠 How It Works
Opti-Mommy uses an AST-based optimization pipeline:
JavaScript source
↓
Parser
↓
AST
↓
Analyzer
↓
Optimization Pipeline
↓
Optimization Passes
↓
Code Generator
↓
Optimized JavaScript
↓
dist/This architecture allows new optimization passes to be added without rewriting the entire optimizer.
💾 Cache
Opti-Mommy uses content hashing to avoid unnecessarily processing files that have not changed.
The cache also takes the optimization mode into account.
For example:
app.js + SAFEand:
app.js + AGGRESSIVEare treated as different optimization results.
This prevents an AGGRESSIVE build from accidentally reusing a SAFE build result.
📁 Project Structure
opti-mommy/
├── src/
│ ├── cli/
│ ├── parser/
│ ├── analyzer/
│ ├── optimizer/
│ │ └── passes/
│ │ ├── safe/
│ │ └── aggressive/
│ ├── generator/
│ ├── watcher/
│ └── cache/
│
├── tests/
│ ├── optimizer/
│ │ ├── safe/
│ │ └── aggressive/
│ ├── parser/
│ └── integration/
│
├── package.json
└── README.mdAll implementation code is written in JavaScript.
There is no TypeScript in the project.
🧰 Technology
| Component | Technology |
| -------------- | ------------------ |
| Runtime | Node.js |
| Language | JavaScript (ESM) |
| Parser | @babel/parser |
| Code generator | @babel/generator |
| File watcher | chokidar |
| Tests | Vitest |
| Cache | Content hash |
🧪 Testing
Run the test suite with:
npm testTests cover optimization passes and core functionality of the optimizer.
When adding a new optimization, add its implementation to the appropriate safe/ or aggressive/ directory and create corresponding tests.
📊 Optimization Pipeline
SAFE
A simplified SAFE build looks like:
Parser
↓
Constant Folding
↓
Dead Code Elimination
↓
GeneratorAGGRESSIVE
An AGGRESSIVE build looks like:
Parser
↓
Constant Folding
↓
Dead Code Elimination
↓
Conservative Function Inlining
↓
GeneratorThe pipeline is modular, allowing additional optimization passes to be added independently.
⚠️ Important
Opti-Mommy performs source-to-source transformations and should be tested before being used in production.
Extra caution is recommended when using:
opti-mommy build --aggressiveAlways benchmark optimized code against the original when performance is important.
🎯 Philosophy
Opti-Mommy is not intended to be just a minifier.
The goal is to build a semantic JavaScript optimizer that understands the structure of JavaScript through its AST and applies transformations based on that structure.
The intended workflow is:
Write JavaScript
↓
Opti-Mommy analyzes it
↓
Opti-Mommy optimizes it
↓
Optimized JavaScript
↓
Run the optimized versionAll while keeping the developer's original source code intact.
📄 License
See the project's license information for details.
