-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
192 lines (174 loc) · 6.6 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
'use strict';
module.exports = function(grunt) {
var options = {
conf: grunt.file.readJSON("config.json"),
buildTasks: function(type, currentTasks) {
var tasks = currentTasks || [];
switch (type) {
case 'css':
if (this.conf.sass.activate === true) tasks.push('sass');
if (this.conf.less.activate === true) tasks.push('less');
if (this.conf.css.concatenation === true) tasks.push('clean:cssBefore', 'concat:css');
if (this.conf.css.concatenation === true && this.conf.css.cleanConcatInput === true) {
tasks.push('clean:css');
}
return tasks;
break;
case 'js':
if (this.conf.js.uglify === true) tasks.push('uglify');
if (this.conf.js.concatenation === true) tasks.push('clean:jsBefore', 'concat:js');
if (this.conf.js.concatenation === true && this.conf.js.cleanConcatInput === true) {
tasks.push('clean:js');
}
return tasks;
break;
case 'all':
tasks = this.buildTasks('css');
this.buildTasks('js', tasks);
if (tasks.length === 0) {
return grunt.log.error('No task was set in config.json');
} else {
return tasks
}
break;
}
},
cleanInput: function(type) {
var src, output;
if (type === 'css') {
src = this.conf.css.concatInput;
output = this.conf.css.output;
} else if (type === 'js') {
src = this.conf.js.concatInput;
output = this.conf.js.output;
}
src = src.toString();
src += ',!' + output;
src = src.split(',');
return src
},
watchFiles: function(type) {
var i, input, src, concat, task, concatInput;
if (type === 'css') {
if (this.conf.sass.activate === true) {
//Mapping Sass watched files
input = this.conf.sass.input;
src = this.conf.sass.src;
task = this.conf.sass.activate;
} else if (this.conf.less.activate === true) {
//Mapping Less watched files
input = this.conf.less.input;
src = this.conf.less.src;
task = this.conf.less.activate;
}
concat = this.conf.css.concatenation;
concatInput = this.conf.css.concatInput;
} else if (type === 'js') {
input = this.conf.js.input;
src = this.conf.js.src;
task = this.conf.js.uglify;
concat = this.conf.js.concatenation;
concatInput = this.conf.js.concatInput;
}
if (task === true) {
for (i = 0; i < src.length; i++) {
src[i] = input + src[i];
}
return src;
} else if (task === false && concat === true) {
return concatInput;
} else {
return [];
}
}
};
grunt.initConfig({
conf: grunt.file.readJSON("config.json"),
sass: {
dist: {
options: {
trace: true,
sourcemap: 'none',
style: '<%= conf.sass.compression %>'
},
files: [{
expand: true,
cwd: '<%= conf.sass.input %>',
src: '<%= conf.sass.src %>',
dest: '<%= conf.sass.output %>',
ext: '<%= conf.sass.extension %>'
}]
}
},
less: {
dist: {
options: {
compress: '<%= conf.less.compression %>',
ieCompat: '<%= conf.less.ieCompat %>',
sourceMap: '<%= conf.less.sourceMap %>'
},
files: [{
expand: true,
cwd: '<%= conf.less.input %>',
src: '<%= conf.less.src %>',
dest: '<%= conf.less.output %>',
ext: '<%= conf.less.extension %>'
}]
}
},
concat: {
css: {
src: '<%= conf.css.concatInput %>',
dest: '<%= conf.css.output %>'
},
js: {
src: '<%= conf.js.concatInput %>',
dest: '<%= conf.js.concatOutput %>'
}
},
clean: {
options: {
force: true
},
css: options.cleanInput('css'),
js: options.cleanInput('js'),
cssBefore: '<%= conf.css.output %>',
jsBefore: '<%= conf.js.concatOutput %>'
},
uglify: {
dist: {
options: {
beautify: '<%= conf.js.beautify %>'
},
files: [{
expand: true,
cwd: '<%= conf.js.input %>',
src: '<%= conf.js.src %>',
dest: '<%= conf.js.output %>',
ext: '<%= conf.js.extension %>'
}]
}
},
watch: {
css: {
files: options.watchFiles('css'),
tasks: options.buildTasks('css')
},
js: {
files: options.watchFiles('js'),
tasks: options.buildTasks('js')
}
}
});
grunt.registerTask('build', 'Build the project (CSS & JS)', options.buildTasks('all'));
grunt.registerTask('buildCss', 'Build the CSS', options.buildTasks('css'));
grunt.registerTask('buildJs', 'Build the JS', options.buildTasks('js'));
grunt.registerTask('watchCss', 'Watch for CSS changes, then build the CSS', ['watch:css']);
grunt.registerTask('watchJs', 'Watch for JS changes, then build the JS', ['watch:js']);
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
};