Asked 8 years ago
17 Oct 2016
Views 1242
debugger

debugger posted

how to Minify JavaScript in php ?

i am testing page speed with google PageSpeed Insights , and i did not find good score ,

google PageSpeed Insights suggest me to Minify JavaScript
Compacting JavaScript code can save many bytes of data and speed up downloading, parsing, and execution time.

so now i want JavaScript to be Minified .but question is now how to Minify JavaScript in php ? so google PageSpeed Insights score goes up

so pls give me some suggestion
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

there are many php script out there which help you to minify JavaScript

some Javascript Minifier what i used

=> JShrink
i try to use https://github.com/tedivm/JShrink but i get some error with php so i had changed it
you can get at
https://github.com/archirayan-infotech/minify-js

this way you can use it dynamically . it will take js file and minify on fly and use it direct on the page


$js=file_get_contents("dynamic.js");

include('Minifier.php');

// Basic (default) usage.
$minifiedCode = Minifier::minify($js);

// Disable YUI style comment preservation.
$minifiedCode = Minifier::minify($js, array('flaggedComments' => false));


2. JSMin
https://github.com/archirayan-infotech/JSMin

$js=file_get_contents("dynamic.js");
include("Jsmin.php");
$minified=JSMin::minify($js);
 


Post Answer