meenie/javascript-packer
PHP library for packing and minifying JavaScript using Dean Edwards’ Packer algorithm. Compresses JS to reduce size and improve load times, with simple PHP API for integrating into builds or runtime processing in Laravel and other projects.
Installation Add the package via Composer:
composer require meenie/javascript-packer
Require the autoloader in your project (Laravel handles this automatically if using composer.json).
First Use Case Pack a single JavaScript file:
use Meenie\JavaScriptPacker\Packer;
$packer = new Packer();
$packedCode = $packer->pack('path/to/your/script.js');
file_put_contents('path/to/packed/script.min.js', $packedCode);
Laravel Integration
For Laravel, publish the config (if available) and bind the packer to the container in AppServiceProvider:
$this->app->singleton(Packer::class, function ($app) {
return new Packer($app['config']['javascript-packer']);
});
Asset Optimization Pipeline
Use the packer in a Laravel Asset pipeline (e.g., app/Providers/AppServiceProvider):
public function boot()
{
$this->app->afterResolving('view', function () {
$packer = app(Packer::class);
$packed = $packer->pack(public_path('js/app.js'));
file_put_contents(public_path('js/app.min.js'), $packed);
});
}
Dynamic Packing in Controllers Pack and serve JS on-demand (e.g., for user-specific scripts):
public function getCustomScript()
{
$packer = app(Packer::class);
$packed = $packer->pack(storage_path('scripts/custom.js'));
return response($packed, 200)->header('Content-Type', 'application/javascript');
}
Combining Multiple Files Pack an array of files into one:
$packer = new Packer();
$packed = $packer->packMultiple([
'file1.js',
'file2.js',
'vendor/script.js'
]);
$filename = 'js/app.min.js?v=' . md5($packed);
file_put_contents(public_path($filename), $packed);
pack:js job) for large projects:
PackScriptJob::dispatch('path/to/script.js')->onQueue('packing');
Encoding Issues
Ensure input files are UTF-8 encoded. The packer may corrupt non-UTF-8 files (e.g., legacy scripts with ISO-8859-1).
Fix: Normalize files before packing:
$content = file_get_contents($file);
$content = mb_convert_encoding($content, 'UTF-8', 'auto');
Line Breaks in Output The packer strips whitespace by default. To preserve formatting (e.g., for source maps), use:
$packer = new Packer(['preserve_formatting' => true]);
Variable Name Collisions
The packer shortens variable names (e.g., var x = 1; → var a=1;). This can break:
window.jQuery).
Fix: Exclude files/libraries from packing or use the exclude_vars option:$packer = new Packer(['exclude_vars' => ['jQuery', 'window']]);
Memory Limits
Packing large files (>10MB) may hit PHP’s memory_limit. Increase it temporarily:
ini_set('memory_limit', '256M');
$packer->pack('large-file.js');
$packer = new Packer(['debug' => true]);
node -e "require('vm').createScript(require('fs').readFileSync('packed.js'));"
Custom Packer Classes
Extend the Packer class to add pre/post-processing:
class CustomPacker extends Packer {
protected function beforePack($code) {
return str_replace('// DEBUG', '', $code); // Remove debug comments
}
}
Hooks for External Tools Integrate with tools like UglifyJS or Terser by wrapping the packer:
$packer = new Packer();
$packed = $packer->pack($file);
$minified = shell_exec("uglifyjs -c -m {$packed}");
Configuration Overrides
Override default settings per environment (e.g., config/javascript-packer.php):
'production' => [
'compression_level' => 9,
'exclude_vars' => ['React', 'Vue'],
],
How can I help you explore Laravel packages today?