-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
157 lines (138 loc) · 4.84 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// gulp
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var plumber = require('gulp-plumber');
var livereload = require('gulp-livereload');
var notify = require('gulp-notify');
var runSequence = require('run-sequence');
// javascript (browserify, uglify, jshint, jscs)
var packageJSON = require('./package.json');
var jshintConfig = packageJSON.jshint;
var jscsConfig = packageJSON.jscs;
var stylish = require('jshint-stylish');
var jshint = require('gulp-jshint');
var jscs = require('gulp-jscs');
var through2 = require('through2');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
// images
var imagemin = require('gulp-imagemin');
// project
var david = require('gulp-david');
var todo = require('gulp-todo');
// css
var postcss = require('gulp-postcss');
var cssnext = require('cssnext');
var normalize = require('postcss-normalize');
var assets = require('postcss-assets');
var nested = require('postcss-nested');
var mixins = require('postcss-mixins');
var extend = require('postcss-extend');
var postcssImport = require('postcss-import');
var minify = require('gulp-minify-css');
var bemLinter = require('postcss-bem-linter');
var reporter = require('postcss-reporter');
var pxtorem = require('postcss-pxtorem');
// postcss compailer with plugins
gulp.task('css', function () {
var processors = [
normalize,
postcssImport({
plugins: [
mixins(),
nested(),
bemLinter(),
reporter({ throwError: true })
]
}),
cssnext({ browsers: 'last 2 version, > 1%, ie > 8', url: false }),
assets({ basePath: 'client/', relativeTo: 'client/css', loadPaths: ['img'], cachebuster: true }),
extend(),
pxtorem()
];
return gulp.src('client/css/style.css')
.pipe(plumber({errorHandler: notify.onError('CSS error: <%= error.message %>')}))
.pipe(sourcemaps.init())
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('assets/css/'))
.pipe(livereload());
});
// lint javascript files using jshint
gulp.task('jshint', function () {
return gulp.src(['client/js/**/*.js', '!client/js/vendor/**/*.js'])
.pipe(jshint(jshintConfig))
.pipe(jshint.reporter(stylish))
.pipe(jshint.reporter('fail'));
});
// check javascript code style via jscs
gulp.task('jscs', function () {
return gulp.src(['client/js/**/*.js', '!client/js/vendor/**/*.js'])
.pipe(jscs(jscsConfig));
});
// compile javascript files via browserify
gulp.task('browserify', ['jshint'], function () {
var browserified = function (file, enc, next) {
browserify({
entries: file.path,
debug: true
}).bundle(function (err, res) {
file.contents = (typeof res !== 'undefined') ? res : null;
next(err, file);
});
};
return gulp.src(['client/js/bundles/*.js'])
.pipe(plumber({errorHandler: notify.onError('Browserify error: <%= error.message %>')}))
.pipe(sourcemaps.init())
.pipe(through2.obj(browserified))
.pipe(sourcemaps.write())
.pipe(gulp.dest('assets/js/'))
.pipe(livereload());
});
// optimize images using imagemin
gulp.task('images', function () {
return gulp.src('client/img/**/*')
.pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
.pipe(gulp.dest('assets/img'));
});
// check package.json for outdated dependencies
gulp.task('david', function () {
return gulp.src('./package.json')
.pipe(david({ error404: true, errorDepType: true }))
.pipe(david.reporter);
});
// update package.json with new versions of dependencies
gulp.task('update', function () {
return gulp.src('./package.json')
.pipe(david({ update: true }))
.pipe(david.reporter)
.pipe(gulp.dest('./'));
});
// get all todos from code
gulp.task('todos', function () {
return gulp.src(['client/js/**/*.js'])
.pipe(todo())
.pipe(gulp.dest('./'));
});
// minify css
gulp.task('minify', ['css'], function () {
return gulp.src('assets/css/*.css')
.pipe(minify({ keepSpecialComments: 0 }))
.pipe(gulp.dest('assets/css/'));
});
// uglify javascript files
gulp.task('uglify', ['browserify'], function () {
return gulp.src('assets/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('assets/js/'));
});
gulp.task('watch', ['css', 'browserify'], function () {
livereload.listen();
gulp.watch('client/css/**/*.css', ['css']);
gulp.watch(['client/js/**/*.js'], ['jshint', 'browserify']);
});
gulp.task('default', ['css', 'jshint', 'browserify']);
// build (images are optimized before they are inlined into CSS)
gulp.task('build', function() {
runSequence('images', 'minify', 'uglify', function() {});
});