-
Notifications
You must be signed in to change notification settings - Fork 47
/
Gruntfile.js
650 lines (579 loc) · 17.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Generated on 2014-06-16 using generator-angular 0.8.0
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
var modRewrite = require('connect-modrewrite');
// Configurable paths for the application
var appConfig = {
app : require('./bower.json').appPath || 'app',
dist: 'dist'
};
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Define the configuration for all the tasks
grunt.initConfig({
// Project settings
yeoman: appConfig,
ngconstant : {
options: {
space: ' ',
wrap : '\'use strict\';\n\n {%= __ngModule %}',
name : 'config'
},
build : {
options : {
dest: '<%= yeoman.app %>/scripts/config.js'
},
constants: {
ENV: {
name : '<%= ENVConstant.name %>',
host : '<%= ENVConstant.host %>',
apiEndpoint: '<%= ENVConstant.apiEndpoint %>'
}
}
}
},
// The actual grunt server settings
connect : {
options : {
port : 9000,
// Change this to '0.0.0.0' to access the server from outside. (? still the case?)
hostname : 'localhost',
livereload: 35729
},
rules : [ // certain /keywords will open the folder instead of redirecting to /
{from: '^/(bower_components|fonts|images|scripts|styles|translations|views)(/.*)$', to: '/$1$2'},
{from: '^/404.html', to: '/404.html'},
{from: '^/(.*)$', to: '/index.html'}
],
livereload: {
options: {
open : true,
base : [
'.tmp',
'<%= yeoman.app %>/'
],
middleware: function (connect, options) {
//runs automatically, necessary for dev environment
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
// Setup the proxy
var middlewares = [
// Redirect anything that's not a file or an API call to /index.html.
// This allows HTML5 pushState to work on page reloads.
modRewrite(['!/api|/assets|\\..+$ /index.html']),
// require('grunt-connect-proxy/lib/utils').proxyRequest,
connect.static('.tmp'),
connect().use(
'/bower_components',
connect.static('./bower_components')
),
connect.static(appConfig.app)
];
// Make directory browse-able.
var directory = options.directory || options.base[options.base.length - 1];
middlewares.push(connect.directory(directory));
return middlewares;
}
//middleware: function(connect, options) {
// var middlewares = [];
// var directory = options.directory || options.base[options.base.length - 1];
// if (!Array.isArray(options.base)) {
// options.base = [options.base];
// }
// // Setup the proxy to the backend for api calls
// //middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);
// //enable modrewrite for html5mode
// middlewares.push(require('connect-modrewrite')(['^[^\\.]*$ /index.html [L]']));
// options.base.forEach(function(base) {
// // Serve static files.
// middlewares.push(connect.static(base));
// });
// // Make directory browse-able.
// middlewares.push(connect.directory(directory));
// return middlewares;
//}
}
},
test : {
options: {
port: 9001,
base: [
'.tmp',
'test',
'<%= yeoman.app %>'
]
}
},
dist : {
options: {
base: '<%= yeoman.dist %>'
}
}
},
// Make sure code styles are up to par and there are no obvious mistakes
jshint : {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all : [
'Gruntfile.js',
'<%= yeoman.app %>/scripts/**/*.js'
],
test : {
options: {
jshintrc: 'test/.jshintrc'
},
src : ['test/spec/{,*/}*.js']
}
},
// Empties folders to start fresh
clean : {
dist : {
files: [
{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'//,
// '<%= yeoman.app %>/scripts/config.js'
]
}
]
},
configure: {
files: [
{
dot: true,
src: [
'<%= yeoman.app %>/scripts/config.js'
]
}
]
},
server : '.tmp'
},
// Add vendor prefixed styles
autoprefixer: {
options: {
browsers: ['last 1 version']
},
dist : {
src : '<%= yeoman.app %>/styles/main.css',
dest: '.tmp/styles/main.css'
}
},
// Automatically inject Bower components into the app
wiredep : {
target: {
dependencies : true,
devDependencies: false,
src : ['<%= yeoman.app %>/index.html'],
ignorePath : '<%= yeoman.app %>',
exclude : ['bootstrap.js'],
fileTypes : {
html: {
replace: {
js : '<script src="/{{filePath}}"></script>',
css: '<link rel="stylesheet" href="/{{filePath}}" />'
}
}
}
}
},
// Renames files for browser caching purposes
rev : {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/**/*.js',
'<%= yeoman.dist %>/styles/{,*/}*.css',
'!<%= yeoman.dist %>/styles/overrides.css',
//'<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'!<%= yeoman.dist %>/images/marketing-assets/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'!<%= yeoman.dist %>/images/homepage/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'!<%= yeoman.dist %>/images/theme/tcp-no-image.{png,jpg,jpeg,gif,webp,svg}'//,
//'<%= yeoman.dist %>/fonts/*'
]
}
}
},
// The following *-min tasks produce minified files in the dist folder
cssmin : {
options: {}
},
imagemin: {
dist: {
files: [
{
expand: true,
cwd : '<%= yeoman.app %>/images',
src : '{,*/}*.{png,jpg,jpeg,gif}',
dest : '<%= yeoman.dist %>/images'
}
]
}
},
svgmin: {
dist: {
files: [
{
expand: true,
cwd : '<%= yeoman.app %>/images',
src : '{,*/}*.svg',
dest : '<%= yeoman.dist %>/images'
}
]
}
},
htmlmin: {
options: {
collapseWhitespace : true,
collapseBooleanAttributes: true,
removeCommentsFromCDATA : true
// removeOptionalTags: true // This option breaks livereload when used.
},
server : {
files: [
{
expand: true,
cwd : '<%= yeoman.app %>',
src : ['*.html', 'views/{,*/}*.html'],
dest : '.tmp'
}
]
},
dist : {
files: [
{
expand: true,
cwd : '<%= yeoman.dist %>',
src : ['*.html', 'views/{,*/}*.html'],
dest : '<%= yeoman.dist %>'
}
]
}
},
html2js: {
options : {
singleModule: true,
module : 'Volusion.templates',
rename : function (moduleName) {
return moduleName.replace('../app/', '');
},
htmlmin : {
collapseBooleanAttributes : true,
collapseWhitespace : true,
removeAttributeQuotes : true,
removeComments : true,
removeEmptyAttributes : true,
removeRedundantAttributes : true,
removeScriptTypeAttributes : true,
removeStyleLinkTypeAttributes: true
}
},
templates: {
src : ['<%= yeoman.app %>/views/{,*/}*.html'],
dest: '.tmp/templates.js'
}
},
concat : {
js : {
dest: '.tmp/concat/scripts/scripts.js',
src : '<%= yeoman.app %>/scripts/**/*.js'
},
templates: {
dest: '.tmp/concat/scripts/scripts.js',
src : [
'.tmp/concat/scripts/scripts.js',
'.tmp/templates.js'
]
}
},
// ngAnnotate tries to make the code safe for minification automatically by
// using the Angular long form for dependency injection.
ngAnnotate: {
dist: {
files: [
{
expand: true,
cwd : '.tmp/concat/scripts',
src : '*.js',
dest : '.tmp/concat/scripts'
}
]
}
},
// Copies remaining files to places other tasks can use
copy : {
build: {
files: [
{
expand: true,
dot : true,
cwd : '<%= yeoman.app %>',
dest : '<%= yeoman.dist %>/scripts',
src : [
'<%= yeoman.app %>/.tmp/concat/scripts/scripts.js',
]
},
{
expand: true,
cwd : '<%= yeoman.app %>',
dest : '<%= yeoman.dist %>/index.html',
src : [
'<%= yeoman.app %>/app/index.html'
]
}
]
},
dist : {
files: [
{
expand: true,
dot : true,
cwd : '<%= yeoman.app %>',
dest : '<%= yeoman.dist %>',
src : [
'*.{ico,png,txt}',
'web.config',
'.htaccess',
'*.html',
//'views/{,*/}*.html', //not right, we just need top-level html, everything else is in js
//'images/{,*/}*.{webp}', //not specific enough, wasn't finding all images
'images/**/*.*',
'fonts/**/*.*', //needed to make sure we grab all fonts
'translations/{,*/}*.json',
'settings/{,*/}*',
'styles/overrides.css',
'styles/main.css',
'bower_components/angular-i18n/angular-locale_*.js'
]
},
{
expand: true,
cwd : '.tmp',
dest : '<%= yeoman.dist %>',
src : ['styles/main.css', 'images/generated/{,*/}*.*']
}
]
}
},
// Test settings
karma : {
jasmine: {
configFile: 'test/karma.conf.jasmine.js',
singleRun : true
}
},
sass : {
dist: {
options: {
outputStyle: 'compressed' //We will change this to compressed later, just for testing
},
files : {
//all the sass needs to be in one css file
'app/styles/main.css': 'app/styles/main.scss'
}
}
},
sprite : {
icons : {
src : 'app/images/sprites/icons/*.png',
dest : 'app/images/generated/sprites/icons.png',
destCss : 'app/styles/base/icons.scss', //Use CSS format with scss filtype,
cssFormat: 'css', //otherwise outputs only compass SASS
cssVarMap: function (sprite) {
sprite.name = 'th-product__' + sprite.name;
}
},
social: {
src : 'app/images/sprites/social/*.png',
dest : 'app/images/generated/sprites/social.png',
destCss : 'app/styles/base/social.scss',
cssFormat: 'css',
cssVarMap: function (sprite) {
sprite.name = 'th-social__icon--' + sprite.name;
}
},
cards : {}
},
// Watches files for changes and runs tasks based on the changed files
watch : {
karma: {
files: ['<%= yeoman.app %>/settings/app.js',
'<%= yeoman.app %>/scripts/**/*.js',
'bower_components/vn-toolbox-common/dist/vn-toolbox-public.js'],
tasks: ['clean:server',
'newer:jshint:all',
'karma']
},
dev : {
files: ['<%= yeoman.app %>/settings/app.js',
'<%= yeoman.app %>/scripts/**/*.js',
'bower_components/vn-toolbox-common/dist/vn-toolbox-common.js',
'<%= yeoman.app %>/*.html',
'<%= yeoman.app %>/views/**/*.html',
'<%= yeoman.app %>/styles/**/*.{scss,sass}',
'bower_components/vn-toolbox-common/app/styles/**/*.{scss,sass}'],
tasks: ['sass', //do SASS compilation which generates main.css,
//rest of files are being served from app folder
],
options:{
livereload: true
}
}
},
// Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
useminPrepare: {
html : '<%= yeoman.app %>/index.html',
options: {
root: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
}
},
// Performs rewrites based on rev and the useminPrepare configuration
usemin : {
html : ['<%= yeoman.dist %>/index.html'],
css : ['<%= yeoman.dist %>/styles/{,*/}*.css'],
options: {},
js : [
'<%= yeoman.dist %>/scripts/*.js',
'<%= yeoman.dist %>/settings/app.js'
]
},
});
grunt.registerTask('build', ['build:dev']);
grunt.registerTask('serve', ['build:dev']);
grunt.registerTask('build:dev', function (protocol, server, name, version) {
//if there is no target, we'll set the target to samplestore
if (typeof server === 'undefined') {
server = (protocol || 'http') + '://' + 'www.samplestore.io';
} else {
server = (protocol || 'http') + '://' + server;
}
if (typeof name === 'undefined') {
name = 'samplestore';
}
if (typeof version === 'undefined') {
version = '/api/v1';
}
grunt.log.writeln('API URL set for the environment "' + name + '" - ' + server + version);
// grunt.log.writeln('Switching vn-toolbox-public to vn-toolbox-common');
// var bowerJson = require('./bower.json');
// delete bowerJson.dependencies['vn-toolbox-public'];
// bowerJson.dependencies['vn-toolbox-common'] = '*';
// var fs = require('fs');
// fs.writeFileSync('./bower.json', JSON.stringify(bowerJson, null, 2));
grunt.config.set('ENVConstant', {
name : name,
host : server,
apiEndpoint: version
});
grunt.task.run([
'clean:dist', //erase our dist folder
//no need to clean dist folder; we're serving /app, lets do it anyway
'clean:configure', //erase generated config file
'get_toolbox_dependencies',
'ngconstant:build', //build generated config file
'wiredep', //include the dependencies
'sprite:icons', //Create the icon scss file
'sprite:social', //create the social scss file
'sass', //do SASS compilation
'connect:livereload', //open server
'watch:dev' //watch for files
]);
});
grunt.registerTask('build:dist', function (protocol, server, name, version) {
//there MUST be a target! Can't build for distribution without a target api
if (typeof server !== 'undefined' && server.trim() !== '') {
grunt.fail.warn('WARNING: You are using different server for the API cconfiguration. ' +
'You need to have CORS enabled on the API endpoint for the theme to work.');
server = (protocol || 'http') + '://' + server;
} else {
server = '';
}
if (typeof name === 'undefined') {
name = 'samplestore';
}
if (typeof version === 'undefined') {
version = '/api/v1';
}
grunt.config.set('ENVConstant', {
name : name,
host : server,
apiEndpoint: version
});
//needs to pass karma and JSHint in order to build properly
//should uglify
grunt.task.run([
'clean:dist', //erase our dist folder
'clean:configure', //erase generated config file
'get_toolbox_dependencies',
'ngconstant:build', //build generated config file
'wiredep', //include the dependencies
'karma', //run tests, must pass
'jshint:all', //run jshint, must pass
'useminPrepare', //Prep for creating the html file with the minified files
'sprite:icons', //Create the icon scss file
'sprite:social', //create the social scss file
'sass', //do SASS compilation
'autoprefixer', //add browser prefixes to css file (inplace edit .tmp/style/main.css)
'html2js', //transforms html to templates.js
'concat:generated', //concats vendor JS files to vendor.js and scripts to scripts.js
'concat:templates', //concats templates.js *after* scripts.js
'uglify', //remove spaces, linebreaks, replace variables
'cssmin', //minifies CSS
'copy:dist', //copy the required files to the dist folder, so they can be overwritten
'rev', //cachebusting
'usemin', //last step! here's where we actually make changes to the index.html file
//which is why this step needs to be last, so we don't lose our original
]);
});
grunt.registerTask('test',
['clean:server', //may not be needed?
'newer:jshint:all',
'karma',
'watch:karma']); //only reload on js file change
grunt.registerTask('build:travis', ['build:dist']); //run a test build without watching
grunt.registerTask('test:travis', //don't live watch the files on travis build
['clean:server', //may not be needed?
'newer:jshint:all',
'karma']);
grunt.registerTask('get_toolbox_dependencies', 'Add VN Toolbox Dependencies to bower.json', function () {
var fs = require('fs');
fs.exists('bower_components/vn-toolbox-common/bower.json', function (exists) {
if(exists){
var _ = require('lodash');
var vnBower = JSON.parse(fs.readFileSync('bower_components/vn-toolbox-common/bower.json', 'utf8'));
var origBower = JSON.parse(fs.readFileSync('bower.json', 'utf8'));
_.extend(origBower.dependencies, vnBower.dependencies);
fs.writeFileSync('bower.json', JSON.stringify(origBower, undefined, 2), 'utf8');
}
});
});
grunt.registerTask('default', function () {
grunt.log.errorlns('No Grunt commands selected! Your options are:');
grunt.log.writeln('>grunt test [runs karma tests and jshint]');
grunt.log.writeln('');
grunt.log.writeln('>grunt serve [builds without minification or concatination for http://www.samplestore.io/api/v1]');
grunt.log.writeln('>grunt build [builds without minification or concatination for http://www.samplestore.io/api/v1]');
grunt.log.writeln('>grunt build:dev [builds without minification or concatination for http://www.samplestore.io/api/v1]');
grunt.log.writeln('>grunt build:dev:http:yourserver.here [builds without minification or concatination for http://yourserver.here/api/v1]');
grunt.log.writeln('>grunt build:dev:http:yourserver.here:yourserver:/api/vX [builds without minification or concatination for a server named yourserver, located at http://yourserver.here/api/vX]');
grunt.log.writeln('');
grunt.log.writeln('>grunt build:dist:http:yourserver.here [builds with minification and concatination for http://yourserver.here/api/v1]');
grunt.log.writeln('>grunt build:dist:http:yourserver.here:yourserver:/api/vX [builds with minification and concatination for a server named yourserver, located at http://yourserver.here/api/vX]');
});
};