1 | var path = require('path');
|
2 | var util = require('./utils.js');
|
3 | var confCommon = require('./config_common.js');
|
4 |
|
5 | var _grunt = function(grunt){
|
6 | var basePath = path.resolve('.');
|
7 | var pkgConf = grunt.file.readJSON(basePath + '/package.json');
|
8 | var wowConfig = pkgConf.wow || {};
|
9 | var moduleName = wowConfig.name;
|
10 |
|
11 | util.fnFixEmptyPro(wowConfig, ['tasks']);
|
12 |
|
13 | if (!moduleName) {
|
14 | throw new Error('You must set a module name(wow.name) in package.json');
|
15 | }
|
16 |
|
17 | var modulePath = wowConfig.path || moduleName;
|
18 | var widgetPath = wowConfig.widget || 'src/widget';
|
19 | var serverbase = wowConfig.serverbase || '../webroot';
|
20 |
|
21 | var depsModule = util.fnArrayUnique(wowConfig.deps ? ['common'].concat(wowConfig.deps) : ['common']);
|
22 |
|
23 | var receiver = wowConfig.receiver || '';
|
24 | var dist = wowConfig.dist || 'dist';
|
25 | var confTasks = wowConfig.tasks;
|
26 | var confRemote = wowConfig.remote;
|
27 | var confOnline = wowConfig.online;
|
28 | var confRouter = wowConfig.router || {};
|
29 | var confEntrance = wowConfig.entrance || [];
|
30 | var confLayout = wowConfig.layout || '';
|
31 | var confRewrite = wowConfig.rewrite || {};
|
32 | var confPack = wowConfig.pack || {};
|
33 |
|
34 |
|
35 | var moduleDir = path.resolve(__dirname, '..');
|
36 |
|
37 | var gruntConfig = {
|
38 | name: moduleName,
|
39 | path: modulePath,
|
40 | dist: dist,
|
41 | widget: widgetPath,
|
42 | serverbase: serverbase,
|
43 | deps: depsModule,
|
44 | entrance: confEntrance
|
45 | };
|
46 |
|
47 | util.fnObjExtend(gruntConfig, confCommon);
|
48 |
|
49 |
|
50 | util.fnGruntPluginLoader(grunt, [
|
51 | moduleDir + '/node_modules/grunt-contrib-copy',
|
52 | moduleDir + '/node_modules/grunt-contrib-less',
|
53 | moduleDir + '/node_modules/grunt-contrib-csslint',
|
54 | moduleDir + '/node_modules/grunt-contrib-jshint',
|
55 | moduleDir + '/node_modules/grunt-contrib-requirejs',
|
56 | moduleDir + '/node_modules/grunt-contrib-watch',
|
57 | moduleDir + '/node_modules/grunt-contrib-cssmin',
|
58 | moduleDir + '/node_modules/grunt-contrib-htmlmin',
|
59 | moduleDir + '/node_modules/grunt-contrib-uglify',
|
60 | moduleDir + '/node_modules/grunt-contrib-clean',
|
61 | moduleDir + '/node_modules/grunt-dust',
|
62 | moduleDir + '/node_modules/grunt-string-replace',
|
63 | moduleDir + '/node_modules/grunt-jsbeautifier',
|
64 |
|
65 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-wrap',
|
66 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-spg',
|
67 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-httpserver',
|
68 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-urlmap',
|
69 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-urlfix',
|
70 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-rewrite',
|
71 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-httpupload',
|
72 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-pack',
|
73 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-requiremap',
|
74 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-md5',
|
75 | moduleDir + '/src/grunt-wow-plugins/grunt-wow-addrequiremap',
|
76 | ]);
|
77 |
|
78 | gruntConfig['wow-spg'].common.options.router = confRouter;
|
79 | gruntConfig['wow-spg'].common.options.layout = confLayout;
|
80 | var entrances = [];
|
81 | confEntrance.forEach(function(ent, index){
|
82 | entrances.push({
|
83 | tpl: ent,
|
84 | dist: path.join(dist, 'temp', ent)
|
85 | });
|
86 | });
|
87 | gruntConfig['wow-spg'].common.options.entrance = entrances;
|
88 |
|
89 | gruntConfig['wow-rewrite'].common.options.rewrite = confRewrite;
|
90 | gruntConfig['wow-rewrite'].common.options.target =
|
91 | dist + '/build/rewrite/' + modulePath.replace(/\//g, '_') + '_rewrite.json';
|
92 |
|
93 | gruntConfig['wow-pack'].common.files = (function(){
|
94 | var files = [];
|
95 | var cwd = './' + dist + '/build/static/' + modulePath;
|
96 |
|
97 | util.fnObjForin(confPack, function(item, key){
|
98 | var dest = path.join(cwd, key);
|
99 | var filsConf = {
|
100 | cwd: cwd,
|
101 | dest: dest,
|
102 | src: []
|
103 | };
|
104 |
|
105 | item = [].concat(item);
|
106 |
|
107 | item.forEach(function(source, index){
|
108 | filsConf.src.push(source.
|
109 | replace(/\.dust$/, '.js').
|
110 | replace(/\.less$/, '.css'));
|
111 | });
|
112 |
|
113 | files.push(filsConf);
|
114 | });
|
115 | return files;
|
116 | })();
|
117 |
|
118 | var tasks = [
|
119 | 'clean:common',
|
120 | 'copy:totemp', 'less:common', 'csslint:common', 'jshint:common',
|
121 | 'wow-wrap', 'wow-spg:common', 'wow-urlfix:tplPreFix', 'dust:common',
|
122 | 'copy:tobuild', 'string-replace:common', 'wow-urlmap:common', 'wow-pack:common',
|
123 | 'uglify:common', 'cssmin:common', 'htmlmin:common', 'wow-md5:common',
|
124 | 'wow-urlfix:html', 'wow-urlfix:css', 'wow-urlfix:js', 'wow-requiremap:common', 'wow-addrequiremap:common',
|
125 | 'wow-rewrite:common', 'wow-httpupload:common'];
|
126 | var tasksMap = {};
|
127 | util.fnObjForin(confTasks, function(item, key){
|
128 | tasksMap[key] = {
|
129 | tasks: [].concat(tasks),
|
130 | isWatching: false
|
131 | }
|
132 | util.fnObjForin(item, function(task, taskKey){
|
133 | if (taskKey === 'localserver' && task){
|
134 | tasksMap[key].tasks.push('copy:towebroot');
|
135 | }
|
136 |
|
137 | if (!gruntConfig[taskKey]){
|
138 | return;
|
139 | }
|
140 |
|
141 | gruntConfig[taskKey][key] = gruntConfig[taskKey][key] || {};
|
142 | util.fnObjExtend(gruntConfig[taskKey][key], gruntConfig[taskKey].common);
|
143 |
|
144 | if (task.src){
|
145 | var srcs = [].concat(task.src);
|
146 |
|
147 | gruntConfig[taskKey][key].files[0].src = srcs;
|
148 | }
|
149 | if (task.options){
|
150 | (!gruntConfig[taskKey][key].options) && (gruntConfig[taskKey][key].options = {});
|
151 | util.fnObjExtend(gruntConfig[taskKey][key].options, task.options);
|
152 | }
|
153 | if (task.files){
|
154 | gruntConfig[taskKey][key].files = task.files;
|
155 | }
|
156 |
|
157 | if (!tasksMap[key].isWatching && taskKey === 'watch'){
|
158 | tasksMap[key].isWatching = true;
|
159 | gruntConfig[taskKey][key].tasks = tasksMap[key].tasks;
|
160 | }
|
161 |
|
162 | var taskIndex = tasksMap[key].tasks.indexOf(taskKey + ':common');
|
163 |
|
164 | if (taskIndex > -1){
|
165 | if (task === false){
|
166 | tasksMap[key].tasks.splice(taskIndex, 1);
|
167 | } else{
|
168 | tasksMap[key].tasks[taskIndex] = taskKey + ':' + key;
|
169 | }
|
170 | }
|
171 | });
|
172 | });
|
173 |
|
174 | grunt.initConfig(gruntConfig);
|
175 | util.fnObjForin(tasksMap, function(item, key){
|
176 | grunt.registerTask(key, function(){
|
177 | grunt.task.run(item.tasks);
|
178 | if (item.isWatching){
|
179 | grunt.task.run(['watch' + ':' + key]);
|
180 | }
|
181 | });
|
182 | });
|
183 | };
|
184 |
|
185 | module.exports = _grunt; |
\ | No newline at end of file |