-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
105 lines (84 loc) · 2.11 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
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var uglify = require('gulp-uglify');
var buffer = require('gulp-buffer');
var minifyCSS = require('gulp-minify-css');
var livereload = require('gulp-livereload');
var notify = require('gulp-notify');
var fs = require('fs');
// 环境变量
var env = 'prod'; // dev||prod
var live = livereload();
livereload.listen();
// 路径
var paths = {
main: './js/app.js',
css: './css/*.css',
destDir: 'build',
destCSS: 'build/css'
};
/**
*
*/
gulp.task('bundle-js', function() {
return browserify({
entries:[paths.main]
})
// 所有檔案合併為一,並指定要生成 source map
.bundle()
.on('error', function(err){
console.log('[錯誤]', err);
this.end();
gulp.src('').pipe(notify('✖ Bunlde Failed ✖'))
})
// 利用 vinyl-source-stream 幫檔案取名字
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./build'))
});
/**
* 压缩css
*/
gulp.task('minify-css', function() {
gulp.src(paths.css)
.pipe(minifyCSS(
{
noAdvanced: false,
keepBreaks:true,
cache: true // 這是 gulp 插件獨有的
}))
.pipe(gulp.dest(paths.destCSS))
});
/**
* 將 index.html 與 css/ 複製到 build/ 下面
* 才方便測試
*/
gulp.task('copy', function(){
return gulp.src(['./index.html'], {base: './'})
.pipe(gulp.dest(paths.destDir));
})
/**
* 监控 app/ 下所有 js, jsx, html, css 变化,重新编译
*/
//gulp.task('watch', function() {
// gulp.watch('./**/*', ['bundle-js', 'minify-css', 'copy']);
//});
/**
* livereload refresh
gulp.task( 'refresh', function(){
// console.log( '\nlivereload > refresh\n' );
setTimeout(function(){
live.changed('');
}, 500)
})
*/
//========================================================================
//
// 总的指令集
/**
* 初期让 default 跑 dev task,还有 build, deploy
*/
gulp.task('default', ['dev']);
gulp.task('dev', ['bundle-js', 'minify-css', 'copy']);