forked from arcsecw/auto_writing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
169 lines (147 loc) · 3.84 KB
/
gulpfile.babel.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Amaze UI Starter Kit
*/
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import del from 'del';
import runSequence from 'run-sequence';
import browserSync from 'browser-sync';
import browserify from 'browserify';
import watchify from 'watchify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import autoprefixer from 'autoprefixer';
const $ = gulpLoadPlugins();
const reload = browserSync.reload;
const isProduction = process.env.NODE_ENV === 'production';
const AUTOPREFIXER_BROWSERS = [
'ie >= 9',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 2.3',
'bb >= 10'
];
const paths = {
dist: {
base: 'dist',
js: 'dist/js',
css: 'dist/css',
i: 'dist/i',
fonts: 'dist/fonts'
}
};
// 图片优化
gulp.task('images', function() {
return gulp.src('app/i/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(paths.dist.i))
.pipe($.size({title: 'images'}));
});
// 拷贝相关资源
gulp.task('copy', function() {
return gulp.src([
'app/*',
'!app/*.html',
'!app/js',
'!app/less',
'!app/i',
'node_modules/amazeui/dist/css/amazeui.min.css',
'node_modules/amazeui/dist/fonts/*'
], {
dot: true
}).pipe(gulp.dest(function(file) {
var filePath = file.path.toLowerCase();
if (filePath.indexOf('.css') > -1) {
return paths.dist.css;
} else if (filePath.indexOf('fontawesome') > -1) {
return paths.dist.fonts;
}
return paths.dist.base;
}))
.pipe($.size({title: 'copy'}));
});
// 编译 Less,添加浏览器前缀
gulp.task('styles', function() {
return gulp.src(['app/less/app.less'])
.pipe($.less())
.pipe($.postcss([autoprefixer({browsers: AUTOPREFIXER_BROWSERS})]))
.pipe(gulp.dest('dist/css'))
.pipe($.csso())
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest('dist/css'))
.pipe($.size({title: 'styles'}));
});
// 打包 Common JS 模块
var b = browserify({
cache: {},
packageCache: {},
entries: ['./app/js/app.js'],
debug: !isProduction,
transform: ['babelify']
});
if (!isProduction) {
b = watchify(b);
}
// 如果不想把 React 打包进去,可以把下面一行注释去掉
// b.transform('browserify-shim', {global: true});
var bundle = function() {
const s = (
b.bundle()
.on('error', $.util.log.bind($.util, 'Browserify Error'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulp.dest(paths.dist.js))
.pipe($.size({title: 'script'}))
);
return !isProduction ? s : s.pipe($.uglify())
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest(paths.dist.js))
.pipe($.size({
title: 'script minify'
}));
};
gulp.task('browserify', function() {
if (!isProduction) {
b.on('update', bundle).on('log', $.util.log);
}
return bundle();
});
// 压缩 HTML
gulp.task('html', function() {
return gulp.src('app/**/*.html')
.pipe($.minifyHtml())
.pipe($.replace(/\{\{__VERSION__\}\}/g, isProduction ? '.min' : ''))
.pipe(gulp.dest('dist'))
.pipe($.size({title: 'html'}));
});
// 洗刷刷
gulp.task('clean', function() {
return del(['dist/*', '!dist/.git'], {dot: true});
});
// 构建任务
gulp.task('build', function(cb) {
runSequence('clean', ['styles', 'html', 'images', 'copy', 'browserify'], cb);
});
// 监视源文件变化自动cd编译
gulp.task('watch', function() {
gulp.watch('app/**/*.html', ['html']);
gulp.watch('app/less/**/*less', ['styles']);
gulp.watch('app/i/**/*', ['images']);
});
// 默认任务
// 启动预览服务,并监视 Dist 目录变化自动刷新浏览器
gulp.task('default', ['build', 'watch'], function() {
browserSync({
notify: false,
logPrefix: 'ASK',
server: 'dist'
});
gulp.watch(['dist/**/*'], reload);
});