linkorb/jsmin-php
PHP port of Douglas Crockford’s JSMin for minifying JavaScript. Provides a simple API to strip comments and whitespace, shrinking scripts for faster delivery. Lightweight, dependency-free and easy to integrate into Laravel or any PHP build/deploy workflow.
Installation:
composer require linkorb/jsmin-php
Add to composer.json if not auto-discovered:
"autoload": {
"psr-4": {
"App\\": "app/",
"JsMin\\": "vendor/linkorb/jsmin-php/src/"
}
}
Run composer dump-autoload.
First Use Case: Minify a JavaScript string in a Blade template or controller:
use JsMin\JSMin;
$minified = JSMin::minify($yourJsString);
Quick Example:
$js = 'function foo() { return "bar"; }';
echo JSMin::minify($js); // Outputs: function foo(){return"bar";}
src/JSMin.php (core logic).tests/JSMinTest.php (edge cases).Blade Directives: Create a custom Blade directive for minification:
// app/Providers/BladeServiceProvider.php
Blade::directive('minify', function ($expression) {
return "<?php echo JsMin\\JSMin::minify({$expression}); ?>";
});
Usage in Blade:
<script>
@minify($jsString)
</script>
Middleware for Asset Optimization: Minify JS in a middleware before sending responses:
public function handle($request, Closure $next) {
$response = $next($request);
if ($response->headers->get('Content-Type') === 'application/javascript') {
$content = $response->getContent();
$response->setContent(JsMin::minify($content));
}
return $response;
}
Service Integration: Wrap JSMin in a service class for reusability:
class JsMinifier {
public static function minify(string $js): string {
return JSMin::minify($js);
}
}
$cacheKey = 'js_minified_' . md5($js);
$minified = Cache::remember($cacheKey, 3600, function() use ($js) {
return JSMin::minify($js);
});
if (!is_string($js)) {
throw new \InvalidArgumentException('Input must be a string.');
}
Performance Overhead:
Edge Cases:
// License header
function foo() { /* comment */ return "bar"; }
→ Becomes: function foo(){return"bar";} (license/comments lost).uglify-js with --source-map for debugging.Non-Standard JS:
Output Mismatches:
uglify-js). Test thoroughly in your stack.try {
$minified = JSMin::minify($js);
} catch (\Exception $e) {
Log::error("JSMin failed: " . $e->getMessage());
return $js; // Fallback to unminified
}
Custom Rules:
Modify the minifier’s logic in src/JSMin.php (e.g., preserve specific comments):
// Override the minify method or extend the class
class CustomJSMin extends JSMin {
protected function shouldRemoveComment($comment) {
return !str_contains($comment, 'PRESERVE');
}
}
Pre/Post-Processing:
Chain with other tools (e.g., uglify-js via PHP bindings):
$minified = JSMin::minify($js);
$uglified = UglifyJS::minify($minified);
Configuration: The package lacks config options, but you can inject settings via dependency injection:
JSMin::setOption('preserveComments', true); // Hypothetical; check source
$minified = JSMin::minify($js);
$jsToUse = app()->environment('production') ? $minified : $js;
//, /* */, /\n/).How can I help you explore Laravel packages today?