-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
342 lines (280 loc) · 7.95 KB
/
gulpfile.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Generated by CoffeeScript 1.10.0
/*
* gulp file 配置
* @author jackie Lin <[email protected]>
*/
'use strict';
var FS, _, argv, browserSync, buildAll, buildAllTpl, buildPath, config, deleteFile, getConfig, getExcludeList, gulp, gutil, initTask, md5, pack, packAll, packFilter, packTask, path, rename, requirejs, through2, tpl, tplTask, uglify, watch, watchTpl;
gulp = require('gulp');
argv = require('yargs').argv;
FS = require('q-io/fs');
path = require('path');
watch = require('gulp-watch');
config = require('./config');
rename = require('./rename');
requirejs = require('./require');
pack = require('./pack');
tpl = require('./tpl');
deleteFile = require('./deleteFile');
_ = require('lodash');
through2 = require('through2');
uglify = require('gulp-uglify');
md5 = require("gulp-md5");
gutil = require('gulp-util');
browserSync = require('browser-sync').create();
/*
* 获取配置文件对象
* @return Promise
*/
getConfig = function(sourePath) {
if (sourePath == null) {
sourePath = '';
}
config.setPath(sourePath);
return config();
};
/*
* 生成 roadMap 信息
* @example
* [{'widget/src': ***}, ***]
* @return {Array}
*/
getExcludeList = function(config) {
var packPath, roadMap, sourePath;
if (config == null) {
config = {};
}
roadMap = config.roadMap || [];
packPath = config.pack || '';
sourePath = config.sourePath || '';
return _.chain(roadMap).map(function(item) {
var basePath;
basePath = item.path;
item.exclude = (item.exclude || []).map(function(v, k) {
return path.join(sourePath, packPath, basePath, v);
});
return item;
}).pluck('exclude').flatten().value();
};
/*
* 设置 packTask 过滤信息
*/
packFilter = function(config) {
if (config == null) {
config = {};
}
if (!exports.packFilter && config.roadMap) {
exports.packFilter = getExcludeList(config);
}
return exports.packFilter;
};
/*
* js 打包任务
* @param {String} filePath 文件目录所在路径
*/
packTask = function(filePath, filter) {
var srcPath;
srcPath = path.dirname(filePath);
return gulp.src(filePath).pipe(pack.combineFile(filter)).pipe(deleteFile(path.join(srcPath, './dist'))).pipe(gulp.dest(path.join(srcPath, './dist'))).pipe(requirejs.rebuildDev(function(baseName) {
return baseName.split('.')[0];
})).pipe(uglify()).pipe(rename(function(file) {
file.path = file.path.replace('all.js', 'all.min.js');
return file;
})).pipe(md5(6)).pipe(gulp.dest(path.join(srcPath, './dist'))).pipe(requirejs.rebuild(function(baseName) {
return baseName.split('.')[0];
}));
};
/*
* 编译模板文件
*/
tplTask = function(filePath, packPath) {
var baseName;
baseName = path.basename(filePath);
return gulp.src(filePath).pipe(deleteFile(path.join(packPath, './tpl', baseName))).pipe(tpl.build()).pipe(md5(6)).pipe(gulp.dest(path.join(packPath, './tpl'))).pipe(requirejs.rebuild(function(baseName) {
return 'tpl/' + baseName.split('.')[0];
}));
};
/*
* 构建对应的目录信息
*/
buildPath = function(srcPath, done) {
return FS.stat(srcPath).then(function(stat) {
if (stat.isDirectory() === false) {
throw new Error('%s srcPath is null', srcPath);
}
return FS.list(srcPath);
}).then(function(list) {
return list.forEach(function(v) {
return (function(v) {
if (v.indexOf('.') !== 0) {
return done(v);
}
})(v);
});
});
};
/*
* 打包所有 js 目录
*/
packAll = function(config) {
var filter, srcPath;
srcPath = path.join(config.sourePath, config.pack);
filter = packFilter(config);
return buildPath(srcPath, function(v) {
return packTask(path.join(srcPath, v, 'src'), filter);
});
};
/*
* 打包所有的模板文件
*/
buildAllTpl = function(config) {
var packPath, srcPath;
srcPath = path.join(config.sourePath, config.tpl);
packPath = path.join(config.sourePath, config.pack, '../');
return buildPath(srcPath, function(v) {
return tplTask(path.join(srcPath, v), packPath);
});
};
/*
* 监听模板的变化
* @param {String} tplPath 模板路径
* @param {String} tplBuildPath 模板保存路径
* @param {Boolean} [browserReload] 是否刷新浏览器
*/
watchTpl = function(tplPath, tplBuildPath, browserReload) {
if (browserReload == null) {
browserReload = false;
}
return watch(tplPath, function(event) {
var filePath;
gutil.log(gutil.colors.green('build tpl: ', event.path));
filePath = path.dirname(event.path);
return tplTask(filePath, tplBuildPath);
});
};
/*
* 初始化任务
*/
initTask = function(sourePath) {
var srcPath;
srcPath = sourePath ? path.join(sourePath, 'gis.json') : '';
return getConfig(srcPath).then((function(_this) {
return function(config) {
config.sourePath = sourePath;
requirejs.setConfig(config);
pack.setConfig(config);
return config;
};
})(this));
};
/*
* 重新编译所有文件
*/
buildAll = function(config) {
packAll(config);
if (config.tplBuild) {
return buildAllTpl(config);
}
};
/*
* 执行初始化任务
* @example
* gulp init --path=path
*/
gulp.task('init', function() {
var sourePath;
sourePath = argv.path;
if (!sourePath) {
throw new Error('init task: path is null');
}
return getConfig().then(function(config) {
config.sourePath = sourePath;
requirejs.setConfig(config);
return FS.makeTree(path.join(sourePath, config.pack)).then(function() {
return FS.makeTree(path.join(sourePath, config.sass || ''));
}).then(function() {
return FS.makeTree(path.join(sourePath, config.tpl));
}).then(function() {
return FS.copy(path.join(__dirname, 'gis.json'), path.join(sourePath, 'gis.json'));
}).then(function() {
return requirejs.initConfigFile();
});
});
});
gulp.task('rebuild', function() {
var sourePath;
sourePath = argv.path;
if (!sourePath) {
throw new Error('init task: path is null');
}
return initTask(sourePath).then(function(config) {
requirejs.initConfigFile();
return buildAll(config);
});
});
/*
* 修改测试环境发布地址
*/
gulp.task('test', function() {
var sourePath;
sourePath = argv.path;
if (!sourePath) {
throw new Error('init task: path is null');
}
return initTask(sourePath).then(function(config) {
return requirejs.changeBaseUrl(config.requireOption.testUrl || '');
});
});
/*
* 修改正式环境发布地址
*/
gulp.task('product', function() {
var sourePath;
sourePath = argv.path;
if (!sourePath) {
throw new Error('init task: path is null');
}
return initTask(sourePath).then(function(config) {
return requirejs.changeBaseUrl(config.requireOption.produceUrl || '');
});
});
gulp.task('watch', function() {
var sourePath;
sourePath = argv.path;
if (!sourePath) {
throw new Error('init task: path is null');
}
return initTask(sourePath).then((function(_this) {
return function(config) {
var filter, packPath, tplBuildPath, tplPath;
gutil.log(gutil.colors.blue('项目正在进行初始化编译..........'));
requirejs.initConfigFile();
buildAll(config);
packPath = path.join(sourePath, config.pack, '**/src/*.js');
filter = packFilter(config);
if (config.browserReload) {
browserSync.init({
server: {
baseDir: sourePath
}
});
}
watch(packPath, function(event) {
var filePath;
gutil.log(gutil.colors.yellow('build pack: ', event.path));
filePath = path.dirname(event.path);
return packTask(filePath, filter);
});
if (config.tplBuild) {
tplPath = path.join(sourePath, config.tpl, '**/*.html');
tplBuildPath = path.join(sourePath, config.pack, '../');
if (!config.browserReload) {
watchTpl(tplPath, tplBuildPath, config.browserReload);
}
if (config.browserReload) {
return watchTpl(tplPath, tplBuildPath, config.browserReload).on('change', browserSync.reload);
}
}
};
})(this));
});