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

Jsqueeze Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Laravel Integration: Bind 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).
  • Build Pipeline: Create an Artisan command (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.
  • Dynamic Responses: For legacy apps serving JS inline (e.g., admin panels), intercept JS generation, minify before sending headers, and set Content-Type: application/javascript.
  • Safe Global Mangling: Use 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.

Gotchas and Tips

⚠️ Critical Pitfalls:

  • Input must be syntactically valid JS—minified, compressed, or malformed input causes silent failures or exceptions.
  • Global variable renaming ($specialVarRx = true) breaks third-party libraries unless their globals follow your naming convention (e.g., prefixed with _/$).
  • Avoid eval(), with, or dynamic property access—JSqueeze’s variable mangling relies on static analysis and fails unpredictably with these.

⚙️ Debugging & Quirks:

  • Set $singleLine = false during development to retain line breaks (JSqueeze converts optional semicolons to \n for readability).
  • Triple semicolons (;;;) 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:

  • Wrap JSqueeze in a CachedMinifier class: hash the input JS, cache the minified result (e.g., using Laravel’s cache), and skip redundant minification.
  • Use Laravel’s Storage facade to minify files before deployment: Storage::put('dist/app.min.js', (new JSqueeze)->squeeze(Storage::get('src/app.js')));
  • If minification breaks unexpectedly, inspect for try {} catch (e) { ... }—JSqueeze fixes IE≤8 issues but may conflict with non-standard error handling.
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
milesj/emojibase
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