UNPKG

2.44 kBJavaScriptView Raw
1/**
2 * 处理脚本任务
3 * @author lukezhu
4 * @date 2016-03-24
5 */
6
7var minimatch = require('minimatch');
8var combiner = require('stream-combiner2');
9
10function minimatchTester(rules, path) {
11 var match = false
12 if (Array.isArray(rules)) {
13 rules.map(function(rule) {
14 if (minimatch(path, rule, { nonegate: true })) {
15 match = true
16 }
17 })
18 } else {
19 match = minimatch(rules, path, { nonegate: true })
20 }
21
22 return match
23}
24
25module.exports = function(gulp, $, conf, browserSync) {
26 var gs = $.sync(gulp);
27 // 开发任务 gulp --dev
28 gulp.task('serve', gs.sync([
29 'clean:tmp',
30 ['scripts', 'styles', 'template'],
31 ['rollup']
32 ]), function() {
33 // 监听样式文件变化
34 $.watch(conf.parsePwd(conf.stylesFiles4Watch), function(evt) {
35 conf.gwChangeHandler(evt);
36 process.env.styleFile = minimatchTester(conf.parsePwd(conf.styleFiles), evt.path) ? evt.path : '';
37 gulp.start('styles');
38 });
39
40 // 监听脚本文件变化
41 $.watch(conf.parsePwd(conf.scriptFiles4Watch), (evt) => {
42 conf.gwChangeHandler(evt)
43 gulp.start(gs.sync(['scripts', 'rollup']));
44 });
45 // $.watch(conf.parsePwd(conf.scriptFiles4Watch), function(evt) {
46 // conf.gwChangeHandler(evt);
47 // gulp.start('scripts');
48 // });
49
50 // 监听模版文件变化
51 $.watch(conf.parsePwd(conf.tmplFiles4Watch), function(evt) {
52 conf.gwChangeHandler(evt);
53 gulp.start('template');
54 });
55
56 // 启动本地调试服务器
57 browserSync.init({
58 ui: false,
59 open: "external",
60 startPath: conf.serve.open,
61 port: conf.serve.port,
62 notify: false,
63 ghostMode: false,
64 server: {
65 baseDir: conf.parsePwd([conf.tmp, conf.app]),
66 index: conf.serve.index,
67 // Enable CORS
68 middleware: function (req, res, next) {
69 res.setHeader('Access-Control-Allow-Origin', '*');
70 next();
71 }
72 }
73 })
74 });
75
76 // 发布任务 gulp build
77 gulp.task('build', gs.sync([
78 ['clean:tmp', 'clean:dist'],
79 ['scripts', 'styles', 'template'],
80 ['rollup'],
81 'copy:tmp',
82 'copy:dist'
83 ]));
84}