This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
138 lines (133 loc) · 4.6 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
// gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
//如果需要可以生成sourcemap
var sourcemaps = require('gulp-sourcemaps');
//css压缩
var minifycss = require('gulp-clean-css');
//重命名
var rename = require('gulp-rename');
//gulp监视器
var watch = require('gulp-watch');
//js检测
var jshint = require('gulp-jshint');
//util
var gutil = require('gulp-util');
//浏览器同步
var browserSync = require('browser-sync').create();
//简化reload
var reload = browserSync.reload;
// es6->es5
var babel = require("gulp-babel");
//html引入
var fileinclude = require('gulp-file-include');
//hash
var rev = require('gulp-rev-append');
//js压缩
var uglify = require('gulp-uglify');
//增量更新
var changed = require('gulp-changed');
var jshintConfig = { "undef": false, "esnext": true, "predef": ["$", "window", "jQuery"] };
gulp.task('default', ['server']);
gulp.task('sass', function() {
return gulp.src("src/sass/main.scss")
.pipe(sass({ style: 'expanded' }))
.on('error', function(err) {
gutil.log('sass Error!', err.message);
this.emit('end');
})
.pipe(gulp.dest("dest/css"))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss({ keepSpecialComments: "*", processImportFrom: ['!fonts.googleapis.com'] }))
.pipe(gulp.dest("dest/css"))
.pipe(reload({ stream: true }));
});
gulp.task('css', function() {
return gulp.src("src/css/**/*.css")
.pipe(sass({ style: 'expanded' }))
.pipe(gulp.dest("dest/css"))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss({ keepSpecialComments: "*", processImportFrom: ['!fonts.googleapis.com'] }))
.pipe(gulp.dest("dest/css"))
.pipe(reload({ stream: true }));
});
gulp.task('html', function() {
gulp.src(["src/html/**/*.html", "!src/html/public"])
.pipe(fileinclude())
.on('error', function(err) {
gutil.log('html Error!', err.message);
this.emit('end');
})
.pipe(gulp.dest("dest"))
.pipe(rev())
.on('error', function(err) {
gutil.log('html Error!', err.message);
this.emit('end');
})
.pipe(gulp.dest("dest"))
.pipe(reload({ stream: true }));
});
gulp.task('script', function() {
return gulp.src("src/js/**/*.js")
.pipe(changed("dest/js"))
.pipe(gulp.dest("dest/js"))
.pipe(babel({
presets: ['es2015']
}))
.on('error', function(err) {
gutil.log('js Error!', err.message);
this.emit('end');
})
// .pipe(jshint(jshintConfig)) //{"esnext" : true}))
// .pipe(jshint.reporter('default'))
.pipe(uglify())
.on('error', function(err) {
gutil.log('js Error!', err.message);
this.emit('end');
})
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest("dest/js"))
.pipe(reload({ stream: true }));
});
// 浏览器重载
gulp.task('script-watch', ['script'], reload);
// gulp.task('html-watch', ['html'], reload);
// 静态服务器
gulp.task('server', ['html', 'sass', 'script', 'css'], function() {
// 从这个项目的根目录启动服务器
browserSync.init({
proxy:"localhost:4000",
//禁止网络模式
online: false
//静止ui模式
// ui: false
});
// 添加 browserSync.reload 到任务队列里
// 所有的浏览器重载后任务完成。
watch("src/sass/**/*.scss", function() { //监听所有sass
gulp.start('sass'); //出现修改、立马执行sass任务
});
watch("src/js/**/*.js", function() { //监听所有js
gulp.start('script-watch'); //出现修改、立马执行js任务
});
watch("src/html/**/*.html", function() { //监听所有html
gulp.start('html').on('change', reload); //出现修改、立马执行html任务
});
watch("src/css/**/*.css", function() { //监听所有css
gulp.start('css'); //出现修改、立马执行css任务
});
});
gulp.task('watch', function() {
watch("src/sass/**/*.scss", function() { //监听所有sass
gulp.start('sass'); //出现修改、立马执行sass任务
});
watch("src/js/**/*.js", function() { //监听所有js
gulp.start('script-watch'); //出现修改、立马执行js任务
});
watch("src/html/**/*.html", function() { //监听所有html
gulp.start('html').on('change', reload); //出现修改、立马执行html任务
});
watch("src/css/**/*.css", function() { //监听所有css
gulp.start('css'); //出现修改、立马执行css任务
});
});