Minify/Combining JS Files - javascript

I have around 6-7 javascript files that I want to minify and combine. I have been minifying my javascript here: http://www.minifyjavascript.com/
I am having a little difficulty "combining" my javascript. By combine, am I just copy and pasting all the javascript to one huge js file? If I do this, I will end up with around 1000 lines of code and I have to figure out exact dependencies. Is there an easy program online that can do this for me? Also, do I have to combine scripts such as jquery with my scripts?
Any help would be appreciated.

There is the Closure Compiler from Google, that will combine JS files and minify them to a level of your choosing. It certainly saves a great deal of playing about with cutting & pasting which, as you probably know, can get very complicated.

I just use Notepad++ and the JSMin plugin. I would just copy them in the order in which you include them in your html files.
And no, I would not combine jsquery plugin with it. Keep them separate so you can update independently of them.

Check out PackScript.
http://danderson00.blogspot.com.au/2013/01/packscript-next-generation-build-for-web.html
It does all you need and much more.

Check this python script.
http://github.com/hkasera/minify
It minifies js as well as css files too. It stores detailed log files and you can add this script as a git hook and save yourself from doing it manually everytime.
Hope it may help!

Related

Javascript mass minification and concat from a folder

Is there some simple way to minify and concatenate bunch of JS files in the same folder + folders in the folder? I only want that, nothing else.
Tried HTML5 Boilerplate ant build script. It is way too complex for this job, though it works somehow. I couldn't figure out how to skip the images optimization part, even when specifying images.default.bypass value in project.properties.
So I need some script to run from a console, so it would minify and concatenate recursively all JS files it can find.
You could use the Microsoft solution http://ajaxmin.codeplex.com/
This can minify CSS and JS. It also has the 'wildcard' option.
Sounds like a job for a Makefile. Most recent versions of make have a built-in wildcard facility like you're asking for. Go with GNU make if you don't know anything about this tool, since its very widely available and well-known.

How do I compress tinymce and all plugins into a static file?

The documentation for tinymce notes that one can compress all the javascript and components (which I assume includes plugins) into a single file. They do note reasons why one might not want to that as well.
Compressing into a static file
It's also possible to simply concatenate the necessary components and some boilerplate code into a single .js file. However you will always have to recreate this file if you want to use other TinyMCE plugins, or you upgrade TinyMCE. You will also probably want to configure your webserver to compress javascript files.
But assuming one actually did want to do it, how does one actually go about it? Build.xml does does not provide an appropriate task it seems. At least when I tried it the plugins did not seem to be included when I loaded tiny_mce.js.
There are some really excellent command line tools for this, but you can also do this easily with just a text editor. The simplest way is to just open each file, copy the contents, and paste the contents into a single JS file ("everything-all-together.js", say). You'll need to make sure you paste the files into the single file in the same order you would've put the script tags into the HTML doc. Once you have all the files all together, you can use tools like JSXMin, YUI Compressor, or Google Closure. There are also some tools online that do this, like http://www.minifyjavascript.com/. You can paste in the uncompressed JS and copy back out the compressed JS. This makes the build process really cumbersome, but if you just need to do this once, that will get you there.
The best way to do this is to do it as a build step for the site. That means when you make changes to the JS files, you rebuild the compressed JS file to include the changes as well. This can be a cumbersome step if you're iterating quickly and changing files over and over again. You don't want to have to rebuild the compressed file with each save. You can solve this by setting up development and production modes of the site. When being loaded in development mode, the JS files aren't grouped together. When all the necessary changes are made, you'd rerun the build step to generate the single compressed JS file. To do the minification from the command line, you'd probably want to use Google Closure: https://developers.google.com/closure/compiler/. If you download the compiler app, you can do the following:
java -jar compiler.jar some-file.js some-other-file.js > compiled.js
That will generate a file called compiled.js that includes the contents of some-file.js and some-other-file.js in a minified format. You can specify as many files to compile as you need to. Actually, I'm selling Closure a bit short to say it's just minified. It's also extremely optimized code. Pretty much every site should be doing this to all of there JS all the time unless they're already doing something better.
I hope I'm getting you (and the tinymce docs) right, but this sounds a lot like combining JavaScript files on the server side. This means taking the contents of all of your JS files, putting them into one file and returning that one to the client.
Why would you do that? Well, this should be obvious, but.. you reduce the number of HTTP requests to your server, which is always a good thing.
How do you do that? There are many solutions out there for all server-side languages and frameworks, I suggest doing a Google search for "[your language] javascript minifier" or something similar.
Hope this helps.

Minifying JS/CSS files

I have a template that has over 20 js/css files that it references to and of course this makes for a lot of http requests. I thought about stitching them together with php using minijs.php/mini.php but the problem Im seeing is the page seems to load slower when using minijs.php/mini.php. I used YSlow and just having each one linked individually it shows 3 seconds to load, when I use the minified solution it takes 7-10 seconds to load, even when cached. Does anyone recommend a better solution or do you even recommend combining them all together dynamically like this?
Yes, I do recommend combining the files and minifying them, ideally using either YUI Compressor, Google Closure Compiler, or, what jQuery recently switched to, UglifyJS.
As for a bit of how and why, read this and search Google for "why should I combine web site assets?".
Also bear in mind that this should be a preprocessing step, or at the very least something that is rendered once and cached and thereafter served by a static file server (Apache or Nginx, php should not be involved).
Check this python script.
http://github.com/hkasera/minify
It minifies js as well as css files too. It stores detailed log files and you can add this script as a git hook and save yourself from doing it manually everytime. I have created it during a project and it helped me a lot.
Hope it may help!
you can use many online tools which have features for compressing and combining multiple files.
for javascript compression you can use jsCompressor
and for css you can use CSS compressor CSSCompressor

how to group jQuery plugins together?

In css you can import a stylesheet in another stylesheet. How do you do this for javascript files? most specialy jquery plugins?
I searched first and found different similar questions but I'm not sure about the answer provided. (http://stackoverflow.com/questions/1618351/loading-js-files-dynamically-via-another-js-file)
I'm just looking for a way to limit http requests.
You can just append them one after another in a single file, or better, minify them alltogether, with either the YUI Compressor or Google Closure compiler....both will combine all files passed in and minify them as well. Then, you just serve that one file to your users that contains all your file you want to combine.
You can look at google's javascript compiler, closure.
I've been using it in my Rails projects for some time now, through a tool called Jammit, and it does do wonders compressing JS to ridiculous sizes.

How should I start a new JavaScript project (Testing, Developing, Building)?

I've developing JavaScript since many years but have never really thought about the whole testing, developing and building stuff - but I realized it's pretty much necessary. We've just used a Subversion repository with simple release tagging (switching to git soon). As a new bigger pure JavaScript project (using jQuery) is arriving soon, I'd like to optimize the whole process.
I already did some research but never found a good starting tutorial.
It's definetly a good idea to split classes and separate code blocks into several js-files and not a big one (as Prototype or jQuery do it). These js-files must be "build" into a single file. How do I achieve that?
It's pretty much necessary to Unit-test the stuff me and my colleagues are coding. I found the js-test-driver which has an eclipse plugin that seems to be doing his job quite good. If my developer-folder contains all these src- and src-test-files, how do I integrate this in the building process?
For testing, take a look at this: https://stackoverflow.com/questions/32809/javascript-unit-testing
For merging all of your JavaScript into one file you can use something like YUI Compressor. You need to be looking for a minimizer first, compression second. A minimizer just takes the files and merges them together and gets rid of whitespace. A compressor will actually try to optimize the js for you by changing variable names and removing unnecessary code.
As for unit testing I am unsure of how you will want to do that. There are a few unit test libraries out there. A popular tool for testing is Selenium. I don't currently do unit testing so I am out of my element there..
For setting up your code you could always look at using a JavaScript framework like ExtJS or JavaScriptMVC. Those help you with setting up your code in the proper way and also helps focus your team on the proper standards and coding structure while also writing a lot of the code for you so you don't have to re-invent the wheel.
EDIT: Just a quick after thought. Even if you don't want to use a JavaScript framework, I would suggest checking them out, especially ExtJS, just to see how they organize their code and some of the tricks they do to keep it clean.
I'll answer part of your question:
These js-files must be "build" into a
single file.
This is possible only with server side language - in ASP.NET you have built in tools for that, otherwise build your own "merger" server side file and reference that file instead of the actual .js files.
These js-files must be "build" into a single file. How do I achieve that?
Definitely keep your files separate in version control, and only merge them during the build process.
The YUI compressor mentioned elsewhere is a java-based tool that will not only merge but -- of course! -- compress your files for faster download.
If you just want a simple merge of files, a simple Perl or bash-script (or other preferred scripting language) could concatenate multiple .js files into one for release -- just make sure that the build script also updates all HTML in the release to reference only the single page.

Categories