{"id":28445238,"url":"https://github.com/scriptedalchemy/tree-shake","last_synced_at":"2025-07-15T01:08:28.665Z","repository":{"id":294360866,"uuid":"986650425","full_name":"ScriptedAlchemy/tree-shake","owner":"ScriptedAlchemy","description":null,"archived":false,"fork":false,"pushed_at":"2025-05-20T03:09:07.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-11T03:32:37.650Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ScriptedAlchemy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-19T23:44:21.000Z","updated_at":"2025-05-20T03:09:10.000Z","dependencies_parsed_at":"2025-05-20T04:33:32.356Z","dependency_job_id":null,"html_url":"https://github.com/ScriptedAlchemy/tree-shake","commit_stats":null,"previous_names":["scriptedalchemy/tree-shake"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ScriptedAlchemy/tree-shake","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScriptedAlchemy%2Ftree-shake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScriptedAlchemy%2Ftree-shake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScriptedAlchemy%2Ftree-shake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScriptedAlchemy%2Ftree-shake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ScriptedAlchemy","download_url":"https://codeload.github.com/ScriptedAlchemy/tree-shake/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScriptedAlchemy%2Ftree-shake/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265383352,"owners_count":23756517,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-06T10:12:10.863Z","updated_at":"2025-07-15T01:08:28.622Z","avatar_url":"https://github.com/ScriptedAlchemy.png","language":"JavaScript","readme":"# Advanced JavaScript Macro System \u0026 On-the-Fly Optimization Demo\n\nThis repository demonstrates a Closure Compiler-inspired macro system for JavaScript. It includes:\n1.  A build-time preprocessor (`optimizer.js` in CLI mode) for conditional code removal based on a JSON configuration.\n2.  An advanced concept: simulating an Edge Worker (`edge-worker-simulation.js`) that uses this macro system to perform **per-request, on-the-fly JavaScript optimization** based on incoming request headers.\n\n## Core Features of the Macro System\n\n-   **Externalized Definitions**: Macro values (e.g., feature flags, environment variables) are passed as a JSON object to the optimizer, not hardcoded in comments within the source.\n-   **Conditional Code Blocks**: `/* @if(expression) */ ... /* @else */ ... /* @endif */` allow for including/excluding blocks of code.\n-   **Inline Conditional Expressions**: `if (/* @if(expression) */ false) { ... }` allows standard `if` statements to be processed by replacing `false` with the compile-time evaluation of the expression.\n-   **Dot Notation**: Supports nested macro variables (e.g., `device.isMobile`, `user.preferences.theme`).\n-   **Complex Expressions**: Conditions can use logical operators (`\u0026\u0026`, `||`, `!`) and comparisons.\n\n## 1. Build-Time Optimization (CLI)\n\nThe `optimizer.js` script can be used as a command-line tool to process a JavaScript file at build time.\n\n**Usage:**\n```bash\nnode optimizer.js \u003cinputFile.js\u003e \u003coutputFile.js\u003e '\u003cjsonDefinitions\u003e'\n```\n\n**Example:**\n```bash\nnode optimizer.js app-template.js processed-app.js '{\"device.isMobile\":false, \"user.language\":\"en\", \"experiment.group\":\"B\"}'\n```\nThis will read `app-template.js`, apply the macros based on the provided JSON, and write the optimized code to `processed-app.js`.\n\n## 2. On-the-Fly Optimization at the Edge (Simulated)\n\nThis is a more advanced demonstration showcasing how the same macro system could be used for runtime optimizations, for instance, within a Cloudflare Worker or similar edge compute environment.\n\n**Concept:**\n\nInstead of generating a single, universally optimized bundle at build time, the edge worker intercepts each incoming user request. It analyzes request headers (like `User-Agent`, `Accept-Language`, cookies for A/B testing, custom headers for feature flags) to dynamically construct a set of `defines` specific to that user and their context. It then processes a master JavaScript template (`app-template.js`) using these dynamic `defines` to generate a uniquely optimized JavaScript payload for that user, on the fly.\n\n**Files:**\n\n-   `app-template.js`: The master JavaScript application code containing various macros tied to user/device characteristics.\n-   `optimizer.js`: Contains the `optimizeJavaScript(code, defines)` function (the same core logic as the CLI tool).\n-   `edge-worker-simulation.js`: A Node.js script that simulates this edge worker environment. It defines several mock user requests with different headers, calls the optimizer for each, and prints the resulting tailored JavaScript.\n\n**How to Run the Edge Simulation:**\n```bash\nnode edge-worker-simulation.js\n```\nThis will output the processed JavaScript for several different simulated user profiles, demonstrating how the code changes based on the dynamic `defines`.\n\n**Benefits of On-the-Fly Edge Optimization:**\n\n-   **Highly Tailored Payloads**: Users download only the code they need, potentially improving performance, especially for diverse user bases or complex A/B testing scenarios.\n-   **Reduced Client-Side Logic**: Logic for feature flagging or device adaptation can be partly handled at the edge, simplifying client-side code.\n\n**Challenges:**\n\n-   **Latency**: Processing JavaScript on every request, even at the edge, adds latency. The optimizer must be extremely fast (e.g., implemented in Rust/WASM, like SWC itself, if it were to support this natively).\n-   **Caching**: Effective caching strategies for generated code (based on common `defines` combinations) would be crucial to mitigate latency.\n-   **Complexity**: Managing the JavaScript template and the logic for deriving `defines` can become complex.\n\n## Macro Syntax in `app-template.js`\n\n(See `app-template.js` for detailed examples.)\n\n-   **Block Condition:**\n    ```javascript\n    /* @if(device.isMobile \u0026\u0026 featureFlags.newMobileUI) */\n    console.log(\"Optimized for new mobile UI.\");\n    /* @else */\n    console.log(\"Using standard or desktop UI.\");\n    /* @endif */\n    ```\n-   **Inline Condition:**\n    ```javascript\n    if (/* @if(user.isLoggedIn) */ false) {\n      loadUserData();\n    }\n    ```\n\nThis repository provides the building blocks and a conceptual demonstration for such advanced optimization techniques. The core idea is to leverage a powerful macro system that can be applied both at build-time and, with careful consideration for performance, at runtime on the edge. ","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscriptedalchemy%2Ftree-shake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscriptedalchemy%2Ftree-shake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscriptedalchemy%2Ftree-shake/lists"}