UNPKG

7.92 kBJavaScriptView Raw
1var path = require('path');
2var util = require('./utils.js');
3var confCommon = require('./config_common.js');
4
5var _grunt = function(grunt){
6 var basePath = path.resolve('.'); //module base path
7 var pkgConf = grunt.file.readJSON(basePath + '/package.json'); //read the config
8 var wowConfig = pkgConf.wow || {}; //wow builder config
9 var moduleName = wowConfig.name; //module name
10
11 util.fnFixEmptyPro(wowConfig, ['tasks']); //fix empty property
12
13 if (!moduleName) { //must set a module name in package.json
14 throw new Error('You must set a module name(wow.name) in package.json');
15 }
16
17 var modulePath = wowConfig.path || moduleName; //module dist path
18 var widgetPath = wowConfig.widget || 'src/widget'; //module's widget path
19 var serverbase = wowConfig.serverbase || '../webroot'; //local server's webroot
20 //the modules depends on
21 var depsModule = util.fnArrayUnique(wowConfig.deps ? ['common'].concat(wowConfig.deps) : ['common']);
22 //remote file receiver, will post file to this url when running remote task
23 var receiver = wowConfig.receiver || '';
24 var dist = wowConfig.dist || 'dist';
25 var confTasks = wowConfig.tasks; //module task conf
26 var confRemote = wowConfig.remote; //module remote conf
27 var confOnline = wowConfig.online; //module online conf
28 var confRouter = wowConfig.router || {}; //module router config
29 var confEntrance = wowConfig.entrance || []; //module router config
30 var confLayout = wowConfig.layout || '';
31 var confRewrite = wowConfig.rewrite || {};
32 var confPack = wowConfig.pack || {}; //package config
33
34 //module base dir
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 //load grunt tasks
50 util.fnGruntPluginLoader(grunt, [
51 moduleDir + '/node_modules/grunt-contrib-copy', //copy file
52 moduleDir + '/node_modules/grunt-contrib-less', //less compile
53 moduleDir + '/node_modules/grunt-contrib-csslint', //css lint
54 moduleDir + '/node_modules/grunt-contrib-jshint', //js hint
55 moduleDir + '/node_modules/grunt-contrib-requirejs', //js hint
56 moduleDir + '/node_modules/grunt-contrib-watch', //watch file
57 moduleDir + '/node_modules/grunt-contrib-cssmin', //css compress
58 moduleDir + '/node_modules/grunt-contrib-htmlmin', //html compress
59 moduleDir + '/node_modules/grunt-contrib-uglify', //js compress
60 moduleDir + '/node_modules/grunt-contrib-clean', //file clean
61 moduleDir + '/node_modules/grunt-dust', //dust compile
62 moduleDir + '/node_modules/grunt-string-replace', //string replace
63 moduleDir + '/node_modules/grunt-jsbeautifier', //code beautifier
64
65 moduleDir + '/src/grunt-wow-plugins/grunt-wow-wrap', //add amd wrap
66 moduleDir + '/src/grunt-wow-plugins/grunt-wow-spg', //compile wow template
67 moduleDir + '/src/grunt-wow-plugins/grunt-wow-httpserver', //compile wow template
68 moduleDir + '/src/grunt-wow-plugins/grunt-wow-urlmap', //generate the url map
69 moduleDir + '/src/grunt-wow-plugins/grunt-wow-urlfix', //url fix
70 moduleDir + '/src/grunt-wow-plugins/grunt-wow-rewrite', //generate the rewrite config file
71 moduleDir + '/src/grunt-wow-plugins/grunt-wow-httpupload', //http upload
72 moduleDir + '/src/grunt-wow-plugins/grunt-wow-pack', //file package
73 moduleDir + '/src/grunt-wow-plugins/grunt-wow-requiremap', //generate the require map file
74 moduleDir + '/src/grunt-wow-plugins/grunt-wow-md5', //generate file md5
75 moduleDir + '/src/grunt-wow-plugins/grunt-wow-addrequiremap', //add require map to entrance template
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 //grunt init
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
185module.exports = _grunt;
\No newline at end of file