-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
251 lines (229 loc) · 7.37 KB
/
gulpfile.js
File metadata and controls
251 lines (229 loc) · 7.37 KB
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
/**
gulp 配置文件
created by rx.tang
*/
var gulp = require('gulp'),
os = require('os'),
gutil = require('gulp-util'),
less = require('gulp-less'),
concat = require('gulp-concat'),
gulpOpen = require('gulp-open'),
uglify = require('gulp-uglify'),
cssmin = require('gulp-cssmin'),
md5 = require('gulp-md5-plus'),
fileinclude = require('gulp-file-include'),
clean = require('gulp-clean'),
spriter = require('gulp-css-spriter'),
base64 = require('gulp-css-base64');
//webpack = require('webpack'),
//webpackConfig = require('./webpack.config.js'),
var connect = require('gulp-connect');
var debug = require('gulp-debug');
var changed = require('gulp-changed');
var proxy = require('http-proxy-middleware');
var rewrite = require('express-urlrewrite')
// 开发
var src = {
path: 'src'
}
var host = {
path: 'build',
port: 3000,
html: 'index.html'
};
var dist = {
path: 'dist'
};
//mac chrome: "Google chrome",
/*var browser = os.platform() === 'linux' ? 'Google chrome' : (
os.platform() === 'darwin' ? 'Google chrome' : (
os.platform() === 'win32' ? 'chrome' : 'firefox'));
*/
// 清理开发和发布文件夹
gulp.task('clean', function (done) {
gulp.src([host.path, dist.path])
.pipe(clean())
.on('end', done);
});
//var pkg = require('./package.json');
//将图片拷贝到目标目录
gulp.task('copy:images', function (done) {
gulp.src([src.path + '/images/**/*'])
.pipe(changed(host.path + '/images'))
//.pipe(debug({title: '编译:'}))
.pipe(gulp.dest(host.path + '/images'))
.on('end', done);
});
gulp.task('copy:dist', function (done) {
gulp.src([src.path + '/dist/**/*'])
.pipe(changed(host.path + '/dist'))
//.pipe(debug({title: '编译:'}))
.pipe(gulp.dest(host.path + '/dist'))
.on('end', done);
});
// 合并Common
gulp.task('commonjs', function (done) {
gulp.src([src.path + '/js/common/*.js'])
.pipe(concat('common.js'))
.pipe(changed(host.path + '/js'))
.pipe(debug({title: '编译:'}))
// .pipe(uglify().on('error', gutil.log))
//.pipe(gulp.dest(host.path+'/js/'))
// .pipe(gulp.dest('build/js/'))
.pipe(gulp.dest(host.path + '/js'))
.on('end', done);
});
// 复制js类库
gulp.task('copyjs', ['commonjs'], function (done) {
gulp.src([src.path + '/js/*.js', src.path + '/js/**/*.js', "!" + src.path + '/js/common/*.js'])
.pipe(changed(host.path + '/js'))
.pipe(debug({title: '编译:'}))
// .pipe(uglify().on('error', gutil.log))
.pipe(gulp.dest(host.path + '/js'))
.on('end', done);
});
// 复制sound库
gulp.task('copysoundlib', function (done) {
gulp.src([src.path + '/fonts/**/**/*.*'])
.pipe(changed(host.path + '/fonts'))
.pipe(debug({title: '编译:'}))
.pipe(gulp.dest(host.path + '/fonts'))
.on('end', done);
});
// 复制css库
gulp.task('copycsslib', function (done) {
gulp.src([src.path + '/css/**/*'])
.pipe(changed(host.path + '/css'))
.pipe(debug({title: '编译:'}))
// .pipe(cssmin())
.pipe(gulp.dest(host.path + '/css'))
.on('end', done);
});
//用于在html文件中直接include文件
gulp.task('fileinclude', function (done) {
gulp.src([src.path + '/*.html', src.path + '/**/*.html', "!" + src.path + '/common/*.html'])
.pipe(changed(host.path))
.pipe(debug({title: '编译:'}))
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest(host.path))
.on('end', done);
});
// // 压缩css
// gulp.task('lessmin',['copycsslib'], function (done) {
// gulp.src( ['src/css/*.css'])
// //.pipe(less())
// //这里可以加css sprite 让每一个css合并为一个雪碧图
// //.pipe(spriter({}))
// //.pipe(concat('style.min.css'))
// .pipe(cssmin())
// .pipe(gulp.dest('dist/css/'))
// .on('end', done);
// });
//雪碧图操作,应该先拷贝图片并压缩合并css
gulp.task('sprite', ['copy:images', 'lessmin'], function (done) {
var timestamp = +new Date();
gulp.src('dist/css/style.min.css')
.pipe(spriter({
spriteSheet: 'dist/images/spritesheet' + timestamp + '.png',
pathToSpriteSheetFromCSS: '../images/spritesheet' + timestamp + '.png',
spritesmithOptions: {
padding: 10
}
}))
.pipe(base64())
.pipe(cssmin())
.pipe(gulp.dest('dist/css'))
.on('end', done);
});
gulp.task('watch', function (done) {
gulp.watch('src/**/**/*', ['copyjs', 'copycsslib', 'fileinclude'])
.on('end', done);
});
gulp.task('connect', function () {
console.log('connect------------');
connect.server({
root: host.path,
port: host.port,
livereload: true,
/*middleware: function (connect, opt) {
return [
proxy('/api', {
target: 'http://myxb.5173.com/',
changeOrigin: true,
}),
rewrite('/vue/*', '/vue/index.html')
]
}*/
});
});
//var myDevConfig = Object.create(webpackConfig);
//var devCompiler = webpack(myDevConfig);
//引用webpack对js进行操作
/*gulp.task("build-js", ['fileinclude'], function(callback) {
devCompiler.run(function(err, stats) {
if(err) throw new gutil.PluginError("webpack:build-js", err);
gutil.log("[webpack:build-js]", stats.toString({
colors: true
}));
callback();
});
});
*/
// 发布Front
gulp.task('deploy:fonts' , function (done) {
gulp.src([host.path + '/fonts/**/*'])
.pipe(gulp.dest(dist.path + '/fonts'))
.on('end', done);
});
// 发布图片
gulp.task('deploy:images', ['deploy:fonts'] , function (done) {
gulp.src([host.path + '/images/**/*'])
.pipe(gulp.dest(dist.path + '/images'))
.on('end', done);
});
// 发布html
gulp.task('deploy:html', ['deploy:images'] ,function (done) {
gulp.src([host.path + '/**/*.html'])
.pipe(gulp.dest(dist.path + '/'))
.on('end', done);
});
// 发布css
gulp.task('deploy:css', ['deploy:html'], function (done) {
gulp.src([host.path + '/css/**/*'])
.pipe(cssmin())
.pipe(md5(10, [dist.path + '/*.html', dist.path + '/**/*.html']))
.pipe(gulp.dest(dist.path + '/css'))
.on('end', done);
});
// 发布js
gulp.task('deploy:js', ['deploy:css'], function (done) {
gulp.src([host.path + '/js/**/*'])
.pipe(uglify().on('error', gutil.log))
.pipe(md5(10, [dist.path + '/*.html', dist.path + '/**/*.html']))
.pipe(gulp.dest(dist.path + '/js'))
.on('end', done);
});
//
////将js加上10位md5,并修改html中的引用路径,该动作依赖build-js
//gulp.task('md5:js', ['build-js'], function (done) {
// gulp.src('dist/js/*.js')
// .pipe(md5(10, 'dist/*.html'))
// .pipe(md5(10, 'dist/*/*.html'))
// .pipe(gulp.dest('dist/js'))
// .on('end', done);
//});
//
////将css加上10位md5,并修改html中的引用路径,该动作依赖sprite
//gulp.task('md5:css', ['sprite'], function (done) {
// gulp.src('dist/css/*.css')
// .pipe(md5(10, 'dist/*.html'))
// .pipe(gulp.dest('dist/css'))
// .on('end', done);
//});
//发布
gulp.task('default', ['deploy:js']);
//开发
gulp.task('dev', ['copy:images','copy:dist', 'copyjs', 'copysoundlib', 'copycsslib', 'fileinclude', 'connect', 'watch']);