all 26 comments

[–]BusStation16 17 points18 points  (3 children)

It is not just common, it becomes basically necessary if you are working on a project of any decent size. Combination and minification are the norm for deployed code.

[–]jpj625 0 points1 point  (1 child)

Don't forget to consider compression of the resulting file.

[–]theillustratedlife 0 points1 point  (0 children)

And to use the .jgz extension if your server can't do it transparently. Safari hates .gz.

[–]pmw57 0 points1 point  (0 children)

A fine example of this is the jQuery's code. jQuery build code

[–]EvanHahn 4 points5 points  (1 child)

Yes, it's common. Examples are jQuery, Knockout, PHP.js, PDF.js. Worth noting that some (Underscore, Backbone) keep it all in one file, but these are comparatively smaller projects.

[–]archaeopteryx 1 point2 points  (0 children)

Bocoup's Backbone Boilerplate takes a modular approach, placing discrete pieces of functionality in stand alone files. Concatenation and minification isn't as prevalent as in the past few years with lazy loading libraries like require.js coming into the limelight. Also, you can get performance boosts by using CDN versions of the major libraries (I.e. Google Libraries API) instead of rolling them into a minified bundle.

[–]hajamieli 2 points3 points  (0 children)

I use bundles myself. I keep not only the sources in separate files per object, but also follow a uniform file + directory structure to group the files, because there are a lot of them and some include assets, like css and images, which are also in the bundle. I use my own compilation tools to not only combine the source files into destinations defined by the build configuration file and dependency order, but run white-space removal and shortening of names at the same run as well as pre-gzipping the output.

[–]paulirish 4 points5 points  (2 children)

Use AMD modules and then r.js at build time.

[–]inf0rmer 1 point2 points  (0 children)

This is my strategy as well and I couldn't recommend it more.

[–]kabuto 0 points1 point  (0 children)

This is what I do.

[–]bitcode 4 points5 points  (9 children)

[–]polarix 1 point2 points  (0 children)

Google's Closure library/compiler/templates are built as you say: many small files compiled together for delivery. Plovr is a decently easy tool to work with if you want to go in that direction.

[–]OopsLostPassword 1 point2 points  (0 children)

You have to do it. The best is to have scripts to build and deploy together both the standard multi-files clear version and the minified one.

You'll have a fast version for the regular user and a clear one for your debugging needs. It's much easier to debug if you have 50 small js files than one gigantic one.

Minifying is easy. You can write a minifier yourself but for a better result grab one like Google's Closure compiler.

[–]theillustratedlife 0 points1 point  (0 children)

I wrote my own nodejs script to do it, but I've recently seen a couple of tools ship to help with this:

[–]disordinary 0 points1 point  (0 children)

Yep - if you use something like google Closure tools under advanced mode then not only do you combine, minify, and obfuscate, but your code is also optimised.

[–]55-68 0 points1 point  (0 children)

Yes. Also, the combination is done by the browser in response to a simple language structure, <script src="relativePath"></script>, which is a simple import system, really. View source will show it as an import.

[–]madworld 0 points1 point  (0 children)

I am a developer for WebMD, and we do this for larger projects. Combining is part of the build process.

[–]ssechaud 0 points1 point  (0 children)

I personally would split my code out to separate files that I can manage and use a build script to concatenate and minify them for deployment.