0

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?

Aónio
  • 579
  • 4
  • 13
  • Possible duplicate of http://stackoverflow.com/questions/11000261/how-to-minify-js-in-php-easily-or-something-else – Adam Copley Mar 23 '17 at 11:14

2 Answers2

0

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.

Adam Copley
  • 1,495
  • 1
  • 13
  • 31
  • That minifier is not the solution because it's designed to minify on the fly, and the OP simply wants something to minify the JS from unix command line (saving time to the client). Furthermore the file has PHP code embedded everywhere within the JS code, and the file is downloaded and the PHP code is thus run upon an AJAX call which parses a variable on the URL, namely `$_GET['f']`. – João Pimentel Ferreira Mar 23 '17 at 18:59
  • Please tell me where in the question it states that the OP wants to compile on unix command line? Based on what the OP has said about wanting to have the php code maintained. I think minifying the code and serving it on the fly is the only solution. Nor did he mention that the code would be served upon an ajax call. I don't know where you have got this information? – Adam Copley Mar 24 '17 at 11:19
  • Minifying on the fly increases latency! Although the OP does not refer command line unix, I think it's implicit that the minification, as normally it is done, should be done offline. – João Pimentel Ferreira Mar 25 '17 at 22:39
  • The main question is: the time gain achieved by compressing the JS file, overcomes the time span taken to the server to compress the JS file? Obviously it depends either on the server processing capability and on the link between server and client, but I have my doubts. Can you clarify? – João Pimentel Ferreira Mar 25 '17 at 22:44
  • Your reading deeper into what the OP hasn't even specified, let's wait until he adds more info rather than making assumptions – Adam Copley Mar 25 '17 at 22:45
0

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.

Manngo
  • 14,066
  • 10
  • 88
  • 110