Last Updated: September 29, 2021
·
4.62K
· theatlasroom

Simple gulp starter

Just thought i'd share my simple starter template for gulpjs. The template is used to concat and minify js and css files, remove all console.log statements and then store the build files in my build directory.

Since im still pretty new to the gulp world, I'd love to hear any tips / useful information some of you pros may have picked up along the way.

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var striplog = require('gulp-strip-debug');
var minfycss = require('gulp-minify-css');
var gutil = require('gulp-util');

// My js files
gulp.task('scripts', function() {
  var js_src = 'assets/js/**/*.js'; 
  var js_dest = 'assets/build/js';
  // pipe the js through concat, console log stripping, uglification and then store
  return gulp.src(js_src)
      .pipe(concat('app.min.js')) // concat all files in the src
      .pipe(striplog())
      .pipe(uglify())   // uglify them all
      .pipe(gulp.dest(js_dest)) // save the file
      .on('error', gutil.log); 
});

// My css files
gulp.task('css', function() {
  var css_src = 'assets/css/**/*.css';
  var css_dest = 'assets/build/css';

  // Concat and minify all the css
  return gulp.src(css_src)
      .pipe(concat('app.min.css')) // concat all files in the src
      .pipe(minfycss()) // uglify them all
      .pipe(gulp.dest(css_dest)) // save the file
      .on('error', gutil.log); 
});

// Clean all builds
gulp.task('clean', function() {
  return gulp.src(['assets/build/'], {read: false})
    .pipe(clean());
});

// Default task - clean the build dir
// Then rebuild the js and css files
gulp.task('default', ['clean'], function() {
    gulp.start('css', 'scripts');
});

If you're currently using gruntjs for relatively trivial tasks like this, I strongly recommend giving gulp a try, i've found it to be pretty damn quick and easy to setup.

If you're totally lost, I recommend having a look at Mark Goodyear's article on getting started with gulpjs.

3 Responses
Add your response

Yeah gulp is great. I use it in almost all of my newer projects and am slowly changing some pretty weighty grunt build processes to use gulp. However, this isnt always easy as gulp requires a fundamental shift in how you do some things and this, I think, is what puts people off.

Embracing streaming for data transformations is generally a fantastic idea and gulp is great at what it does, adding concurrency and its pretty elegant. It still has a few issues to iron out but I'd hope gulp is here to stay as its a fantastic way to build JS projects.

over 1 year ago ·

I've recently integrated gulp into the workflow we use in our development team at work and as such I have created a gulp-starter GitHub repo (albeit a little advanced) which might be of interest to you: https://github.com/uofa/gulp-starter

over 1 year ago ·

I have found this one:
https://github.com/wwwebman/gulp-webpack-starter
Really helpful for me. Author use gulp and webpack.

over 1 year ago ·