For JavaScript files we also want to uglify them. Uglifying JavaScript involves changing variable and function names to reduce their size. So a variable named customer might be renamed to x. JavaScript engines don’t care about descriptive names, only developers. So how do we uglify JavaScript files with gulp?
I know what you are going to say: “Blah, blah, blah… there is a plugin.” and you are correct.
While we are uglifying the file, we will also concat all our JavaScript files together and move them to build/javascripts.
gulpfile.js
12345678910111213141516
...varuglify=require('gulp-uglify');...gulp.task('javascript',function(){console.log("Validate, Concat, Uglify, and Move all the javascript files");returngulp.src("contents/javascripts/**.js").pipe(jsValidate()).on("error",notify.onError(function(error){returnerror.message;})).pipe(uglify()).pipe(concat('main.js')).pipe(gulp.dest('build/javascripts'));});
When you run our gulp javascript task now, we should see that our javascript files were uglified, concated, and moved to the build folder.
In the previous article, we used gulp to validate our JavaScript. The error message would appear in the console. While this is awesome, there is a chance we could miss it.
Let’s use notifications to display a pop up window when we have a JavaScript error.
We will now create a new Javascript task in our gulpfile.js. At first, all we will do is validate our Javascript in a new /contents/javascripts folder.
Sweet codes! Gulp can now check if our JavaScript is valid. But the error message in the console is rather bland, lets find a better way to tell us that we messed up.
_dirname is a helper that returns the absolute path of the code file being run.
Node reads files asynchronously. This is normally where we could dive into what “non-blocking I/O” means vs threads, etc. This is a guide about gulp though so I will keep this detour basic.
For our purposes, this means that we have to listen to events from the stream to be able to read the file.
The events we are going to listen to are data and end.
data fires when a chunk of the file has been read and returned. This chunk is not always the entire file. In fact, you should assume it is not the entire file.
Now since we have our css in a single file, we can continue to increase the performance of our site by minifying our css.
Minifying is the process of eliminating all the unnecessary formatting in a css file. Human’s need spaces and tabs (also known as white space) to more easily read the styles. A browser doesn’t care about white space so we can make the file smaller by removing them.
You should start seeing a pattern when using gulp… because for this we need to use a plugin:
vargulp=require('gulp');varconcat=require('gulp-concat');varclean=require('gulp-rimraf');varcssmin=require("gulp-minify-css");gulp.task('clean',[],function(){console.log("Clean all files in build folder");returngulp.src("build/*",{read:false}).pipe(clean());});gulp.task('default',['clean'],function(){console.log("Concat, move, and minify all the css files in styles folder");returngulp.src("contents/styles/**.css").pipe(concat('main.min.css')).pipe(cssmin()).pipe(gulp.dest('build/styles'));});
This task used to be handled by gulp-clean but has been replaced by gulp-rimraf.
Instead of adjusting our default task, lets create a new task to clean out the directory.
gulpfile.js
123456789101112131415
vargulp=require('gulp');varconcat=require('gulp-concat');varclean=require('gulp-rimraf');gulp.task('clean',[],function(){console.log("Clean all files in build folder");returngulp.src("build/*",{read:false}).pipe(clean());});gulp.task('default',['clean'],function(){console.log("Concating and moving all the css files in styles folder");returngulp.src("contents/styles/**.css").pipe(concat('main.css')).pipe(gulp.dest('build/styles'));});
So like before, we need to require gulp-rimraf.
This time though, we created a new task called clean. We tell this task to look at all the files in the build folder and then pipe them to our clean operation. This will delete the files.
You might notice that in our options we pass in { read: false }. This tells the task to not read the contents of the files it is deleting. It is an easy performance gain.
To run our clean task from the command line, we just tell gulp which task to run:
What we would like is to run our clean task before we run our default task. That way our build folder will be nice and empty before we starting moving files there.
You might have been wondering what the empty array ([]) was before our function. This is where we specify dependency tasks.
A dependency task is a task that needs to be completed before gulp can run the current task.
So for our scenario, our clean task is a dependency for default.
Printing Hello and moving files is rather boring. Let’s do something productive.
When we create websites, we are always trying to deliver the best experience possible.
This includes having our webpages displaying fast. Back in the day, this meant having
all our styles in one css file.
While this made our webpages load faster, it made maintaining the css file a night-mare!
These days we can use multiple css files for better organization and then concat (meaning merge or combine) the files together into one large file.
Right now, we have two separate css files in our build/styles folder. We are going to use a gulp plugin to concat all our css files in the styles folder.
Gulp contains some basic tasks, but the power of gulp is the customization you can bring into your build process by using plugins.
To concat the files together, we will need to install one of these plugins.
1
npminstallgulp-concat--save-dev
We can then update our default gulp task to concat the files.
gulpfile.js
123456789
vargulp=require('gulp');varconcat=require('gulp-concat');gulp.task('default',[],function(){console.log("Concating and moving all the css files in styles folder");gulp.src("contents/styles/**.css").pipe(concat('main.css')).pipe(gulp.dest('build/styles'));});
Couple of things have changed, can you spot them? First, we had to reference the gulp plugin with:
1
varconcat=require('gulp-concat');
We chose to label this concat. Obviously we could call it anything we want, but concat communicates what the plugin does to those reading our build script.
Second, we added another step to our task. In between the src and the pipe(gulp.dest...) steps, we added pipe(concat(...)).
Gulp works by streaming files from one process to another. This allows us to create complex build tasks out of small, simple steps. Composition == winning.