forked from sebastianekstrom/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
110 lines (101 loc) · 3.32 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// /**
// * Gulp
// * -----------------------------------------------------------------------------
// */
'use strict';
/* Plugins
* -------------------- */
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var connect = require('gulp-connect');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var buffer = require('vinyl-buffer');
var lost = require('lost');
var postcss = require('gulp-postcss');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var changed = require('gulp-changed');
var eslint = require('gulp-eslint');
/* Task for building the Sass files
* -------------------- */
gulp.task('styles', function() {
var format = argv.production ? 'compressed' : 'expanded';
return gulp.src('source/sass/*.scss')
.pipe(gulpif(!argv.production, sourcemaps.init()))
.pipe(sass({
outputStyle: format
}))
.pipe(postcss([
lost()
]))
.on('error', function(err) {
console.error(err.message);
})
.pipe(autoprefixer({
browsers: ['last 8 versions', 'ie 8', 'ie 9']
}))
.pipe(gulpif(!argv.production, sourcemaps.write('.')))
.pipe(gulp.dest('dist/css/'))
.pipe(connect.reload());
});
/* Browserify
* -------------------- */
gulp.task('scripts', function() {
return browserify('source/js/main.js')
.bundle()
.pipe(source('script.js'))
.pipe(buffer())
.pipe(gulpif(!argv.production, sourcemaps.init({loadMaps: true})))
.pipe(gulpif(!argv.production, sourcemaps.write('./')))
.pipe(gulpif(argv.production, uglify()))
.pipe(gulp.dest('dist/js/'))
.pipe(connect.reload());
});
/* Image compression
* -------------------- */
gulp.task('image-compression', function () {
return gulp.src('source/img/*')
.pipe(changed('dist/img'))
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('dist/img'));
});
/* JS Lint
* -------------------- */
gulp.task('lint', function () {
return gulp.src(['source/js/**/*.js','!source/js/vendor/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/* Local server
* -------------------- */
gulp.task('connect', function() {
connect.server({
livereload: true,
port: 8080
});
});
/*
* `gulp` will build the JS & CSS files and start watching the `src` folder for changes
* `gulp watch` will only start watching the `src` folder for changes
* `gulp build` will only build the JS & CSS files
* `gulp build --production` will build the CSS files for production use (minify etc.)
* -------------------- */
gulp.task('default', ['build', 'watch', 'connect']);
gulp.task('build', ['scripts', 'styles', 'lint']);
gulp.task('watch', function() {
gulp.watch('source/js/**/*.js', ['scripts']);
gulp.watch('source/sass/**/*.scss', ['styles']);
gulp.watch('source/img/*', ['image-compression']);
gulp.watch('source/js/**/*.js', ['lint']);
});