I'm writing an app that uses many JS files. Underscore, Backbone, jQuery, jQuery plugins for sliders, several files for models, routers, collections and views.
In my dev machine, I load every file separately, but in production I use only one JS file (minified, gziped, less http req, etc.).
In my build process, each file in minified with UglifyJS and then concat into prod.js. Is this the correct way to build that file? Or should I concat each file into prod.js and then minify with UglifyJS?
Thanks a lot!
Minification is just removing unnecesary whitespace and redundant / optional tokens like curlys and semicolons, and can be reversed by using a linter. Uglification is the act of transforming the code into an "unreadable" form, that is, renaming variables/functions to hide the original intent...
Minifying is just removing unnecessary white-space and redundant like comments and semicolons. And it can be reversed back when needed. Uglifying is transforming the code into an "unreadable" form by changing variable names, function names, etc, to hide the original content.
To 'uglify' a JavaScript file is to minify it using Uglify. Uglification improves performance while reducing readability.
UglifyJS. The goal of UglifyJS is to minify and compress your code.
I tested the output of each method using Gulp.
I used 9 JavaScript files totaling 19.15 kB when concatenated (not minified). Each file starts with a 'use strict';
statement.
'use strict';
statements'use strict';
statements'use strict';
statement is 13 bytes. 8 × 13 bytes = 104 bytes, which accounts for the 0.1 kB difference.Use whichever order you prefer.
The difference between these two processes is negligible. Concatenate => Uglify could (theoretically) produce (barely noticeably) smaller files if both of the following are true:
'use strict';
statementHere's the gulpfile.js
I used:
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
var files = [
'!app/scripts/**/*Spec.js', // Exclude test files
'app/scripts/**/*.js'
];
// Register tasks
gulp.task('concat-min', function() {
return gulp.src(files)
.pipe(concat('script.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
gulp.task('min-concat', function() {
return gulp.src(files)
.pipe(uglify())
.pipe(concat('script.min.js'))
.pipe(gulp.dest('dist'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With