-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
128 lines (106 loc) · 3.74 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
128
'user strict';
var gulp = require('gulp'),
less = require('gulp-less'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
minifycss = require('gulp-minify-css'),
jsonminify = require('gulp-jsonminify'),
htmlmin = require('gulp-htmlmin'),
clean = require('gulp-clean'),
rename = require('gulp-rename'),
watch = require('gulp-watch'),
replace = require('gulp-replace'),
runSequence = require('run-sequence'),
browserSync = require('browser-sync'),
filter = require('gulp-filter');
var htmlSrc = ['app/**/*.html', '!app/index.html', '!app/lib/**/*.html', '!app/lib/*.html'];
var jsSrc = ['app/**/*.js', '!app/lib/**/*.js', '!app/assets/**/*.js'];
var cssSrc = ['app/assets/**/*.css', '!app/lib/**/*.css'];
var imgSrc = ['app/assets/img/*', 'app/assets/img/**/*'];
var fontSrc = ['app/assets/fonts/**/*'];
var jsonSrc = ['app/translate/*.json', 'app/translate/**/*.json'];
var libSrc = ['app/lib/**/*'];
var indexSrc = 'app/index.html';
var faviconSrc = 'app/favicon.ico';
var debugPath = 'app/';
var buildPath = 'dist/';
gulp.task('moveFavicon', function() {
return gulp.src(faviconSrc)
.pipe(gulp.dest(buildPath));
});
gulp.task('createIndex', ['moveFavicon'], function() {
return gulp.src(indexSrc)
.pipe(replace(/\.js/g, '.min.js'))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(buildPath))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compressHTML', function() {
return gulp.src(htmlSrc)
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(buildPath))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compressJS', function() {
var filterIndex = filter(['**/app.config.js', '**/app.module.js', '**/app.route.oclazy.js'], {restore: true});
return gulp.src(jsSrc)
.on('error', function(err) {//语法检查,打印错误
console.log('Error: ', err.message);
})
.pipe(filterIndex)
.pipe(rename({suffix: '.min'}))
.pipe(filterIndex.restore)
.pipe(uglify())//压缩
.pipe(gulp.dest(buildPath))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compressCSS', function() {
return gulp.src(cssSrc)
//.pipe(concat('index.min.css')) 为了使用oclazyload不整合
.pipe(minifycss())
.pipe(gulp.dest(buildPath + 'assets'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compressJSON', function () {
return gulp.src(jsonSrc)
.pipe(jsonminify())
.pipe(gulp.dest(buildPath + 'translate'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('copyImg', function() {
return gulp.src(imgSrc)
.pipe(gulp.dest(buildPath + 'assets/img'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('copyLib', function() {
return gulp.src(libSrc)
.pipe(gulp.dest(buildPath + 'lib'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('copyFont', function() {
return gulp.src(fontSrc)
.pipe(gulp.dest(buildPath + 'assets/fonts'))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('clean', function() {
return gulp.src(buildPath)
.pipe(clean());
});
gulp.task('startCreate', ['clean'], function() {
runSequence('createIndex',
['compressHTML', 'compressJS', 'compressCSS', 'compressJSON'],
['copyLib', 'copyImg', 'copyFont']);
});
gulp.task('browserSync', ['startCreate'], function() {
browserSync.init({
server: {
baseDir: debugPath
}
});
watch(indexSrc, () => {gulp.start('createIndex');});
watch(htmlSrc, () => {gulp.start('compressHTML');});
watch(jsSrc, () => {gulp.start('compressJS');});
watch(cssSrc, () => {gulp.start('compressCSS');});
watch(jsonSrc, () => {gulp.start('compressJSON');});
});
gulp.task('default', ['browserSync']);