How adopisoft/minify-dir works
Main Components
• Input Source Directory: Represents the directory supplied by the user (e.g., “src”) containing files and subdirectories.
• File Walker/Chooser: A module that recursively processes the directory tree, applies “excludePatterns” to ignore files/folders (like .git or any other configured exclusions), and categorizes files based on type (JS, TS, or non-code assets).
• Code Removal Module: This component (represented by src/remove-code.ts) processes files and removes blocks of code marked for removal if conditions are met (e.g., removeIf(prod) … endRemoveIf(prod)).
• Minification Engine: Integrated with the “uglify-js” library, it is responsible for minifying JavaScript code. This is optionally engaged if the "minify" flag is true.
• Typescript Transpiler Module: Triggered via src/tsc.ts when processing TypeScript files using provided tsc configuration options.
• Output/Copier Module: This part handles writing the processed files (minified or transpiled) into the destination directory (“dest”, such as “release”), and copying non-JS files as per the “copy” flag.
• Option Handler & Configurator: Initializes and passes the options object (minify, copy, removeCode conditions, tsc options, etc.) and sets the basePath.
• Logging/Error Handling: Although not explicitly a separate file, include components for logging (like the console outputs in usage) and catching errors during the process.
Design the Interactions Between Components
• The process begins when a request is made (via the minifyDir call) with a source directory and options.
• The File Walker component scans the source, filters files based on excludePatterns, and sends each file for processing.
• JS and TS files are routed to the Code Removal Module first; the code blocks are conditionally removed.
• Once refined, JS files are sent to the Minification Engine, while TS files are forwarded to the Typescript Transpiler.
• In parallel, non-code assets (files that are not JavaScript/TypeScript) are handled by the Output/Copier Module and simply copied to the destination if “copy” is true.
• Processed files are then stored in the appropriate subdirectories within the destination directory, mirroring the original structure.
• Error handling and logging are present throughout all phases to capture issues and notify the user.
Architectural Patterns & Design Principles
• Separation of Concerns: Each component (e.g., file handling, code removal, minification, transpilation) is isolated for clarity and maintainability.
• Modular Design: The different functionality is split into distinct modules (e.g., index.ts for coordination, remove-code.ts for block removal).
• Pipeline / Flow-Based Processing: Files pass through a sequential pipeline where each step (filtering, code removal, transformation, copying) is clearly demarcated.
• Configurability: The behavior of the system is driven by an options object that tailors the flow (e.g., toggling minification, setting tsc options, and specifying removal conditions).