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

Jshrink Laravel Package

tedivm/jshrink

Native PHP JavaScript minifier for shrinking JS on the fly (cache recommended). Simple API: JShrink\Minifier::minify($js), with options like disabling flagged comment preservation. BSD licensed.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require tedivm/jshrink. Ensure mbstring extension is enabled (required dependency). Use the minimal API immediately:

use JShrink\Minifier;

$minified = Minifier::minify($jsString);

Start with a small, self-contained script in a test route or tinker session to validate functionality and environment compatibility—this avoids early surprises in production.

Implementation Patterns

  • Static Asset Preprocessing: Integrate into deploy hooks (e.g., custom Artisan command) to minify JS files before deployment:
    $minified = Minifier::minify(file_get_contents('public/js/app.js'));
    file_put_contents('public/js/app.min.js', $minified);
    
  • On-Demand Minification with Caching: For dynamic JS (e.g., inline templates), cache results:
    $key = 'minified_js_' . md5($jsString);
    $minified = Cache::remember($key, 3600, fn() => Minifier::minify($jsString));
    return $minified;
    
  • Configuration per-context: Override quote style or preserve special patterns using options:
    Minifier::minify($jsString, ['quoteStyle' => Minifier::QUOTE_PRESERVE]);
    
  • Legacy PHP Fallback: As a stopgap in environments where Node.js tooling (e.g., Laravel Mix) is unavailable or prohibited—only when all other options are infeasible.

Gotchas and Tips

  • No ES6+ Support: Fails silently on modern syntax (e.g., arrow functions, classes, modules). Test rigorously with your codebase—this is not a modern minifier.
  • License Ambiguity: NOASSERTION legally blocks most enterprise adoption. Confirm internally if legal review permits use; otherwise, avoid entirely.
  • No Source Maps: Minified output is opaque. Never serve minified files in development; use original sources during debugging.
  • Whitespace Edge Cases: Unusual line endings (\r\n, mixed), comments with syntax-like text (e.g., // like if(true)``), or template literals may break minification. Normalize input sources first.
  • No Active Maintenance: With only 27 stars and minimal commits since ~2017, bug fixes or PHP 8.x patches are unlikely. Fork if critical issues arise, but expect heavy maintenance burden.
  • Laravel-Specific Tip: Avoid middleware-based minification for performance; pre-minify assets in build pipelines instead. If runtime minification is unavoidable, wrap in a robust caching layer.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4