-
Notifications
You must be signed in to change notification settings - Fork 35
/
gulpfile.js
executable file
·150 lines (135 loc) · 3.95 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs')
const path = require('path')
const gulp = require('gulp')
const gulpLoadPlugins = require('gulp-load-plugins')
const plumber = require('gulp-plumber');
const notify = require("gulp-notify")
const del = require('del')
const runSequence = require('run-sequence')
// load all gulp plugins
const plugins = gulpLoadPlugins()
const env = process.env.NODE_ENV || 'development'
const isProduction = () => env === 'production'
function handleErrors(errorObject, callback) {
notify.onError(errorObject.toString().split(': ').join(':\n')).apply(this, arguments);
// Keep gulp from hanging on this task
if (typeof this.emit === 'function') {
this.emit('end');
}
}
/**
* Clean distribution directory
*/
gulp.task('clean', del.bind(null, ['dist/*']))
/**
* Compile js source to distribution directory
*/
gulp.task('compile:js', () => {
return gulp.src(['src/**/*.js'])
.pipe(plumber(handleErrors))
.pipe(plugins.newer('dist'))
.pipe(plugins.logger({ showChange: true }))
.pipe(plugins.babel({
presets: ['es2015']
}))
.pipe(plugins.if(isProduction, plugins.uglify()))
.pipe(gulp.dest('dist'))
})
/**
* Compile xml source to distribution directory
*/
gulp.task('compile:xml', () => {
return gulp.src(['src/**/*.xml'])
.pipe(plumber(handleErrors))
.pipe(plugins.rename({ extname: '.wxml' }))
.pipe(plugins.newer('dist'))
.pipe(plugins.logger({ showChange: true }))
.pipe(plugins.if(isProduction, plugins.htmlmin({
collapseWhitespace: true,
keepClosingSlash: true, // xml
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})))
.pipe(gulp.dest('dist'))
})
/**
* Compile less source to distribution directory
*/
gulp.task('compile:less', () => {
return gulp.src(['src/**/*.less'])
.pipe(plumber(handleErrors))
.pipe(plugins.less())
.pipe(plugins.rename({ extname: '.wxss' }))
.pipe(plugins.newer('dist'))
.pipe(plugins.logger({ showChange: true }))
.pipe(plugins.if(isProduction, plugins.cssnano({ compatibility: '*' })))
.pipe(gulp.dest('dist'))
})
/**
* Compile json source to distribution directory
*/
gulp.task('compile:json', () => {
return gulp.src(['src/**/*.json'])
.pipe(plumber(handleErrors))
.pipe(plugins.newer('dist'))
.pipe(plugins.logger({ showChange: true }))
.pipe(plugins.jsonminify())
.pipe(gulp.dest('dist'))
})
/**
* Compile img source to distribution directory
*/
gulp.task('compile:img', () => {
return gulp.src(['src/**/*.{jpg,jpeg,png,gif}'])
.pipe(plumber(handleErrors))
.pipe(plugins.newer('dist'))
.pipe(plugins.logger({ showChange: true }))
.pipe(plugins.imagemin())
.pipe(gulp.dest('dist'))
})
/**
* Compile source to distribution directory
*/
gulp.task('compile', ['clean'], next => {
runSequence([
'compile:js',
'compile:xml',
'compile:less',
'compile:json',
'compile:img'
], next)
})
/**
* Copy extras to distribution directory
*/
gulp.task('extras', [], () => {
return gulp.src([
'src/**/*.*',
'!src/**/*.js',
'!src/**/*.xml',
'!src/**/*.less',
'!src/**/*.json',
'!src/**/*.{jpe?g,png,gif}'
])
.pipe(gulp.dest('dist'))
})
/**
* Build
*/
gulp.task('build', next => runSequence(['compile', 'extras'], next))
/**
* Watch source change
*/
gulp.task('watch', ['build'], () => {
gulp.watch('src/**/*.js', ['compile:js'])
gulp.watch('src/**/*.xml', ['compile:xml'])
gulp.watch('src/**/*.less', ['compile:less'])
gulp.watch('src/**/*.json', ['compile:json'])
gulp.watch('src/**/*.{jpe?g,png,gif}', ['compile:img'])
})
/**
* Default task
*/
gulp.task('default', ['watch'])