-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
141 lines (128 loc) · 4.9 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
const gulp = require('gulp');
const autoprefixer = require('gulp-autoprefixer');
const gulpRemoveHtml = require('gulp-remove-html');
const htmlmin = require('gulp-htmlmin');
// 标签清除
const removeEmptyLines = require('gulp-remove-empty-lines');
// 清除空白行
const minifycss = require('gulp-minify-css');
const uglify = require('gulp-uglify');
const imagemin = require('gulp-imagemin');
const rename = require('gulp-rename');
const clean = require('gulp-clean');
const concat = require('gulp-concat');
const cache = require('gulp-cache');
const less = require('gulp-less');
const gutil = require('gulp-util');
const htmlImport = require('gulp-html-import');
const sequence = require('gulp-sequence');
const connect = require('gulp-connect');
const fs = require('fs');
const openurl = require('openurl');
const config = JSON.parse(fs.readFileSync('config.json'));
const path = config.list[config.name].path;
// 项目操作
// npm install gulp -g
// html压缩
gulp.task('html', function () {
let options = {
removeComments: true, // 清除HTML注释
collapseWhitespace: false, // 压缩HTML
collapseBooleanAttributes: true, // 省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true, // 删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true, // 删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true, // 删除<style>和<link>的type="text/css"
minifyJS: true, // 压缩页面JS
minifyCSS: true // 压缩页面CSS
};
return gulp.src(path + '/*.html')
.pipe(htmlImport(path + '/components/'))
.pipe(gulpRemoveHtml()) // 清除特定标签
.pipe(removeEmptyLines({removeComments: true})) // 清除空白行
.pipe(htmlmin(options))
.pipe(gulp.dest(path + '/dist/'))
.pipe(connect.reload());
});
// 样式
gulp.task('css', function () {
return gulp.src(path + '/statics/css/style.less')
.pipe(less())
.on('error', function (err) {
gutil.log(gutil.colors.red('[Error]'), err.toString());
})
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest(path + '/dist/statics/css'))
.pipe(gulp.dest(path + '/statics/css'))
.pipe(connect.reload());
});
// 拷贝
gulp.task('copy', function () {
if (fs.exists(path + '/dist/statics')) {
return false;
}
return gulp.src('./statics/**/*')
.pipe(gulp.dest(path + '/dist/statics/'));
});
// 脚本
gulp.task('js', function () {
return gulp.src([path + '/statics/js/**/*.js', '!' + path + '/statics/js/**/*.min.js'])
.on('error', function (err) {
gutil.log(gutil.colors.red('[Error]'), err.toString());
})
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.on('error', function (err) {
gutil.log(gutil.colors.red('[Error]'), err.toString());
})
.pipe(gulp.dest(path + '/dist/statics/js'))
.pipe(gulp.dest(path + '/statics/js'))
.pipe(connect.reload());
});
// 图片
gulp.task('img', function () {
return gulp.src([path + '/statics/img/**/*.jpg', path + '/statics/img/**/*.png', path + '/statics/img/**/*.gif'])
.pipe(cache(imagemin({optimizationLevel: 3, progressive: true, interlaced: true})))
.on('error', function (err) {
gutil.log(gutil.colors.red('[Error]'), err.toString());
})
.pipe(gulp.dest(path + '/dist/statics/img'))
.pipe(connect.reload());
});
// 清理
gulp.task('clean', function () {
return gulp.src([path + '/dist/statics/css', path + '/dist/statics/js', path + '/dist/statics/img', path + '/dist/*.html'], {read: false})
.pipe(clean());
});
// 看守
gulp.task('watch', function () {
// 看守所有.less档
gulp.watch(path + '/statics/css/**/*.less', ['css']);
// 看守所有.js档
gulp.watch(path + '/statics/js/**/*.js', ['js']);
// 看守所有图片档
gulp.watch([path + '/statics/img/**/*.jpg', path + '/statics/img/**/*.png', path + '/statics/img/**/*.gif'], ['img']);
// 看守所有.html档
gulp.watch([path + '/*.html', path + '/components/*.html'], ['html']);
});
// 服务
gulp.task('server', function () {
connect.server({
root: [path + '/dist'], // 监控的目录
port: config.port, // 端口
livereload: true // 启用热加载
});
});
// 默认
gulp.task('default', sequence('clean', 'copy', 'css', 'js', 'img', 'watch', 'server', 'html', function () {
console.log('执行完成!');
openurl.open('http://localhost:' + config.port);
}));
// 构建
gulp.task('build', ['clean'], function () {
sequence('clean', 'css', 'js', 'img', function () {
console.log('执行完成!');
});
});