patchwork/jsqueeze
PHP JavaScript minifier that removes whitespace/comments, mangles local vars, and preserves important/conditional comments. Works on semicolon-free, parse-error-free JS; compression comparable to YUI Compressor/UglifyJS. Single class, Composer install.
Install via Composer: composer require patchwork/jsqueeze. The package is a single PHP class—no config or service provider needed. Begin by creating a minimal minification script or route: instantiate Patchwork\JSqueeze, then call squeeze($jsString, true, true) to minify inline JS. The first practical use case is minifying static JS snippets in legacy PHP views or dynamic asset responses. Verify input is syntactically valid JS (e.g., no syntax errors like unmatched braces), as JSqueeze will throw Exception otherwise. Start with $singleLine = true and $keepImportantComments = true to preserve /*! ... */ license headers and get predictable output.
JSqueeze as a singleton in a service provider (App::singleton(JSqueeze::class)), then inject it into controllers or queueable jobs for on-demand minification (e.g., in a MinifyJs job).php artisan minify:js) that reads all .js files from resources/assets/, runs them through JSqueeze, and writes minified output to public/build/. Batch process files to avoid repeated instantiation overhead.Content-Type: application/javascript.JSqueeze::SPECIAL_VAR_PACKER only when your codebase explicitly prefixes global variables (e.g., _config, $api) to prevent naming collisions—never enable globally unless you control all JS dependencies.⚠️ Critical Pitfalls:
$specialVarRx = true) breaks third-party libraries unless their globals follow your naming convention (e.g., prefixed with _/$).eval(), with, or dynamic property access—JSqueeze’s variable mangling relies on static analysis and fails unpredictably with these.⚙️ Debugging & Quirks:
$singleLine = false during development to retain line breaks (JSqueeze converts optional semicolons to \n for readability).;;;) are treated as single-line comments—use ;;; // comment for intentional comments in minified output.SPECIAL_VAR_PACKER only matches names starting with _, $, __, $$, etc.—it does not support regex patterns.🚀 Pro Tips:
CachedMinifier class: hash the input JS, cache the minified result (e.g., using Laravel’s cache), and skip redundant minification.Storage facade to minify files before deployment: Storage::put('dist/app.min.js', (new JSqueeze)->squeeze(Storage::get('src/app.js')));try {} catch (e) { ... }—JSqueeze fixes IE≤8 issues but may conflict with non-standard error handling.How can I help you explore Laravel packages today?