forked from fwon/gulp-webpack-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
144 lines (126 loc) · 4.13 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
/**
Gulpfile for gulp-webpack-demo
created by fwon
*/
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'),
connect = require('gulp-connect');
var host = {
path: 'dist/',
port: 3000,
html: 'index.html'
};
//mac chrome: "Google chrome",
var browser = os.platform() === 'linux' ? 'Google chrome' : (
os.platform() === 'darwin' ? 'Google chrome' : (
os.platform() === 'win32' ? 'chrome' : 'firefox'));
var pkg = require('./package.json');
//将图片拷贝到目标目录
gulp.task('copy:images', function (done) {
gulp.src(['src/images/**/*']).pipe(gulp.dest('dist/images')).on('end', done);
});
//压缩合并css, css中既有自己写的.less, 也有引入第三方库的.css
gulp.task('lessmin', function (done) {
gulp.src(['src/css/main.less', 'src/css/*.css'])
.pipe(less())
//这里可以加css sprite 让每一个css合并为一个雪碧图
//.pipe(spriter({}))
.pipe(concat('style.min.css'))
.pipe(gulp.dest('dist/css/'))
.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/app/*.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/app/*.html'))
.pipe(gulp.dest('dist/css'))
.on('end', done);
});
//用于在html文件中直接include文件
gulp.task('fileinclude', function (done) {
gulp.src(['src/app/*.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest('dist/app'))
.on('end', done);
// .pipe(connect.reload())
});
//雪碧图操作,应该先拷贝图片并压缩合并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('clean', function (done) {
gulp.src(['dist'])
.pipe(clean())
.on('end', done);
});
gulp.task('watch', function (done) {
gulp.watch('src/**/*', ['lessmin', 'build-js', 'fileinclude'])
.on('end', done);
});
gulp.task('connect', function () {
console.log('connect------------');
connect.server({
root: host.path,
port: host.port,
livereload: true
});
});
gulp.task('open', function (done) {
gulp.src('')
.pipe(gulpOpen({
app: browser,
uri: 'http://localhost:3000/app'
}))
.on('end', done);
});
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();
});
});
//发布
gulp.task('default', ['connect', 'fileinclude', 'md5:css', 'md5:js', 'open']);
//开发
gulp.task('dev', ['connect', 'copy:images', 'fileinclude', 'lessmin', 'build-js', 'watch', 'open']);