-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
127 lines (107 loc) · 2.77 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
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var plumber = require('gulp-plumber'); // 错误自启动
var browserify = require('browserify');
var del = require('del');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
// var stylish = require('jshint-stylish');
var buffer = require('vinyl-buffer');
var _ = require('lodash');
var TARGET = process.env.npm_lifecycle_event;
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var api = require('./api/api');
var srcRoot = './src';
var dist = './dist';
gulp.task('clean', function(cb) {
return del([
'app/tmp'
], cb);
});
gulp.task('fonts', function () {
return gulp.src(['node_modules/bootstrap/fonts/*', srcRoot+'/*.woff'])
.pipe(gulp.dest(dist + '/fonts'));
});
gulp.task('images', function () {
return gulp.src(srcRoot+'/**/**/imgs/*')
.pipe($.imagemin({
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(dist + '/imgs/'));
});
gulp.task('html', function() {
return gulp.src(srcRoot+'/index.html')
.pipe(plumber())
.pipe(gulp.dest(dist));
});
gulp.task('styles', function() {
return gulp.src(srcRoot+'/main.less')
.pipe($.less())
.pipe($.autoprefixer())
.pipe($.rename('bundle.css'))
.pipe(gulp.dest(dist))
.pipe(reload({ stream: true }));
});
var bundler = _.memoize(function(watch) {
var options = {debug: true};
if (watch) {
_.extend(options, watchify.args);
}
var b = browserify([srcRoot+'/plugins.js',srcRoot+'/main.js'], options);
if (watch) {
b = watchify(b);
}
return b;
});
var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
delete args[0].stream;
$.util.log.apply(null, args);
this.emit('end');
};
function bundle(cb, watch) {
return bundler(watch)
.bundle()
.on('error', handleErrors)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.sourcemaps.write('./maps'))
.pipe(gulp.dest(dist))
.on('end', cb)
.pipe(reload({ stream: true }));
}
gulp.task('scripts', function(cb) {
bundle(cb, true);
});
var reporter = 'spec';
gulp.task('build', [
'clean',
'fonts',
'images',
'html',
'styles',
'scripts'
//'test'
]);
gulp.task('watch', ['build'], function() {
browserSync.init({
server: {
baseDir: dist,
middleware: function(req, res, next) {
api(req, res, next);
}
}
});
reporter = 'dot';
bundler(true).on('update', function() {
gulp.start('scripts');
//gulp.start('test');
});
//gulp.watch('./test/!**!/!*.js', ['test']);
gulp.watch([srcRoot+'/main.less', srcRoot+'/**/*.less'], ['styles']);
gulp.watch([srcRoot+'/!*.html'], ['html']);
});
gulp.task('default', ['watch']);