-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
120 lines (96 loc) · 2.56 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
'use strict';
// 引入 gulp
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
// 引入组件
var minifyCSS = require('gulp-minify-css');
var del = require('del');
var mocha = require('gulp-mocha');
var runSequence = require('run-sequence');
var argv = require('minimist')(process.argv.slice(2));
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var watch = false;
// clean
gulp.task('clean', function (cb) {
del(['./public/bundle/', './public/javascripts/**.*', './public/stylesheets/**.css'], cb);
});
// Bundle
gulp.task('bundle', function (cb) {
var started = false;
var config = require('./webpack.config.js');
var bundler = webpack(config);
var verbose = !!argv.verbose;
function bundle(err, stats) {
if (err) {
throw new $.util.PluginError('webpack', err);
}
console.log(stats.toString({
colors: $.util.colors.supportsColor,
hash: verbose,
version: verbose,
timings: verbose,
chunks: verbose,
chunkModules: verbose,
cached: verbose,
cachedAssets: verbose
}));
if (!started) {
started = true;
return cb();
}
}
if (watch) {
bundler.watch(200, bundle);
} else {
bundler.run(bundle);
}
});
// watch
gulp.task('watch', function (cb) {
watch = true;
runSequence('bundle', 'livereload', cb);
});
// liveload
gulp.task('livereload', function() {
var server = $.livereload();
gulp.watch(__dirname + '/public/', function() {
server.changed(file.path);
});
});
//// 合并
//gulp.task('concat', ['bundle'], function () {
// gulp.src('./public/javascripts/*.js')
// .pipe($.concat('app.js'))
// .pipe(gulp.dest('./public/javascripts'))
//});
// 压缩 js
gulp.task('minify-js', ['bundle'], function () {
gulp.src('./public/bundle/bundle.js')
.pipe($.sourcemaps.init())
.pipe($.uglify())
.pipe($.rename('app.min.js'))
.pipe($.sourcemaps.write('../javascripts'))
.pipe(gulp.dest('./public/javascripts'));
});
// 压缩 css
gulp.task('minify-css', function () {
gulp.src('./public/stylesheets/style.css')
.pipe(minifyCSS({keepBreaks: true}))
.pipe($.rename('style.min.css'))
.pipe(gulp.dest('./public/stylesheets/'))
});
// 测试
gulp.task('test', function () {
process.env.NODE_ENV = 'test';
return gulp.src('./server/model/test/*.js')
.pipe(mocha({}));
});
// 默认任务
gulp.task('default', function (cb) {
runSequence(['watch'], cb);
});
// build
gulp.task('build', function (cb) {
runSequence(['clean', 'bundle', 'minify-js', 'minify-css'], cb);
});