You can use the PHP-JS-CSS-Minifier, it is a PHP library that compresses javascript and css files in real time and saves them to a project folder.
It relies on the site’s API Javascript Minifier. Just include the codes as in the example:
include_once("minifier.php");
$js = array(
"js/application.js" => "js/application.min.js",
"js/main.js" => "js/main.min.js"
);
$css = array(
"css/application.css" => "css/application.min.css",
"css/main.css" => "css/main.min.css"
);
minifyJS($js);
minifyCSS($css);
I tested with the jQuery plugin through their url https://code.jquery.com/jquery-3.2.1.js
and worked as expected:
include_once("minifier.php");
$js = array(
"https://code.jquery.com/jquery-3.2.1.js" => "js/jquery-3.2.1.min.js"
);
minifyJS($js);
The downside is that as compression is done in real time, the request becomes slow and it turns out to be no advantage to put directly into the body of the page of your site, since every time it will perform this compression. An exit would be to insert a if
before you start compressing and compare the date of the two files to check if your script is up to date.
Why can’t you drop them and leave on the project?
– BrTkCa
Because for example, updates, if I download, I will have to download every time it is updated, another thing is that, in this project that I am, I do not have access to the source code, I can only edit html, css, javascript, etc...
– Lucas de Carvalho
I get it. I don’t know (and I believe) that you don’t have to compress external js resources because you’re being a client. Or you would have to call some minificado and/or compacted or download and work it. The ideal is to download, even with the updates, in fact I imagine that at most check once a day.
– BrTkCa
The Javascript Minifier has an API that you can perform compression. It does not help you?
– bio
I believe this guy does more or less what Gulp does @bio, does not minify in download time an external resource.
– BrTkCa
@Lucascosta I think so, this link has an example of how to perform real-time compression with PHP. Below is a library that a user created to do this, tested and worked with jQuery. It compresses in real time and writes the compressed file to a project folder. The only problem I noticed is that the compression was very long.
– bio
Have you tried making asynchronous calls to these files?
– Darlei Fernando Zillmer
Darley, searching, when I make these calls, will it load the JS file only after the site loaded? I didn’t get it very well.
– Lucas de Carvalho