Skip to content Skip to sidebar Skip to footer

Minify JS Files Embedded With PHP Code

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

Solution 1:

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.


Solution 2:

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.


Post a Comment for "Minify JS Files Embedded With PHP Code"