forked from ifa6/amt-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
177 lines (155 loc) · 3.98 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
170
171
172
173
174
175
176
177
/**
* 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';
const $ = gulpLoadPlugins();
const isProduction = process.env.NODE_ENV === "production";
const AUTOPREFIXER_BROWSERS = [
'ie_mob >= 10',
'ff >= 40',
'chrome >= 40',
'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/css/fonts'
}
};
// JavaScript 格式校验
gulp.task('jshint', () => {
return gulp.src('app/js/**/*.js')
.pipe($.eslint())
.pipe($.eslint.format());
// .pipe($.eslint.failOnError());
});
// 图片优化
gulp.task('images', () => {
return gulp.src('app/i/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(paths.dist.i))
.pipe($.size({title: 'images'}));
});
// 清除 gulp-cache 缓存
gulp.task('clearCache', (done) => {
return $.cache.clearAll(done);
});
// 拷贝相关资源
gulp.task('copy', () => {
return gulp.src([
'app/*',
'!app/*.html',
'!app/js',
'!app/style',
'!app/i',
'node_modules/amazeui-touch/dist/amazeui.touch.min.css',
'node_modules/amazeui-touch/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('ratchicons') > -1) {
return paths.dist.fonts;
}
return paths.dist.base;
}))
.pipe($.size({title: 'copy'}));
});
// 编译 Sass,添加浏览器前缀
gulp.task('styles', () => {
return gulp.src(['app/style/app.scss'])
.pipe($.sass({
outputStyle: 'expanded'
}).on('error', $.sass.logError))
.pipe($.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);
}
var bundle = () => {
var 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', () => {
if (!isProduction) {
b.on('update', bundle).on('log', $.util.log);
}
return bundle();
});
// 压缩 HTML
gulp.task('html', () => {
return gulp.src('app/**/*.html')
.pipe($.minifyHtml())
.pipe($.replace(/\{\{__VERSION__\}\}/g, isProduction ? '.min' : ''))
.pipe(gulp.dest('dist'))
.pipe($.size({title: 'html'}));
});
// 洗刷刷
gulp.task('clean', () => {
return del(['dist/*', '!dist/.git'], {dot: true});
});
// 监视源文件变化自动cd编译
gulp.task('watch', () => {
gulp.watch('app/**/*.html', ['html']);
gulp.watch('app/style/**/*.scss', ['styles']);
gulp.watch('app/i/**/*', ['images']);
gulp.watch('app/js/**/*', ['jshint']);
});
// 启动预览服务,并监视 Dist 目录变化自动刷新浏览器
gulp.task('default', ['build', 'watch'], () => {
const bs = browserSync.create();
bs.init({
notify: false,
logPrefix: 'AMT',
server: ['dist']
});
gulp.watch(['dist/**/*'], bs.reload);
});
gulp.task('build', (cb) => {
runSequence('clean', ['styles', 'jshint', 'html', 'images', 'copy', 'browserify'], cb);
});