-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
342 lines (312 loc) · 9.04 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* ----------------------------------------------------------------------------
* Imports
* ------------------------------------------------------------------------- */
var gulp = require("gulp");
var addsrc = require("gulp-add-src");
var args = require("yargs").argv;
var autoprefix = require("autoprefixer-core");
var cache = require("gulp-cached");
var clean = require("del");
var collect = require("gulp-rev-collector");
var concat = require("gulp-concat");
var ignore = require("gulp-ignore");
var mincss = require("gulp-minify-css");
var minhtml = require("gulp-htmlmin");
var modernizr = require("gulp-modernizr");
var mqpacker = require("css-mqpacker");
var notifier = require("node-notifier");
var gulpif = require("gulp-if");
var pixrem = require("pixrem");
var plumber = require("gulp-plumber");
var postcss = require("gulp-postcss");
var reload = require("gulp-livereload");
var rev = require("gulp-rev");
var sass = require("gulp-sass");
var sourcemaps = require("gulp-sourcemaps");
var sync = require("gulp-sync")(gulp).sync;
var child = require("child_process");
var uglify = require("gulp-uglify");
var util = require("gulp-util");
var vinyl = require("vinyl-paths");
var debug = require("gulp-debug");
var react = require('gulp-react');
var order = require('gulp-order');
/* ----------------------------------------------------------------------------
* Locals
* ------------------------------------------------------------------------- */
/* Application server */
var server = null;
/* ----------------------------------------------------------------------------
* Overrides
* ------------------------------------------------------------------------- */
/*
* Override gulp.src() for nicer error handling.
*/
var src = gulp.src;
gulp.src = function() {
return src.apply(gulp, arguments)
.pipe(plumber(function(error) {
util.log(util.colors.red(
"Error (" + error.plugin + "): " + error.message
));
notifier.notify({
title: "Error (" + error.plugin + ")",
message: error.message.split("\n")[0]
});
this.emit("end");
})
);
};
/* ----------------------------------------------------------------------------
* Assets pipeline
* ------------------------------------------------------------------------- */
/*
* Build stylesheets from SASS source.
*/
gulp.task("app:stylesheets", function() {
return gulp.src("app/styles/*.scss")
.pipe(sass({
includePaths: [
"bower_components/foundation/scss"
],
outputStyle: 'nested',
errLogToConsole: true}))
.pipe(gulpif(args.sourcemaps, sourcemaps.init()))
.pipe(gulpif(args.production,
postcss([
autoprefix(),
mqpacker,
pixrem("10px")
])))
.pipe(gulpif(args.sourcemaps, sourcemaps.write()))
.pipe(gulpif(args.production, mincss()))
.pipe(gulp.dest("public/static/"))
.pipe(reload());
});
/*
* Build javascripts from Bower components and source.
*/
gulp.task("app:javascripts", function() {
return gulp.src([
"bower_components/jquery/dist/jquery.js",
"bower_components/react/JSXTransformer.js",
"bower_components/react/react-with-addons.js",
"bower_components/react-router/build/umd/ReactRouter.js",
"bower_components/react-bootstrap/react-bootstrap.js",
"bower_components/react-router-bootstrap/lib/ReactRouterBootstrap.js",
"app/scripts/**/*.jsx"
])
.pipe(order([
"bower_components/jquery/dist/jquery.js",
"bower_components/react/JSXTransformer.js",
"bower_components/react/react-with-addons.js",
"bower_components/react-router/build/umd/ReactRouter.js",
"bower_components/react-bootstrap/react-bootstrap.js",
"bower_components/react-bootstrap/react-bootstrap.js",
"bower_components/react-router-bootstrap/lib/ReactRouterBootstrap.js",
"app/scripts/app.jsx",
"app/scripts/components/*",
"app/scripts/actions/*",
"app/scripts/constants/*",
"app/scripts/stores/*",
"app/scripts/dispatcher/*",
"app/scripts/routes.jsx"
], { base: './' }))
.pipe(gulpif(args.sourcemaps, sourcemaps.init()))
.pipe(concat("application.js"))
.pipe(react())
.pipe(gulpif(args.sourcemaps, sourcemaps.write()))
.pipe(gulpif(args.production, uglify()))
.pipe(gulp.dest("public/static/"))
.pipe(reload());
});
/*
* Create a customized modernizr build.
*/
gulp.task("app:modernizr", function() {
return gulp.src([
//"public/stylesheets/style.css",
"public/javascripts/application.js"
]).pipe(
modernizr({
options: [
"addTest", /* Add custom tests */
"fnBind", /* Use function.bind */
"html5printshiv", /* HTML5 support for IE */
"setClasses", /* Add CSS classes to root tag */
"testProp" /* Test for properties */
]
}))
.pipe(addsrc.append("bower_components/respond/dest/respond.src.js"))
.pipe(concat("modernizr.js"))
.pipe(gulpif(args.production, uglify()))
.pipe(gulp.dest("public/static"));
});
/*
* Minify views.
*/
gulp.task("app:views", args.production ? [
"app:stylesheets",
"app:javascripts",
"app:modernizr",
"app:revisions:clean",
"app:revisions"
] : [], function() {
return gulp.src(["views/**/*.tmpl"])
.pipe(cache("views"))
.pipe(
minhtml({
collapseBooleanAttributes: true,
collapseWhitespace: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyCSS: true,
minifyJS: true
}))
.pipe(gulp.dest(".views"));
});
/*
* Clean outdated revisions.
*/
gulp.task("app:revisions:clean", function() {
return gulp.src(["public/**/*.{css,js}"])
.pipe(ignore.include(/-[a-f0-9]{8}\.(css|js)$/))
.pipe(vinyl(clean));
});
/*
* Revision app after build.
*/
gulp.task("app:revisions", [
"app:stylesheets",
"app:javascripts",
"app:modernizr",
"app:revisions:clean"
], function() {
return gulp.src(["public/**/*.{css,js}"])
.pipe(ignore.exclude(/-[a-f0-9]{8}\.(css|js)$/))
.pipe(rev())
.pipe(gulp.dest("public"))
.pipe(rev.manifest())
.pipe(gulp.dest("public"));
})
/*
* Build app.
*/
gulp.task("app:build", [
//"app:stylesheets",
"app:javascripts",
"app:modernizr"
]);
/*
* Watch app for changes and rebuild on the fly.
*/
gulp.task("app:watch", function() {
/* Rebuild stylesheets on-the-fly */
//gulp.watch([
// "app/styles/**/*.scss"
//], ["app:stylesheets"]);
/* Rebuild javascripts on-the-fly */
gulp.watch([
"app/scripts/**/*.js",
"app/scripts/**/*.jsx",
"bower.json"
], ["app:javascripts"]);
});
/* ----------------------------------------------------------------------------
* Application server
* ------------------------------------------------------------------------- */
/*
* Build application server.
*/
gulp.task("server:build", function() {
if (server)
server.kill();
var build = child.spawnSync("go", ["install"]);
if (build.stderr.length) {
var lines = build.stderr.toString()
.split("\n").filter(function(line) {
return line.length
});
for (var l in lines)
util.log(util.colors.red(
"Error (go install): " + lines[l]
));
notifier.notify({
title: "Error (go install)",
message: lines
});
}
return build;
});
/*
* Restart application server.
*/
gulp.task("server:spawn", function() {
if (server)
server.kill();
/* Spawn application server */
server = child.spawn("tyovuoro.exe");
/* Trigger reload upon server start */
server.stdout.once("data", function() {
reload.reload("/");
});
/* Pretty print server log output */
server.stdout.on("data", function(data) {
var lines = data.toString().split("\n")
for (var l in lines)
if (lines[l].length)
util.log(lines[l]);
});
/* Print errors to stdout */
server.stderr.on("data", function(data) {
process.stdout.write(data.toString());
});
});
/*
* Watch source for changes and restart application server.
*/
gulp.task("server:watch", function() {
/* Restart application server */
gulp.watch([
".views/**/*.tmpl",
"locales/*.json"
], ["server:spawn"]);
/* Rebuild and restart application server */
gulp.watch([
"*.go",
"*/**/*.go"
], sync([
"server:build",
"server:spawn"
], "server"));
});
/* ----------------------------------------------------------------------------
* Interface
* ------------------------------------------------------------------------- */
/*
* Build assets and application server.
*/
gulp.task("build", [
"app:build",
"server:build"
]);
/*
* Start asset and server watchdogs and initialize livereload.
*/
gulp.task("watch", [
/* "app:build",*/
"server:build"
], function() {
reload.listen();
return gulp.start([
"app:build",
"app:watch",
"server:watch",
"server:spawn"
]);
});
/*
* Build app by default.
*/
gulp.task("default", ["build"]);