Asked 7 years ago
22 Oct 2016
Views 3011
sachin

sachin posted

how to minify JavaScript on fly in php ?

How to minify JavaScript on fly in php . i had many dynamic custom JavaScript and used some open source. dynamic javascript should be minified every time on the fly .so suggest some code
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));


=> JSMin
https://github.com/archirayan-infotech/JSMin

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


shyam

shyam
answered Nov 30 '-1 00:00


1. https://github.com/mrclay/jsmin_minify


include("JSMin.php")
$javascriptCode = JSMin::minify($javascriptCode);


2.
https://github.com/promatik/PHP-JS-CSS-Minifier

download minifier.php

and use it like this

include_once("minifier.php");

$js = array(
    "js/application.js"     => "js/application.min.js",
    "js/main.js"            => "js/main.min.js"
); 

minifyJS($js);
 


Not advisable because it use web api https://javascript-minifier.com/raw to shrink js which is slow for web page speed.


ravi

ravi
answered Nov 30 '-1 00:00

use YaboJsCss
it compress and minify js/css


https://github.com/jujudellago/YaboJsCss

Post Answer