-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
181 lines (154 loc) · 4.67 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
171
172
173
174
175
176
177
178
179
180
181
//
// Define Gulp plugin config options
// --------------------------------------------
// Configure Auto Prefixer (https://github.com/sindresorhus/gulp-autoprefixer / https://github.com/ai/browserslist)
var autoprefixerOptions = {
browsers: ['IE 8', 'IE 9', 'last 2 versions']
};
// Configure Main Bower Files (https://github.com/ck86/main-bower-files)
var mainBowerFilesOptions = require('./bowerconfig.json');
// assign gulp-bless options
var blessOptions = {
imports: true, // import split files rather than outputting various files
suffix: '-chunk' // override the default suffix of '-blessed'
}
//
// Beginning of Gulp processes
// --------------------------------------------
// Include gulp
var gulp = require('gulp');
var es = require('event-stream');
var gutil = require('gulp-util');
// Include plugins
var plugins = require("gulp-load-plugins")({
pattern: ['gulp-*', 'gulp.*', 'main-bower-files'],
replaceString: /\bgulp[\-.]/
});
// Define config file
var config = require('./gulpconfig.json');
// Allows gulp --dev to be run for a more verbose output
var isProduction = true;
var sassStyle = 'compressed';
if (gutil.env.dev === true) {
isProduction = false;
sassStyle = 'expanded';
}
gulp.task('css', loadStyles());
gulp.task('js', loadScripts());
function loadStyles() {
var theme = gutil.env.type;
if (theme && config.styles[theme]) {
// requested theme
config.styles[theme].forEach(function(data) {
watchStyles(data);
});
} else {
// all
for (var theme in config.styles) {
config.styles[theme].forEach(function(data) {
watchStyles(data);
});
}
}
}
function buildStyles(data) {
// start by concatinating list of main bower assets and local app assets
var css = gulp.src(
plugins.mainBowerFiles(mainBowerFilesOptions)
.concat(data.src)
)
// filter to allow only css and scss based file extensions
.pipe(plugins.filter(['*.css', '*.scss']))
// init sourcemaps plugin when not for production
.pipe(isProduction ? gutil.noop() : plugins.sourcemaps.init())
// init plumber for error reporting and stop compilation
.pipe(plugins.plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}
}))
// compile the sass
.pipe(plugins.sass({
outputStyle: sassStyle,
precision: 8
}))
// assign dest(s) from our gulpconfig
.pipe(plugins.concat(data.dest.filename))
// push sass through broser auto-prefixer
.pipe(plugins.autoprefixer(autoprefixerOptions))
// pipe through bless plugin to split large css sheets to
// avoid IE9 file length bug when in production environment
.pipe(isProduction ? plugins.bless(blessOptions) : gutil.noop())
// minify scss when for production
.pipe(isProduction ? plugins.cleanCss({advanced:false}) : gutil.noop())
// write sourcemaps when not in a production environment
.pipe(isProduction ? gutil.noop() : plugins.sourcemaps.write())
// output file sizes
.pipe(plugins.size({showFiles:true,title:'Output'}))
// Output file to each destination in paths
if (typeof data.dest.paths === 'string') {
css.pipe(gulp.dest(data.dest.paths));
} else {
for (var i=0; i<data.dest.paths.length; i++) {
css.pipe(gulp.dest(data.dest.paths[i]));
}
}
// add blank line to aid output readability
gutil.log('');
return;
}
function watchStyles(data) {
gulp.watch(data.watch, ['css']).on('change', function(evt) {
buildStyles(data);
changeEvent(evt, data.src);
});
return;
}
function loadScripts() {
var theme = gutil.env.type;
if (theme && config.scripts[theme]) {
// requested theme
config.scripts[theme].forEach(function(data) {
watchScripts(data);
});
} else {
// all
for (var theme in config.scripts) {
config.scripts[theme].forEach(function(data) {
watchScripts(data);
});
}
}
return;
}
function watchScripts(data) {
gulp.watch(data.watch, ['js']).on('change', function(evt) {
buildScripts(data);
changeEvent(evt, data.src);
});
return;
}
function buildScripts(data) {
var js = gulp.src(plugins.mainBowerFiles(mainBowerFilesOptions).concat(data.src))
.pipe(plugins.filter('*.js'))
.pipe(plugins.concat(data.dest.filename))
.pipe(isProduction ? plugins.uglify() : gutil.noop())
.pipe(plugins.size({showFiles:true,title:'Output'}));
// Output file to each destination in paths
if (typeof data.dest.paths === 'string') {
js.pipe(gulp.dest(data.dest.paths));
} else {
for (var i=0; i<data.dest.paths.length; i++) {
js.pipe(gulp.dest(data.dest.paths[i]));
}
}
// add blank line to aid output readability
gutil.log('');
return;
}
var changeEvent = function(evt, src) {
gutil.log('File', gutil.colors.cyan(evt.path), 'was', gutil.colors.magenta(evt.type));
return;
};
gulp.task('default', ['css', 'js']);