After long search in Google and on several Github repositories, I couldn't find anything solid (I found for HTML, not for JS)
The minifiers I use for JS files mess with my PHP code, for example, if I have the file example.js.php
<?php header("Content-Type: application/javascript");
$var1 = $_GET['f'];
?>
//some JS and PHP code
For example, the JS minifiers I use, convert $_GET['f']
to $_GET.f
; which is OK for a JS object, but not for PHP.
Any ideas on how to compact the JS code leaving the PHP intact?
More up to date than the answers in the duplicate question. You could use JShrink
You could create your javascript and store it all in a single string, then pass this string into the library like in the example on the JSrink page.
Minifiers are trying to make nearly impossible decisions, and embedding them in PHP is really stretching the friendship.
You should minify the JavaScript separately, and then included it or send it through the PHP.
Two possibilities are:
<?php
header('Content-Type: application/javascript');
require_once 'script.min.js';
?>
if that is appropriate, or
<?php
header('Content-Type: application/javascript');
print file_get_contents('script.min.js');
?>
depending on why you’re trying to mix them.