forked from pi-engine/pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
221 lines (201 loc) · 5.3 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
module.exports = function(grunt) {
var fs = require('fs');
var path = require('path');
var util = require('util');
var config = require('./Gruntfile-config.js');
function assetModuleCwd(name) {
return path.join('usr/module', name, 'asset');
}
function assetThemeCwd(name) {
return path.join('usr/theme', name, 'asset');
}
function assetModuleCwdBuild(name) {
return path.join('usr/module', name, 'asset/_build');
}
function assetThemeCwdBuild(name) {
return path.join('usr/theme', name, 'asset/_build');
}
function wwwAssetModuleCwd(name) {
return path.join('www/asset', 'module-' + name);
}
function wwwAssetThemeCwd(name) {
return path.join('www/asset', 'theme-' + name);
}
function vender(name) {
return path.join('www/static/vendor', name);
}
var modules = (function() {
var modules = [];
if (config.modules == 'all') {
//Auto read usr/module file name
fs.readdirSync('usr/module')
.forEach(function(path) {
if (~path.indexOf('.')) return;
modules.push(path);
});
} else if (util.isArray(config.modules)) {
modules = config.modules;
}
return modules;
})();
var themes = (function() {
var themes = [];
if (config.themes == 'all') {
//Auto read usr/module file name
fs.readdirSync('usr/theme')
.forEach(function(path) {
if (~path.indexOf('.')) return;
themes.push(path);
});
} else if (util.isArray(config.themes)) {
themes = config.themes;
}
return themes;
})();
/**
* 1. Copy for build
* 2. publish www/asset to module asset, it will be useful when you develop in windows.After
* you done module asset, you can use 'grunt back'.
*/
var copyOpts = (function() {
var ret = {
build: {
files: []
},
publishBack: {
files: []
}
};
modules.forEach(function(item) {
ret.build.files.push({
cwd: assetModuleCwd(item),
src: ['**'],
dest: assetModuleCwdBuild(item),
expand: true
});
ret.publishBack.files.push({
cwd: wwwAssetModuleCwd(item),
src: ['**'],
dest: assetModuleCwd(item),
expand: true
});
ret.publishBack.files.push({
cwd: path.join('www/public/', 'module-' + item),
src: ['**'],
dest: path.join('usr/module/', item, 'public'),
expand: true
});
});
themes.forEach(function(item) {
ret.build.files.push({
cwd: assetThemeCwd(item),
src: ['**'],
dest: assetThemeCwdBuild(item),
expand: true
});
ret.publishBack.files.push({
cwd: wwwAssetThemeCwd(item),
src: ['**'],
dest: assetThemeCwd(item),
expand: true
});
});
return ret;
})();
/**
* Clear modules or themes asset build files
*/
var cleanOpts = (function() {
var ret = {
pi: {
src: [vender('angular') + 'pi*.min.js', vender('angular') + 'i18n/*.min.js']
},
build: {}
};
var builds = [];
modules.forEach(function(item) {
builds.push(assetModuleCwdBuild(item));
});
themes.forEach(function(item) {
builds.push(assetThemeCwdBuild(item));
});
ret.build.src = builds;
return ret;
})();
var uglifyOpts = (function() {
var ret = {
options: {
//banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
pi: {
cwd: vender('angular'),
src: ['pi*.js', 'i18n/*.js'],
expand: true,
dest: vender('angular'),
ext: '.min.js'
},
modules: {
files: []
},
themes: {
files: []
}
};
modules.forEach(function(item) {
ret.modules.files.push({
cwd: assetModuleCwdBuild(item),
src: '**/*.js',
dest: assetModuleCwdBuild(item),
expand: true
});
});
themes.forEach(function(item) {
ret.themes.files.push({
cwd: assetThemeCwdBuild(item),
src: '**/*.js',
dest: assetThemeCwdBuild(item),
expand: true
});
});
return ret;
})();
var cssminOpts = (function() {
var list = [];
modules.forEach(function(item) {
list.push({
cwd: assetModuleCwdBuild(item),
src: '**/*.css',
dest: assetModuleCwdBuild(item),
expand: true
});
});
themes.forEach(function(item) {
list.push({
cwd: assetThemeCwdBuild(item),
src: '**/*.css',
dest: assetThemeCwdBuild(item),
expand: true
});
});
return list;
})();
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy: copyOpts,
uglify: uglifyOpts,
clean: cleanOpts,
cssmin: cssminOpts
});
// Load the plugin.
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
//Handler asset files for optimize loading
grunt.registerTask('default', ['clean', 'copy:build', 'uglify', 'cssmin']);
//Clear modules and themes asset build
grunt.registerTask('clear', ['clean:build']);
//For www/asset and www/public files to usr
grunt.registerTask('back', ['copy:publishBack']);
};