-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
185 lines (151 loc) · 4.47 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
var gulp = require('gulp'),
less = require('gulp-less'),
uglify = require('gulp-uglify'),
cleancss = require('gulp-clean-css'),
del = require('del'),
webpack = require('webpack-stream'),
rename = require('gulp-rename'),
htmlmin = require('gulp-htmlmin'),
template = require('gulp-compile-template'),
banner = require('gulp-banner'),
eslint = require('gulp-eslint'),
postcssClean = require('postcss-clean'),
named = require('vinyl-named'),
path = require('path'),
exec = require('child_process').execSync,
pkg = require('./package.json');
var src = 'src',
build = 'build',
dist = 'dist',
static = 'static',
compress = true;
var staticPath = path.join(build, static);
var comment = '/*! Kub Mobile JavaScript Components Library v<%= pkg.version%>.*/\n';
//clean
gulp.task('clean', function() {
del.sync(build);
del.sync(dist);
});
//less
gulp.task('less', function() {
var target = path.join(dist, 'css');
var stream = gulp.src([src + '/less/kub.less'])
.pipe(less())
.pipe(banner(comment, {
pkg: pkg
}))
//output unmin css
stream = stream.pipe(gulp.dest(target));
//min css
compress && (stream = stream.pipe(cleancss()));
//rename css
stream = stream.pipe(rename(function(path) {
path.basename += '.min';
}))
//output min css
stream = stream.pipe(gulp.dest(target));
//output build
stream = stream.pipe(rename(function(path) {
path.basename = 'index';
}))
return stream.pipe(gulp.dest(staticPath));
});
//js
gulp.task('js',['tpl'], function() {
var target = path.join(dist, 'js');
var stream = gulp.src([src + '/js/kub*.js'])
.pipe(named(function(file) {
var args = path.parse(file.path),
basenameParent = args.dir.replace(new RegExp('^' + path.resolve(src + '/js') + '\/?'), '');
return path.join(basenameParent, args.name);
}))
.pipe(webpack({
module: {
loaders: [{
test: /\.less$/,
loader: 'small-style?{"insertAt":"top"}!postcss!less'
}]
},
postcss: function() {
return [postcssClean()];
}
}))
.pipe(banner(comment, {
pkg: pkg
}))
//output unmin css
stream = stream.pipe(gulp.dest(target));
//min css
compress && (stream = stream.pipe(uglify()));
//rename css
stream = stream.pipe(rename(function(path) {
path.basename += '.min';
}))
//add banner
compress && (stream = stream.pipe(banner(comment, {
pkg: pkg
})))
//output min css
stream = stream.pipe(gulp.dest(target));
//output build
stream = stream.pipe(rename(function(path) {
path.basename = path.basename.replace('.min','').replace('kub','index');
}))
return stream.pipe(gulp.dest(staticPath));
});
//tpl
gulp.task('tpl', function() {
var target = path.join(src, 'js/tpl');
return gulp.src([src + '/js/tpl/html/*.html'])
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(template({
underscore: {
variable: 'data'
}
}))
.pipe(gulp.dest(target))
});
//eslint
gulp.task('eslint', function() {
var source = [path.join(src, 'js/**/*.js'), '!' + path.join(src, 'js/tpl/**/*.js')];
return gulp.src(source)
.pipe(eslint())
.pipe(eslint.format());
});
//只生成文档
//docker -i src/js -o build/pages/docs -x tpl
//监听文档并生成文档
//docker -i src/js -o build/pages/docs -x tpl -w
gulp.task('docs', function(cb) {
var target = path.join(build, 'pages/docs', 'v' + pkg.version),
source = path.join(src, 'js');
try {
exec('docker -i ' + source + ' -o ' + target + ' -x tpl');
cb();
} catch (e) {
console.log(e.message);
process.exit(0);
}
});
gulp.task('default', ['clean'], function() {
gulp.start('less', 'js');
});
//发布到CDN
gulp.task('publish', ['default'], function() {
gulp.start('docs');
});
gulp.task('watch', ['default'], function() {
compress = false;
gulp.watch([src + '/**/*.less'], function() {
gulp.start('less');
});
gulp.watch([src + '/**/*.js'], function() {
gulp.start('js');
});
gulp.watch([src + '/**/*.html'], function() {
gulp.start('tpl');
});
});