-
Notifications
You must be signed in to change notification settings - Fork 5
/
Gruntfile.js
executable file
·96 lines (77 loc) · 2.48 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
var file = require(__dirname + '/lib/node_modules/nodame/file');
var path = require(__dirname + '/lib/node_modules/nodame/path');
var fs = require('fs');
module.exports = function (grunt) {
// Project Configuration
process.stdout.write('Reading config ... ');
var configPath = path.safe(path.app + '/configs');
var config = file.readGRUNT(configPath + '/assets.toml');
process.stdout.write('Done\n\n');
var filename = configPath + '/.assets';
var confWrite = copyConfig(config);
var jsonConfig = JSON.stringify(config);
process.stdout.write('Writing production assets configuration ... ');
var err = fs.writeFileSync(filename, jsonConfig);
if (err !== undefined) {
process.stdout.write('Failed\n\n');
return;
} else {
process.stdout.write('Done\n');
}
grunt.initConfig({
default: {
},
cssmin: {
main: {
files: confWrite.css
}
},
uglify: {
page: {
options: {
mangle: true,
compress: true
},
files: confWrite.js
}
}
});
//Load NPM tasks
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-uglify');
//Making grunt default to force in order not to break the project.
grunt.option('force', true);
//Default task(s).
grunt.registerTask('default', ['cssmin', 'uglify']);
};
var rename = function (filepath, min) {
var name;
var typeDir = path.extname(filepath).substr(1);
if (min) {
var re = /^.+[/]/;
name = path.safe(path.app + '/assets/min/' + filepath.replace(re, ''));
} else {
name = path.safe(path.app + '/assets/' + typeDir + '/' + filepath);
}
return name;
};
var copyConfig = function (config) {
var tmp = {};
for (var type in config) {
tmp[type] = [];
for (var typeIdx in config[type]) {
for (var key in config[type][typeIdx]) {
var _tmp = {};
var _key = rename(key, true);
_tmp[_key] = [];
for (var keyFile in config[type][typeIdx][key]) {
var _file = config[type][typeIdx][key][keyFile];
_file = rename(_file, false);
_tmp[_key].push(_file);
}
tmp[type].push(_tmp);
}
}
}
return tmp;
};