Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Uglifyjs Laravel Package

gkralik/php-uglifyjs

PHP port of Dean Edwards’ JavaScript Packer for minifying/packing JS. Install via Composer and compress a script with GK\JavascriptPacker->pack(), choosing encoding (0/10/62/95), optional fast decoder, and special chars handling.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Performance Optimization Roadmap: Enables client-side JavaScript minification to reduce payload sizes, directly impacting Core Web Vitals (LCP, FID) and TTFB for asset-heavy Laravel applications. Aligns with initiatives to achieve Google PageSpeed scores >90 or Lighthouse 100.
  • Build vs. Buy Decision: Avoids reinventing minification logic while sidestepping the complexity of Node.js tooling (e.g., Webpack, Terser). Ideal for teams prioritizing PHP-native solutions or constrained by legacy infrastructure (e.g., shared hosting without Node).
  • Security-Obsessed Development: Supports obfuscation of proprietary logic (e.g., licensing checks, anti-tampering) in client-side JavaScript, reducing exposure to reverse engineering. Useful for SaaS applications or enterprise dashboards with sensitive client-side logic.
  • Cost Efficiency: Zero licensing fees (LGPL-2.1) and minimal runtime overhead (~10KB), making it viable for bootstrapped startups or high-traffic projects where every millisecond/millibyte matters.
  • Legacy System Modernization: Bridges the gap for older Laravel apps (pre-Laravel Mix) or monolithic PHP applications migrating to modern frontend practices without a full rewrite.
  • Edge-Case Scenarios: Enables runtime JS compression for:
    • Dynamic JavaScript generation (e.g., CMS plugins, user-specific scripts).
    • API responses serving JavaScript (e.g., SPAs, real-time apps).
    • Embedded scripts in emails, PDFs, or third-party integrations.

When to Consider This Package

Adopt When:

  • Your primary constraint is performance, and you’re targeting:
    • Large JavaScript bundles (>50KB) where minification yields measurable gains.
    • Legacy browsers (IE11+) where modern tools like Terser may not be viable.
    • High-traffic pages (e.g., homepages, product listings) where payload size directly impacts conversions.
  • You lack Node.js expertise but need JS minification in a PHP-centric stack.
  • Your build process is simple: You’re not using Webpack/Vite and rely on Laravel Mix or custom asset pipelines.
  • Obfuscation is a priority: You need to hide logic (e.g., API keys, algorithms) from client-side inspection.
  • You’re constrained by hosting: Shared environments without Node.js or PHP-only deployments (e.g., Heroku, Platform.sh).
  • You’re working with dynamic JS: Generating scripts at runtime (e.g., Blade templates, API responses) and need compression before delivery.

Look Elsewhere When:

  • Modern JavaScript is critical: Your app uses ES6+ features (classes, arrow functions, modules) that this package does not support (based on 2006-era tech).
  • Advanced optimizations are needed: You require tree-shaking, dead-code elimination, or source maps (use Terser or esbuild instead).
  • You’re building a SPA or modern frontend: Frameworks like React/Vue/Svelte need Webpack/Vite for optimal bundling.
  • Real-time compression is required: This is build-time only; for edge/CDN compression, use Brotli/Gzip or Cloudflare Polish.
  • Your team has Node.js expertise: Native tools (Terser, esbuild) offer better performance and features.
  • Security is a higher priority than obfuscation: This package does not sanitize input—malicious JS could break or exploit the compressor. Use DOMPurify or eslint-plugin-no-unsanitized for safety.
  • You need long-term maintenance: The package is unmaintained (last commit 2016). Forking may be required for critical fixes.

How to Pitch It (Stakeholders)

For Executives (C-Suite, Product Leaders)

*"This PHP package lets us shrink JavaScript files by up to 50% without adding Node.js complexity—directly improving page load times and reducing bandwidth costs. For a low-code, zero-cost solution, it’s a no-brainer for:

  • Speeding up legacy Laravel apps (e.g., e-commerce, dashboards) where every millisecond counts.
  • Protecting proprietary client-side logic (e.g., licensing, anti-tampering) from competitors or bad actors.
  • Avoiding vendor lock-in while delivering immediate performance wins.

Why now? Our [Competitor X] loads in 3.2s; we can match that with minimal effort. The trade-off? We sacrifice modern JS features, but for our target audience (enterprise users on IE11), this is a high-ROI fix."*


For Engineering Leaders (CTOs, Tech Leads)

*"Pros:

  • No Node.js dependency: Runs in PHP, simplifying CI/CD and reducing toolchain complexity.
  • Seamless Laravel integration: Works with Mix, Forge, or custom pipelines without rewrites.
  • Obfuscation: Hides sensitive logic (e.g., API keys, business rules) in client-side JS.
  • Lightweight: ~10KB footprint; negligible impact on server resources.

Trade-offs:

  • ES3/ES5 only: Breaks with modern JS (ES6+). Requires Babel/Webpack transpilation first.
  • Unmaintained: Last update in 2016. We’ll need to fork for critical fixes (e.g., security).
  • No source maps: Debugging minified JS is harder (use fastDecode: true as a workaround).

Recommendation:

  • Use for: Legacy apps, dynamic JS, or obfuscation needs.
  • Avoid for: New projects, SPAs, or apps needing ES6+ support.
  • Pair with: Laravel Mix (for static assets) or a forked version for long-term use.

Alternatives:

  • Terser (Node.js): Better compression, ES6+ support, but adds Node dependency.
  • esbuild: Faster than Terser, but overkill for simple minification.
  • Brotli/Gzip: Compress at CDN level instead of runtime."*

For Developers (Frontend/Backend)

*"Quick Start:

composer require gkralik/php-uglifyjs

Basic Usage:

$packer = new GK\JavascriptPacker($yourJsCode, 62, true, false);
$minified = $packer->pack(); // Returns compressed JS

Where to Use It:

  1. Blade Templates: Minify inline JS.
    @packJs($script) // Custom Blade directive
    
  2. Asset Pipeline: Replace Laravel Mix’s JS minification.
    // app/Providers/AppServiceProvider.php
    $this->app->singleton('jsPacker', fn() => new GK\JavascriptPacker('', 62));
    
  3. API Responses: Compress JS payloads dynamically.
    return response()->json(['script' => app('jsPacker')->pack($js)]);
    

Gotchas:

  • ES6+ breaks it: Avoid const, let, arrow functions, or modules.
  • No input sanitization: Validate JS before passing it in (malicious input can crash the packer).
  • Encoding levels matter:
    • 62 (default): Balanced compression/readability.
    • 95: Aggressive obfuscation (harder to debug).
    • 'None': No compression (for debugging).

Debugging:

  • Use fastDecode: true to include a decoder for debugging minified JS.
  • Fallback to original JS if compression fails:
    try {
        return app('jsPacker')->pack($js);
    } catch (\Exception $e) {
        return $js; // Graceful degradation
    }
    ```"*
    
    

For DevOps/SRE

*"Deployment Impact:

  • No build step changes: Compression happens at runtime (no Webpack/Vite dependency).
  • CPU-intensive: Compression is CPU-bound; avoid on high-traffic endpoints.
  • Memory usage: Large scripts (>100KB) may spike memory (test with memory_get_usage()).

Scaling Strategies:

  1. Cache compressed JS: Use Redis/Memcached with TTL (e.g., 1 hour).
    $cacheKey = 'js:minified:' . md5($js);
    $minified = cache()->remember($cacheKey, 3600, fn() => app('jsPacker')->pack($js));
    
  2. Offload to queues: For large scripts (e.g., admin dashboards).
    dispatch(new CompressJsJob($js))->onQueue('compression');
    
  3. Monitor performance:
    • Track `js_compression
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle