0

Since I've heard people whining how PHP can't handle it, how about minifying JavaScript with JavaScript itself? I want something that I can just copy and paste and use right away--my JS file is about 17KB and I know I can reduce it to about 15KB easily.

And I want to be able to do it right on the server. I don't want to have to go to a website such as jscompress.com every time I make changes to my script.

Again, I would like something like this:

<script type="text/javascript" src="scripts.php?file_to_minify=scripts_template.js"></script>

And then I would just let JS minify it on the PHP file.

Now, I do have second thoughts...Would this even work???

Your help is appreciated. Thanks!

RickyAYoder
  • 963
  • 1
  • 13
  • 29
  • 2
    I'm confused.. you can do what you propose in your code sample.. but that would be using PHP to minify the script. However didn't you say you don't want to use PHP? – Brian Glaz Jul 08 '12 at 03:23
  • Say I have a function in JS on the scripts.php page called "minify_js". What I would do is simply this minify_js( echo(file_get_contents($_GET['file_to_minify'])); ?>); I'm not sure if this would work though, which is why I'm asking if it would. – RickyAYoder Jul 08 '12 at 03:26
  • 2
    @RickyAYoder - What could you possibly gain by sending unminified code to the browser, and then minifying it on the client side??? – Joseph Silber Jul 08 '12 at 03:27
  • Reducing 17K to 15K is most likely ... pointless. In any case, this is what HTTP deflate compression (and caching) is for. –  Jul 08 '12 at 03:27
  • You should just look for a tool that watches for when you make changes to your JS source directory and runs a minifier, writing the output to a minified directory. I know they exist, although I haven't needed them myself as such is part of a separate build process for me. – Scott Sauyet Jul 08 '12 at 03:33
  • Well, my goal is to have something automatic. I have another post that didn't get me what I was looking for exactly... Check the following link to read more about what I'm looking for: something that's easy, something that updates itself as I go, and something that uses PHP or JS. http://stackoverflow.com/questions/11000261/how-to-minify-js-in-php-easily-or-something-else/11004660#11004660 – RickyAYoder Jul 08 '12 at 03:34
  • @Scott Sauyet that's sounds like EXACTLY what I want, but I don't know where to find one. – RickyAYoder Jul 08 '12 at 03:41
  • @RickyAYoder - I don't either exactly as I've never had the need, but I know that tools like LESS and SASS often incorporate such watchers in them. In fact, those would probably work directly without using any of the other features of LESS/SASS, although that might be a bit of overkill. – Scott Sauyet Jul 08 '12 at 13:07

1 Answers1

1

Personally, I would look to have a local version of my site, with all source/configuration files under version control. You could then have a release script that would prepare your files for upload, including minifying your javascript using a command line tool.

This ensures that you don't lose anything if you make a mistake, also it makes build of your site repeatable. This should be requirement for a production site, and is useful for a personal/hobby site (no-one likes losing changes/breaking things with no way back).

Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
Romski
  • 1,912
  • 1
  • 12
  • 27