-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.js
170 lines (143 loc) · 5.47 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
158
159
160
161
162
163
164
165
166
167
168
169
170
//paths for source and bundled parts of app
var basePaths = {
src: 'src/',
dest: 'assets/',
npm: 'node_modules/',
bower: 'bower_components/'
};
//require plugins
var gulp = require('gulp');
var es = require('event-stream'),
gutil = require('gulp-util'),
bourbon = require('node-bourbon'),
path = require('relative-path'),
runSequence = require('run-sequence'),
importCss = require('gulp-import-css'),
del = require('del');
//plugins - load gulp-* plugins without direct calls
var plugins = require("gulp-load-plugins")({
pattern: ['gulp-*', 'gulp.*'],
replaceString: /\bgulp[\-.]/
});
//env - call gulp --prod to go into production mode
var sassStyle = 'expanded'; // SASS syntax
var sourceMap = true; //wheter to build source maps
var isProduction = false; //mode flag
if(gutil.env.prod === true) {
isProduction = true;
sassStyle = 'compressed';
sourceMap = false;
}
//log
var changeEvent = function(evt) {
gutil.log('File', gutil.colors.cyan(evt.path.replace(new RegExp('/.*(?=/' + basePaths.src + ')/'), '')), 'was', gutil.colors.magenta(evt.type));
};
//js
gulp.task('build-js', function() { //common JS for every page
var vendorFiles = [],
appFiles = [basePaths.src+'js/main-*']; //our own JS files
return gulp.src(vendorFiles.concat(appFiles)) //join them
.pipe(plugins.filter('*.js'))//select only .js ones
.pipe(plugins.concat('bundle.js'))//combine them into bundle.js
.pipe(isProduction ? plugins.uglify() : gutil.noop()) //minification
.pipe(plugins.size()) //print size for log
.on('error', console.log) //log
.pipe(gulp.dest(basePaths.dest+'js')) //write results into file
});
gulp.task('build-admin-css', function() {
var paths = require('node-bourbon').includePaths,
appFiles = gulp.src(basePaths.src+'sass/admin.scss')
.pipe(!isProduction ? plugins.sourcemaps.init() : gutil.noop()) //process the original sources for sourcemap
.pipe(plugins.sass({
outputStyle: sassStyle, //SASS syntas
includePaths: paths //add bourbon + mdl
}).on('error', plugins.sass.logError))//sass own error log
.pipe(plugins.autoprefixer({ //autoprefixer
browsers: ['last 4 versions'],
cascade: false
}))
.pipe(!isProduction ? plugins.sourcemaps.write() : gutil.noop()) //add the map to modified source
.on('error', console.log); //log
return appFiles
.pipe(plugins.concat('admin.css')) //combine into file
.pipe(isProduction ? plugins.cssmin() : gutil.noop()) //minification on production
.pipe(plugins.size()) //display size
.pipe(gulp.dest(basePaths.dest+'css')) //write file
.on('error', console.log); //log
});
//revision
gulp.task('revision-clean', function(){
// clean folder https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
return del([basePaths.dest+'rev/**/*']);
});
gulp.task('revision', function(){
return gulp.src([basePaths.dest+'css/*.css', basePaths.dest+'js/*.js'])
.pipe(plugins.rev())
.pipe(gulp.dest( basePaths.dest+'rev' ))
.pipe(plugins.rev.manifest())
.pipe(gulp.dest(basePaths.dest+'rev')) // write manifest to build dir
.on('error', console.log); //log
});
//builds
gulp.task('full-build', function(callback) {
runSequence('build-admin-css',
'build-js',
'revision-clean',
'revision',
callback);
});
gulp.task('full-build-css', function(callback) {
runSequence('build-admin-css',
'revision-clean',
'revision',
callback);
});
gulp.task('full-build-js', function(callback) {
runSequence('build-js',
'revision-clean',
'revision',
callback);
});
//svg - combine and clear svg assets
gulp.task('svg-opt', function() {
//minification should be before cherio (so it's run twice)
var icons = gulp.src([basePaths.src+'svg/icon-*.svg'])
.pipe(plugins.svgmin({
plugins: [{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}]
})) //minification
.pipe(plugins.cheerio({
run: function ($) { //remove fill from icons
$('[fill]').removeAttr('fill');
$('[fill-rule]').removeAttr('fill-rule');
},
parserOptions: { xmlMode: true }
})),
pics = gulp.src([basePaths.src+'svg/pic-*.svg'])
.pipe(plugins.svgmin({
plugins: [{
removeTitle: true,
removeDesc: { removeAny: true },
removeEditorsNSData: true,
removeComments: true
}]
})); //minification
return es.concat(icons, pics)
.pipe(plugins.svgstore({ inlineSvg: true })) //combine for inline usage
.pipe(gulp.dest(basePaths.dest+'svg'));
});
//watchers
gulp.task('watch', function(){
gulp.watch([basePaths.src+'sass/*.scss', basePaths.src+'sass/**/*.scss'], ['full-build-css']).on('change', function(evt) {
changeEvent(evt);
});
gulp.watch(basePaths.src+'js/*.js', ['full-build-js']).on('change', function(evt) {
changeEvent(evt);
});
});
//default
gulp.task('default', ['full-build', 'watch']);