-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
171 lines (156 loc) · 4.72 KB
/
gulpfile.js
File metadata and controls
171 lines (156 loc) · 4.72 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
"use strict";
// http://pinkyjie.com/2015/03/24/refactor-your-gulpfile/
// 加载插件
var gulp = require('gulp'),
browserSync = require('browser-sync'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer'),
minifyCSS = require('gulp-minify-css'),
header = require('gulp-header'),
moment = require('moment'),
minifyHTML = require('gulp-minify-html'),
uglify = require('gulp-uglify'),
reload = browserSync.reload,
del = require('del'),
notify = require('gulp-notify'),
plumber = require('gulp-plumber'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
ejs = require('gulp-ejs');
// rename = require("gulp-rename");
// 配置路径
// https://github.com/mikestreety/gulp/blob/master/gulpfile.js
var path = {
css: {
src: './src/css/',
dist: './dist/css/'
},
sass: {
src: './src/sass/',
dist: './dist/sass/'
},
js: {
src: './src/js/',
dist: './dist/js/'
},
img: {
src: './src/img/',
dist: './dist/img/'
}
};
// 注释格式
// /*!
// * @project : pkg.name
// * @version : pkg.version
// * @author : pkg.author
// * @update :
// */
var pkg = require('./package.json');
var banner = '/*!' + '\n * @project : ' + pkg.name + '\n * @version : ' + pkg.version + '\n * @author : ' + pkg.author + '\n * @update : ' + moment().format('YYYY-MM-DD HH:mm:ss a') + '\n */\r';
// 启动服务
// 自动启动Chrome浏览器
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./",
directory: true
},
notify: false,
port: 8080,
open: "external",
browser: "google chrome"
});
});
// SASS编译
// https://github.com/sindresorhus/gulp-autoprefixer/issues/8
// http://kevingimbel.com/snippet-source-maps-gulp/
// http://whiskers.nukos.kitchen/2014/12/06/gulp-notify.html
// http://stackoverflow.com/questions/22787673/gulp-sass-error-with-notify
gulp.task('sass', function() {
return gulp.src(path.sass.src+'**/*.scss')
.pipe(plumber({
errorHandler: notify.onError("Error: <%= error.message %>")
}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(minifyCSS({"compatibility": "ie7"}))
.pipe(sourcemaps.write())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(autoprefixer())
.pipe(minifyCSS({"compatibility": "ie7"}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.css.src))
.pipe(reload({
stream: true
}))
.pipe(notify({
message: '✌️SASS任务编译通过'
}));
});
// 压缩HTML
gulp.task('minify-html', function() {
//var opts = {empty:true,cdata:true,comments:true,conditionals:true,spare:true,quotes:true};
var opts = {};
gulp.src('./src/*.html')
.pipe(minifyHTML(opts))
.pipe(gulp.dest('./dist/'))
});
// 样式压缩
gulp.task('minify-css', function() {
gulp.src('./src/css/*.css')
.pipe(minifyCSS({"compatibility": "ie7"}))
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest('./dist/css/'))
});
// 压缩脚本
gulp.task('compress', function() {
gulp.src('./src/js/*.js')
.pipe(uglify())
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest('./dist/js/'))
});
// 压缩图片
gulp.task('minify-img', function () {
return gulp.src('./src/img/*')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('./dist/img/'));
});
// Reload all Browsers
gulp.task('bs-reload', function() {
browserSync.reload();
});
// Clean file
gulp.task('clean', function() {
del.sync(['./dist/**']);
});
// ejs解析
gulp.task('ejs', function() {
return gulp.src('src/tpl/**/*.ejs')
.pipe(ejs({
msg: "Hello Gulp!"
}))
.pipe(gulp.dest('./src/'))
.pipe(reload({stream:true}))
});
// 默认任务
gulp.task('default', ['browser-sync'], function() {
gulp.watch(path.sass.src + '**/*.scss', ['sass']);
gulp.watch('./src/tpl/**/*.ejs', ['ejs']);
gulp.watch('./src/**/*.html').on('change', browserSync.reload);
gulp.watch('./src/**/*.js').on('change', browserSync.reload);
gulp.watch('./src/**/*.png').on('change', browserSync.reload);
gulp.watch('./src/**/*.jpg').on('change', browserSync.reload);
});
// 发布任务
gulp.task('build', ['clean', 'minify-css','minify-html','compress','minify-img']);