-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
260 lines (226 loc) · 7.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var gulp = require("gulp"),
args = require("yargs").argv,
del = require("del"),
browserSync = require("browser-sync"),
$ = require("gulp-load-plugins")({lazy: true});
var config = require("./config")();
gulp.task("default", ["help"]);
gulp.task("help", $.taskListing);
gulp.task("clean", function (done) {
var path = [].concat(config.build, config.css);
log("Cleaning: " + $.util.colors.blue(path));
del(path, done);
});
gulp.task("clean-fonts", function (done) {
clean(config.build + "fonts/**/*.*", done);
});
gulp.task("clean-images", function (done) {
clean(config.build + "images/**/*.*", done);
});
gulp.task("clean-styles", function (done) {
clean(config.css + "**/*.css", done);
});
gulp.task("clean-code", function (done) {
var files = [].concat(
config.css + "**/*.css",
config.temp + "**/*.js",
config.build + "**/*.html",
config.build + "js/**/*.js"
);
clean(files, done);
});
gulp.task("fonts", ["clean-fonts"], function () {
log("*** Copying fonts");
return gulp.src(config.fonts)
.pipe(gulp.dest(config.build + "fonts"));
});
gulp.task("images", ["clean-images"], function () {
log("*** Copying and compressing the images");
return gulp.src(config.images)
.pipe($.imagemin({ optimizationLevel: 4 }))
.pipe(gulp.dest(config.build + "images"));
});
gulp.task("styles", ["clean-styles"], function () {
log("*** Compiling Stylus to CSS");
return gulp.src(config.styles)
.pipe($.plumber())
.pipe($.stylus())
.pipe($.autoprefixer({ browsers: ["Last 2 version", "> 5%"] }))
.pipe(gulp.dest(config.css));
});
gulp.task("lint", function () {
log("*** Linting all JS files");
return gulp.src(config.allJs)
.pipe($.if(args.verbose, $.print()))
.pipe($.jshint())
.pipe($.jshint.reporter("jshint-stylish", { verbose: true }))
.pipe($.jshint.reporter("fail"));
});
gulp.task("templatecache", ["clean-code"], function () {
log("*** creating AngularJS $templatecache");
return gulp.src(config.htmlTemplates)
.pipe($.minifyHtml({ empty: true }))
.pipe($.angularTemplatecache(
config.templateCache.file,
config.templateCache.options
))
.pipe(gulp.dest(config.temp));
});
gulp.task("wiredep", function () {
log("*** Wiring up bower css, js and custom js files into the index.html file");
var wiredep = require("wiredep").stream,
options = config.getWiredepDefaultOptions();
return gulp.src(config.index)
.pipe(wiredep(options))
.pipe($.inject(gulp.src(config.js)))
.pipe(gulp.dest(config.client));
});
gulp.task("inject", ["wiredep", "styles", "templatecache"], function () {
log("*** Injecting custom css files.");
return gulp.src(config.index)
.pipe($.inject(gulp.src(config.css + "**/*.css")))
.pipe(gulp.dest(config.client));
});
gulp.task("optimize", ["inject", "fonts", "images"], function () {
log("*** Optimizing the javascripts, css and html");
var assets = $.useref.assets({ searchPath: config.root });
var templateCache = config.temp + config.templateCache.file;
var cssFilter = $.filter("**/*.css", { restore: true });
var jsLibFilter = $.filter("**/" + config.optimized.lib, { restore: true });
var jsAppFilter = $.filter("**/" + config.optimized.app, { restore: true });
return gulp.src(config.index)
.pipe($.plumber())
.pipe($.inject(gulp.src(templateCache, { read: false }), {
starttag: "<!-- inject:templates.js -->"
}))
.pipe(assets)
.pipe(cssFilter)
.pipe($.csso())
.pipe(cssFilter.restore)
.pipe(jsLibFilter)
.pipe($.uglify())
.pipe(jsLibFilter.restore)
.pipe(jsAppFilter)
.pipe($.ngAnnotate())
.pipe($.uglify())
.pipe(jsAppFilter.restore)
.pipe($.rev())
.pipe(assets.restore())
.pipe($.useref())
.pipe($.revReplace())
.pipe(gulp.dest(config.build))
.pipe($.rev.manifest())
.pipe(gulp.dest(config.build));
});
gulp.task("bump", function () {
var msg = "Bumping versions";
var type = args.type;
var version = args.version;
var options = {
};
if (version) {
options.version = version;
msg += " to " + version;
} else {
options.type = type;
msg += " for a " + type;
}
log(msg);
return gulp.src(config.packages)
.pipe($.bump(options))
.pipe(gulp.dest(config.root));
});
gulp.task("serve-build", ["optimize"], function (isDev) {
serve(false /* isDev */);
});
gulp.task("serve-dev", ["inject"], function () {
log("*** Serving up development environment");
serve(true /* isDev */);
});
function serve(isDev) {
var options = {
script: config.nodeServer,
delayTime: 1,
env: {
"PORT": config.port,
"NODE_ENV": isDev ? "dev" : "production"
},
watch: [config.server]
};
$.nodemon(options)
.on("restart", function (ev) {
log("*** nodemon restarted.");
log("files changed on restart:\n" + ev);
setTimeout(function () {
browserSync.notify("Reloading now...");
browserSync.reload({ stream: false });
}, config.browserReloadDelay);
})
.on("start", function () {
console.log("*** nodemon started.");
startBrowserSync(isDev);
})
.on("crash", function () {
log("*** nodemon crashed due to unexpected reason(s).");
})
.on("exit", function () {
log("*** nodemon exited successfully!.");
});
}
// Utilities Functions
function changeEvent(event) {
var srcPattern = new RegExp('/.*(?=/' + config.source + ')/');
log('File ' + event.path.replace(srcPattern, '') + ' ' + event.type);
}
function startBrowserSync(isDev) {
log("*** Starting browser sync");
if (browserSync.active || args.nosync) {
return;
}
if (isDev) {
gulp.watch([config.styles], ["styles"])
.on("change", function (event) { changeEvent(event); });
} else {
gulp.watch([config.styles, config.js, config.html], ["optimize", browserSync.reload])
.on("change", function (event) { changeEvent(event); });
}
var options = {
proxy: "localhost:" + config.port,
port: 8000,
files: isDev ? [
config.client + "**/*.*",
"!" + config.styles,
config.css + "**/*.css"
] : [],
ghostMode: {
clicks: true,
scroll: true,
location: false,
form: true
},
injectChanges: true,
logFileChanges: true,
logLevel: "debug",
logPrefix: "gulp-bs",
notify: true,
reloadDelay: 1,
};
browserSync(options);
}
function clean(path, done) {
del(path).then(function () {
log("Cleaning: " + $.util.colors.blue(path));
done();
});
}
function log(msg) {
if (typeof msg === "object") {
for (var item in msg) {
if (item.hasOwnProperty(item)) {
$.util.log($.util.colors.blue(msg[item]));
}
}
} else {
$.util.log($.util.colors.blue(msg));
}
}