-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
207 lines (179 loc) · 4.64 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
var pkg = require('./package.json');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var header = require('gulp-header');
var del = require('del');
var gulpif = require('gulp-if');
var minimist = require('minimist');
var gulpOpen = require('gulp-open');
var os = require('os');
var connect = require('gulp-connect');
var watch = require('gulp-watch');
//获取参数
var argv = require('minimist')(process.argv.slice(2), {
default: {
ver: 'all'
}
})
// host配置
,
host = {
path: '',
port: 3000,
html: 'index.html'
}
// 配置open
,
browser = os.platform() === 'linux' ? 'Google chrome' : (
os.platform() === 'darwin' ? 'Google chrome' : (
os.platform() === 'win32' ? 'chrome' : 'firefox'))
//注释
, note = [
'/**\n* <%= pkg.name %>-v<%= pkg.version %> <%= pkg.license %> License By \n* github: <%= pkg.homepage %>\n**/\n<%= js %>', {
pkg: pkg,
// js: "var modsConfig = " + JSON.stringify(config) + ";"
js: ""
}
]
//模块
// , mods = config.moduleType
//任务
, task = {
//压缩js模块
minjs: (ver) => {
ver = ver === 'open';
//可指定模块压缩,eg:gulp minjs --mod
var mod = argv.mod ? function() {
return '(' + argv.mod.replace(/,/g, '|') + ')';
}() : '',
src =[
'./node_modules/jquery/dist/jquery.js',
'./node_modules/html2canvas/dist/html2canvas.js',
'./node_modules/webuploader/dist/webuploader.html5only.js',
'./src/**/viewEdit.js',
'./src/**/module/*.js'
],
dir = ver ? 'dist' : 'build';
gulp.src(src)
//合并输出为app.js
.pipe(concat('ViewEdit.js', {
newLine: ''
}))
.pipe(uglify())
.pipe(header.apply(null, note))
.pipe(gulp.dest('./' + dir));
}
,
alljs: function(ver) {
ver = ver === 'open';
var src = [
// './node_modules/jquery/dist/jquery.js',
'./node_modules/html2canvas/dist/html2canvas.js',
'./node_modules/webuploader/dist/webuploader.html5only.js',
'./src/**/viewEdit.js',
'./src/**/module/*.js'
],
dir = ver ? 'dist' : 'build';
return gulp.src(src)
//合并输出为app.js
.pipe(concat('ViewEdit.js', {
newLine: ''
}))
.pipe(header.apply(null, note))
.pipe(gulp.dest('./' + dir + ''));
}
//压缩css文件
,
mincss: function(ver) {
ver = ver === 'open';
var src = ['./src/**/*.css'],
dir = ver ? 'dist/style' : 'build/style',
noteNew = JSON.parse(JSON.stringify(note));
noteNew[1].js = '';
return gulp.src(src)
.pipe(concat('ViewEdit.css', {
newLine: ''
}))
.pipe(minify({
compatibility: 'ie7'
})).pipe(header.apply(null, noteNew))
.pipe(gulp.dest('./' + dir + '/'));
}
//复制iconfont文件
,
font: function(ver) {
ver = ver === 'open';
var dir = ver ? 'dist' : 'build';
return gulp.src('./src/font/*')
.pipe(rename({}))
.pipe(gulp.dest('./' + dir + '/font'));
}
//复制组件可能所需的非css和js资源
,
mv: function(ver) {
ver = ver === 'open';
var src = ['./src/**/*.{png,jpg,gif,html,mp3,json,svg}'],
dir = ver ? 'dist' : 'build';
gulp.src(src).pipe(rename({}))
.pipe(gulp.dest('./' + dir));
}
// 监听变化
,
watch: function(ver) {
return gulp.watch(['./src/**/*.{js,css}'], function() {
for (var key in task) {
if (key != "watch" && key != "open" && key != "minjs" ) {
task[key]();
}
}
});
}
// 预览
,
open: function(ver) {
connect.server({
root: host.path,
port: host.port,
livereload: true
});
gulp.src('')
.pipe(gulpOpen({
app: browser,
uri: 'http://localhost:3000/demo'
}))
this.watch();
}
};
//清理
gulp.task('clear', function(cb) {
return del(['./build/*'], cb);
});
gulp.task('clearRelease', function(cb) {
return del(['./dist/*'], cb);
});
//压缩版本
gulp.task('default', ['clearRelease'], function() { //命令:gulp
for (var key in task) {
if (key != "watch" && key != "open" && key != "alljs") {
task[key]('open');
}
}
});
gulp.task('local', function() { //命令:监听
for (var key in task) {
if ( key == "open" ) {
task[key]();
}
}
});
//完整任务
gulp.task('all', ['clear'], function() { //命令:gulp all,过滤lwj:gulp all --open
for (var key in task) {
if (key != "watch" && key != "open" && key != "minjs") {
task[key]();
}
}
});