-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
executable file
·546 lines (448 loc) · 12.9 KB
/
Gruntfile.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
module.exports = function(grunt) {
// Load all grunt tasks.
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
// Load the config files.
var mrpig = grunt.file.readYAML('mr_pig/mr_pig.yaml');
if (grunt.file.exists('mr_pig/mr_pig.preferences.yaml')) {
var mrpigPrefs = grunt.file.readYAML('mr_pig/mr_pig.preferences.yaml');
} else {
var mrpigPrefs = {};
// Inform user that they can tweak some preferences.
grunt.log.subhead('Mr. Pig');
grunt.log.writeln('-------');
grunt.log.writeln(
'You can set personal preferences for things like which browser to use during development ' +
'in `mr_pig/mr_pig.preferences.yaml`. See `mr_pig/mr_pig.preferences.yaml.sample`.'
);
}
// Set default preferences.
mrpig['browser'] = 'Google Chrome Canary';
mrpig['local_url'] = 'http://localhost:8080';
// Set empty values for potentially absent options.
mrpig['ssh'] = {};
mrpig['ssh']['user'] = '';
mrpig['ssh']['host'] = '';
// Overwrite default preferences.
mrpig = mergeObjects(mrpig, mrpigPrefs);
// Build task lists.
var buildTasks = [];
var concurrentTasks = [];
addToTaskList('copy:app', mrpig.enable.copy, buildTasks);
addToTaskList('copy:theme', mrpig.enable.copy, buildTasks);
addToTaskList('copy:misc', mrpig.enable.copy, buildTasks);
addToTaskList('copy:picturefill', mrpig.enable.picturefill, buildTasks);
addToTaskList(
'useminPrepare',
mrpig.enable.usemin.js || mrpig.enable.usemin.css,
buildTasks
);
addToTaskList('concat:generated', mrpig.enable.usemin.js, buildTasks);
addToTaskList('uglify:generated', mrpig.enable.usemin.js, buildTasks);
addToTaskList('cssmin:generated', mrpig.enable.usemin.css, buildTasks);
addToTaskList(
'usemin',
mrpig.enable.usemin.js || mrpig.enable.usemin.css,
buildTasks
);
// Just go ahead and add `concurrent:build`. It just won't do anything if
// neither imageoptim plugin is enabled.
addToTaskList('newer:concurrent:build', true, buildTasks);
addToTaskList('shell:autoprefixer', mrpig.enable.autoprefixer, buildTasks);
addToTaskList('newer:sass:build', mrpig.enable.sass, buildTasks);
addToTaskList('shell:uncss', mrpig.enable.uncss, buildTasks);
addToTaskList('cssmin:build', mrpig.enable.cssmin, buildTasks);
addToTaskList(
'imageoptim:imageAlpha',
mrpig.enable.imageoptim.imageAlpha,
concurrentTasks
);
addToTaskList(
'imageoptim:jpegMini',
mrpig.enable.imageoptim.jpegMini,
concurrentTasks
);
addToTaskList(
'svgmin',
mrpig.enable.svgmin,
concurrentTasks
);
// Set the theme paths for the local and built versions of the site.
mrpig.theme_path = mrpig.local + '/_themes/' + mrpig.theme;
mrpig.theme_path_dist = mrpig.dist + '/_themes/' + mrpig.theme;
grunt.initConfig({
// Allow using vars in strings (`'<%= mrpig.theme_path %>'`).
mrpig: mrpig,
autoprefixer: {
dev: {
files: [{
expand: true,
cwd: mrpig.theme_path + '/css/',
src: ['*.css'],
dest: mrpig.theme_path + '/css/'
}],
options: {
map: true
}
},
build: {
src: mrpig.theme_path_dist + '/css/*.css'
},
options: {
browsers: mrpig.autoprefixer.browsers
}
},
browserSync: {
dev: {
bsFiles: {
src: [
mrpig.theme_path + '/css/**/*.css',
mrpig.theme_path + '/**/*.{js,html}',
mrpig.local + '/_content/**/*.md'
]
},
options: {
browser: mrpig.browser,
proxy: mrpig.local_url,
watchTask: true
}
}
},
buildcontrol: {
stage: {
options: {
remote: mrpig.ssh.user + '@' + mrpig.ssh.host + ':' +
mrpig.buildcontrol.environments.stage.options.remote,
branch: 'master'
}
},
live: {
options: {
remote: mrpig.ssh.user + '@' + mrpig.ssh.host + ':' +
mrpig.buildcontrol.environments.live.options.remote,
branch: 'master'
}
},
options: {
dir: mrpig.dist,
commit: true,
push: true,
message: mrpig.buildcontrol.options.message
}
},
// Generated by usemin. This is here to prevent errors when using newer.
concat: {},
concurrent: {
build: {
tasks: concurrentTasks
}
},
copy: {
// All Statamic core files.
app: {
expand: true,
cwd: mrpig.local,
dest: mrpig.dist,
src: [
'_add-ons/**',
'_app/**',
'_cache/index.html',
'_config/**',
'_content/**',
'_logs/index.html',
'_storage/index.html',
'admin/**',
'.htaccess',
'admin.php',
'index.php'
]
},
// All theme files that don't get moved by another plugin.
theme: {
expand: true,
cwd: mrpig.theme_path,
dest: mrpig.theme_path_dist,
src: [
'{layouts,partials,templates}/**/*.html',
'css/fonts/**',
'img/**',
'*.yaml'
]
},
// Other, dev-specified necessary files.
misc: {
expand: true,
cwd: mrpig.local,
dest: mrpig.dist,
src: mrpig.copy.src
},
picturefill: {
files: {
'<%= mrpig.theme_path_dist %>/js/vendor/picturefill.min.js': [
'node_modules/picturefill/dist/picturefill.min.js'
]
}
},
options: {
mode: true
}
},
cssmin: {
build: {
files: [{
expand: true,
cwd: mrpig.theme_path_dist + '/css',
src: ['*.css'],
dest: mrpig.theme_path_dist + '/css',
}]
}
},
// htmlmin is touchy with Statamic templates.
// Known incompatibilities:
// - YAML front matter
// - Conditionals where boolean attributes are b/w tags w/o whitespace.
// Ex: <option {{ if some_var }}selected{{ /if }}>
//
// htmlmin: {
// build: {
// files: [{
// expand: true,
// cwd: mrpig.theme_path_dist,
// src: '**/*.html',
// dest: mrpig.theme_path_dist
// }],
// options: {
// collapseBooleanAttributes: true,
// collapseWhitespace: true,
// conservativeCollapse: true,
// minifyCSS: true,
// minifyJS: true,
// preserveLineBreaks: true,
// removeAttributeQuotes: true,
// removeComments: true,
// removeEmptyAttributes: true,
// removeEmptyElements: true,
// removeOptionalTags: true,
// removeRedundantAttributes: true,
// removeScriptTypeAttributes: true,
// removeStyleLinkTypeAttributes: true,
//
// // Allow attributes to be wrapped in
// // [conditionals with spaces and/or newlines].
// customAttrSurround: [
// [
// // Opening {{ if }}, {{ elseif }}, {{ else }}, {{ unless }},
// // or {{ unlesselse }}
// /\{\{(.|\n)*(if|elseif|else|unless|unlesselse)(.|\n)*\}\}/,
// // Closing {{ /if }} or {{ endif }}
// /\{\{(.|\n)*(\/|end)if(.|\n)*\}\}/
// ]
// ]
// }
// }
// },
imageoptim: {
imageAlpha: {
files: [{
expand: true,
cwd: mrpig.dist,
src: pushReturn(
mrpig.imageoptim.imageAlpha.src,
'_themes/<%= mrpig.theme %>/img/**/*.{jpg,jpeg,png,gif}'
)
}],
options: {
imageAlpha: true
}
},
jpegMini: {
files: [{
expand: true,
cwd: mrpig.dist,
src: pushReturn(
mrpig.imageoptim.jpegMini.src,
'_themes/<%= mrpig.theme %>/img/*.{jpg,jpeg}'
)
}],
options: {
jpegMini: true
}
}
},
sass: {
dev: {
files: [{
expand: true,
cwd: mrpig.theme_path + '/scss',
src: ['*.scss'],
dest: mrpig.theme_path + '/css',
ext: '.css'
}],
options: {
lineNumbers: true,
style: 'expanded'
}
},
build: {
files: [{
expand: true,
cwd: mrpig.theme_path + '/scss',
src: ['*.scss'],
dest: mrpig.theme_path_dist + '/css',
ext: '.css'
}],
options: {
sourcemap: 'none',
style: 'compressed',
update: true
}
}
},
shell: {
autoprefixer: {
// Update the caniuse db for autoprefixer.
command: 'npm update caniuse-db'
},
uncss: {
command: buildUncssCommand(mrpig.uncss.config, mrpig.local_url)
}
},
svgmin: {
build: {
files: [{
expand: true,
cwd: mrpig.theme_path_dist,
src: mrpig.svgmin.src,
dest: mrpig.theme_path_dist,
ext: '.svg'
}]
},
options: {
plugins: mrpig.svgmin.plugins
}
},
// Generated by usemin. This is here to prevent errors when using newer.
uglify: {},
usemin: {
html: {
src: mrpig.usemin.src,
dest: mrpig.dist
}
},
useminPrepare: {
html: {
src: prependArray(mrpig.theme_path_dist, mrpig.usemin.src),
dest: mrpig.dist
}
},
watch: {
configFiles: {
files: [
'Gruntfile.js',
'mr_pig/mr_pig.yaml'
]
},
dev: {
files: [mrpig.theme_path + '/scss/**/*.scss'],
tasks: [
'sass:dev',
'autoprefixer:dev'
]
}
}
});
// ---
// Use `serve` to maintain consistency with other popular Gruntfiles.
grunt.registerTask('serve', [
'browserSync',
'watch'
]);
// Set default to `serve` for convenience.
grunt.registerTask('default', ['serve']);
grunt.registerTask('build', buildTasks);
var target = grunt.option('live') ? 'live' : 'stage';
grunt.registerTask('deploy', ['buildcontrol:' + target]);
};
// ---
// HELPERS
/**
* Prepend each value in an array with a string, then return the array.
* @param {str} str The string to prepend.
* @param {arr} arr The array whose values need appendin'.
*/
function prependArray(str, arr) {
for (var i in arr) {
arr[i] = str + arr[i];
}
return arr;
}
/**
* Push a value to an array, then return the array.
* @param {arr} arr The array to push to.
* @param {val} val The value to push to the array.
*/
function pushReturn(arr, val) {
arr.push(val);
return arr;
}
/**
* Recursively merge the properties of two objects into the first, then return
* the object.
* @param {object} obj1 The first object.
* @param {object} obj2 The second object.
*/
function mergeObjects(obj1, obj2) {
for (var p in obj2) {
try {
// Property in destination object set; update its value.
if (obj2[p].constructor == Object) {
obj1[p] = mergeObjects(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch(e) {
// Property in destination object not set; create it and set its value.
obj1[p] = obj2[p];
}
}
return obj1;
}
/**
* Use mr_pig.yaml to build the commands for shell:uncss.
* @param {object} config The configuration for the command.
* @param {string} rootUrl The URL to the root page.
*/
function buildUncssCommand(config, rootUrl) {
// Create an array to hold the commands.
var commands = [];
// For each index in the config...
for (var i in config) {
// Create the variable to hold the URLs string.
var urls = '';
// For each URL in the config...
for (var url in config[i].urls) {
// Add the URL and a comma to separate it from the next one.
urls += rootUrl + config[i].urls[url] + ',';
}
// Remove the trailing comma from `urls`.
urls = urls.substring(0, urls.length - 1);
// Add each command to the commands array.
commands.push(
'node mr_pig/uncss.js ' +
config[i].stylesheet + ' ' +
urls
);
}
// Join each command from the array into a single command and return it.
return commands.join('&&');
}
/**
* Conditionally add the given task to the given task list.
* @param {string} task The task to add.
* @param {boolean} enabled Whether or not to add the task.
* @param {object} taskList The task list to add the task to.
*/
function addToTaskList(task, enabled, taskList) {
if (enabled) {
return taskList.push(task);
}
}