-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
171 lines (168 loc) · 6.46 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
(function () {
'use strict';
var gulp = require('gulp'),
$ = require('gulp-load-plugins')(),
merge = require('merge-stream'),
src = './src/',
paths = {
target: './dist/',
css: ['./css/**/*.*', '!./**/*.html'],
js: './js/**/*.js',
deviceJs: './js/device/',
static: ['./static/**/*.*', './data/**/*.*'],
src: src,
swig: src + 'swig/**/*.swig',
less: src + 'less/*.less',
md: src + 'markdown/**/*.md'
},
swigConfig = {
data: {
demain: '/web'
},
helpData: {
pageClass: 'help theme-gray no-js'
}
};
var jsMiniOpts = {mangle: {except: ['$', 'require', 'exports']}};
var mini = false;
var platforms = ['android', 'ios', 'browser'];
function extend(target) {
'use strict';
target = target || {};
var sources = [].slice.call(arguments, 1);
sources.forEach(function(source) {
if(source === null) {
return;
}
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
target[prop] = source[prop];
}
}
});
return target;
}
gulp.task('help', function () {
console.info('Usage: gulp <task>');
console.info(' gulp help: 显示帮助');
console.info(' gulp clean: 清理编译目录');
console.info(' gulp dev: 编译开发版本');
console.info(' gulp prod: 编译生产版本');
console.info(' gulp watch: 自动编译');
});
gulp.task('md', function () {
var header = [
"{% extends '../_layout_help.swig' %}",
'{% block pageContent %}',
'<div class="content inset">'].join('\n');
var dest = paths.target + 'page';
return gulp.src(paths.md)
.pipe($.newer({ dest: dest, ext: '.html'}))
.pipe($.frontMatter())
.pipe($.markdown())
.pipe($.header(header))
.pipe($.footer('</div>\n{% endblock %}'))
.pipe($.swig({
data: function (file) {
var data = file.frontMatter || file.data;
return extend({}, swigConfig.data, swigConfig.helpData, data);
},
defaults: {cache: false}
}))
.pipe($.if(mini, $.minifyHtml()))
.pipe(gulp.dest(dest));
});
gulp.task('less', function () {
var dest = paths.target + "css";
return gulp.src(paths.less)
.pipe($.newer({ dest: dest, ext: '.css'}))
.pipe($.less())
.pipe($.autoprefixer({
browsers: ['last 2 Chrome versions', 'iOS 7'],
cascade: false
}))
.pipe($.if(mini, $.cleanCss()))
.pipe(gulp.dest(dest));
});
gulp.task('swig', function () {
var dest = paths.target + 'page';
return gulp.src([paths.swig, '!./**/_*.*'])
.pipe($.newer({ dest: dest, ext: '.html'}))
.pipe($.frontMatter())
.pipe($.swig({
defaults: {
cache: false
},
data: function (file) {
var data = file.frontMatter || file.data;
return extend({}, swigConfig.data, data);
}
}))
.pipe($.if(mini, $.minifyHtml()))
.pipe(gulp.dest(dest));
});
gulp.task('static', function () {
var statics = gulp.src(paths.static, { base: '.' })
.pipe($.newer(paths.target))
.pipe(gulp.dest(paths.target));
var iconfontFilter = $.filter(['css/iconfont*.*ss', 'css/plugin/**/*.css'], {restore: true});
var lessFilter = $.filter(['**/*.less'], {restore: true});
var cssDest = paths.target + "css";
var css = gulp.src(paths.css)
.pipe(iconfontFilter).pipe($.newer('iconfont.css')).pipe($.concat('iconfont.less')).pipe(iconfontFilter.restore)
.pipe(lessFilter).pipe($.newer({ dest: cssDest, ext: '.css'})).pipe($.less()).pipe(lessFilter.restore)
.pipe($.newer(cssDest))
.pipe($.if(mini, $.if("*.css", $.cleanCss())))
.pipe(gulp.dest(cssDest));
var jsPluginFilter = $.filter(['**/plugin/*.js', '**/main.js'], {restore: true});
var jsDest = paths.target + "js";
var js = gulp.src([paths.js, '!' + paths.deviceJs + '**/*.js'])
.pipe(jsPluginFilter).pipe($.newer('core/main.js')).pipe($.concat('core/main.js')).pipe(jsPluginFilter.restore)
.pipe($.newer(jsDest))
.pipe($.if(mini, $.uglify(jsMiniOpts)))
.pipe(gulp.dest(jsDest));
var tasks = [statics, css, js];
for (var i in platforms) {
var platform = platforms[i];
var basePath = paths.deviceJs + platform;
var pluginJs = basePath + '/cordova_plugins.js';
var pluginTask = gulp.src([pluginJs, basePath + '/plugins/**/*.js'], {
base: '.'
})
.pipe($.newer(paths.target + pluginJs))
.pipe($.if(mini, $.uglify(jsMiniOpts)))
.pipe($.concat(pluginJs))
.pipe(gulp.dest(paths.target));
var cordovaTask = gulp.src([basePath + '/*.js', '!' + pluginJs], {
base: '.'
})
.pipe($.newer(paths.target))
.pipe($.if(mini, $.uglify(jsMiniOpts)))
.pipe(gulp.dest(paths.target));
tasks.push(pluginTask, cordovaTask);
}
return merge(tasks);
});
gulp.task('dev', ['md', 'swig', 'less', 'static']);
gulp.task('watch', ['dev'], function () {
gulp.watch(paths.less, ['less']);
gulp.watch(paths.swig, ['swig']);
gulp.watch(paths.static, ['static']);
gulp.watch(paths.js, ['static']);
gulp.watch(paths.md, ['md']);
gulp.watch(paths.css, ['static']);
$.livereload({ start: true });
gulp.watch(paths.target + '**/*', function (file) {
$.livereload.changed(file.path);
});
});
gulp.task('default', ['watch']);
gulp.task('switchProd', function (cb) {
mini = true;
cb && cb();
});
gulp.task('prod', ['switchProd', 'dev']);
gulp.task('clean', function () {
return gulp.src([paths.target]).pipe($.clean());
});
})();