This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
executable file
·220 lines (179 loc) · 6.25 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
module.exports = function(grunt) {
var Toolbox = {};
Toolbox.config = {
src: grunt.option("src") || null,
dest: grunt.option("dest") || null,
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
imagemin: {
inplace: {
options: {
optimizationLevel: 7,
progressive: true,
interlaced: true
},
files: [{
expand: true,
cwd: Toolbox.config.src,
src: ['**/*.{png,jpg,gif,jpeg}'],
dest: Toolbox.config.src,
}]
},
staticinplace: {
options: {
optimizationLevel: 7,
progressive: true,
interlaced: true
},
files: {}
},
static: {
options: {
optimizationLevel: 7,
progressive: true,
interlaced: true
},
files: {}
},
default: {
options: {
optimizationLevel: 7,
progressive: true,
interlaced: true
},
files: [{
expand: true,
cwd: Toolbox.config.src,
src: ['**/*.{png,jpg,gif,jpeg}'],
dest: Toolbox.config.dest,
}]
}
},
// CONCAT JS
// concat: {
// options: {
// separator: "\n", //add a new line after each file
// banner: "", //added before everything
// footer: "" //added after everything
// },
// dist: {
// // the files to concatenate
// src: [
// //include libs
// Toolbox.jsSrcPath() + 'jquery.colorbox-min.js',
// Toolbox.jsSrcPath() + 'jquery.cycle.js',
// ],
// // the location of the resulting JS file
// dest: Toolbox.jsDestPath() + 'scripts.js'
// }
// },
// uglify: {
// build: {
// files: [{
// expand: true,
// cwd: Toolbox.jsDestPath(),
// src: ['scripts.js'],
// dest: Toolbox.jsDestPath(),
// ext: '.js'
// }]
// }
// },
watch: {
// scripts: {
// files: [Toolbox.jsSrcPath() + '*.js'],
// tasks: ['concat:dist', 'uglify'],
// options: {
// interrupt: true
// }
// },
newimages: {
files: ["**/*.{png,jpg,jpeg,gif}"],
tasks: ['compress-images:new'],
options: {
cwd: Toolbox.config.src
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-newer');
// Image related Tasks
grunt.registerTask('compress', "Watches a folder and compresses images inside", function(){
var args = this.args;
if(args[0] === "images"){
if (args[1] === "watch"){
// compress:images:watch
grunt.task.run(['compress-images', 'watch:newimages']);
} else if (args[1] === "new"){
if (args[2] === "watch"){
// compress:images:new:watch
grunt.task.run(['compress-images:new', 'watch:newimages']);
} else {
// compress:images:new
grunt.task.run(['compress-images:new']);
}
} else {
// compress:images
grunt.task.run(['compress-images']);
}
} else if (args[0] === "image"){
grunt.task.run(['compress-image']);
} else if (args[0] === "watch"){
if(!Toolbox.config.src) grunt.fatal("Please specify a folder to watch with --src argument");
else grunt.task.run(['watch:newimages']);
} else if (args[0] === undefined) {
grunt.task.run(['compress-images', 'watch:newimages']);
} else {
grunt.fatal("This task is not available. Please use `grunt help` for usage examples.");
}
});
grunt.registerTask('compress-images', "Compresses images inside a folder using imagemin", function(isNew){
var src = Toolbox.config.src;
var dest = Toolbox.config.dest;
if(src && dest && (src !== dest)) {
grunt.log.writeln("Optimizing with different src and dest");
if(isNew) grunt.task.run(['newer:imagemin:default']);
else grunt.task.run(['imagemin:default']);
} else if (src) {
grunt.log.writeln("Optimizing in place");
if(isNew) grunt.task.run(['newer:imagemin:inplace']);
else grunt.task.run(['imagemin:inplace']);
} else {
grunt.fatal("Please specify a source and/or destination folder");
}
});
grunt.registerTask('compress-image', "Compresses an image using imagemin", function(){
var src = Toolbox.config.src;
var dest = Toolbox.config.dest;
if(src && dest && (src !== dest)) {
grunt.log.writeln("Optimizing with different src and dest");
var staticfiles = {};
staticfiles[dest] = src;
grunt.config.set(['imagemin', 'static', 'files'], staticfiles);
grunt.task.run(['imagemin:static']);
} else if (src) {
grunt.log.writeln("Optimizing in place");
var staticfiles = {};
staticfiles[src] = src;
grunt.config.set(['imagemin', 'staticinplace', 'files'], staticfiles);
grunt.task.run(['imagemin:staticinplace']);
} else {
grunt.fatal("Please specify a source and/or destination folder");
}
});
grunt.registerTask('help', "Displays help and usage", function(){
grunt.log.writeln("\ngrunt loptimize:images --src=/path/to/images/ [--dest=/path/to/destination/]\n");
grunt.log.writeln("Compresses images in `src` path and subsequently watches for new images, compressing them in turn. If a `dest` path is specified, optimize:images will send optimized images in destination folder. Otherwise, images will be optimized and overwritten in-place.");
});
// Javascript related Tasks
// grunt.registerTask('compress-js', ['clean:js','concat:dist', 'uglify:build']);
// Garbaging
// grunt.registerTask('cleanall', ['clean:images', 'clean:backup', 'clean:js']);
};