-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
230 lines (205 loc) · 7.49 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var gulp = require('gulp')
var gutil = require('gulp-util')
var uglify = require('gulp-uglify')
//var uglifyjs = require('uglify-js-harmony');
//var minifier = require('gulp-uglify/minifier');
//minifier({}, uglifyjs),
var watchPath = require('gulp-watch-path')
var combiner = require('stream-combiner2')
//var sourcemaps = require('gulp-sourcemaps')
var minifycss = require('gulp-clean-css')
//var autoprefixer = require('gulp-autoprefixer')
var sass = require('gulp-sass')
var automerge = require('gulp-automerge');
var rename = require('gulp-rename')
var imagemin = require('gulp-imagemin')
var htmlmin = require('gulp-html-minify')
var clean = require('gulp-clean');
var template = require('gulp-template');
var autoprefixer = require('gulp-autoprefixer');
var config = require('./gulpConfig');
var srcPath = config.srcPath;
var distPath = config.distPath;
var options = gutil.env;
var env = options.e || 'pro';//连接环境
var dev = options.dev || false;//连接环境
var tpls = {},
tplKeys = Object.keys(config.templateVar[env]) || [];
tplKeys.map(function(key){
tpls[key] = (typeof config.templateVar[env][key] == 'object')?JSON.stringify(config.templateVar[env][key]):config.templateVar[env][key];
})
var handleError = function (err) {
var colors = gutil.colors;
console.log('\n')
gutil.log(colors.red('Error!'))
gutil.log('fileName: ' + colors.red(err.fileName))
gutil.log('lineNumber: ' + colors.red(err.lineNumber))
gutil.log('message: ' + err.message)
gutil.log('plugin: ' + colors.yellow(err.plugin))
}
gulp.task("clean", function(){
return gulp.src(distPath+'/*')
.pipe(clean());
})
gulp.task('template', function() {
gulp.src(config.tplConf.src)
.pipe(template({domainConf: JSON.stringify(config.templateVar[env].domainConf)}))
.pipe(gulp.dest(distPath))
});
gulp.task('watchhtml', function () {
gulp.watch(config.other.src, function (event) {
var paths = watchPath(event, srcPath, distPath)
/*
paths
{ srcPath: 'src/js/log.js',
srcDir: 'src/js/',
distPath: 'dist/js/log.js',
distDir: 'dist/js/',
srcFilename: 'log.js',
distFilename: 'log.js' }
*/
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' + paths.distPath)
gulp.src(paths.srcPath)
.pipe(htmlmin())
.pipe(gulp.dest(paths.distDir));
})
})
gulp.task('htmlmin', function () {
gulp.src(config.other.src,{base:srcPath})
.pipe(htmlmin())
.pipe(gulp.dest(distPath));
});
gulp.task('watchjs', function () {
gulp.watch(config.js.src, function (event) {
var paths = watchPath(event, srcPath, distPath)
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' + paths.distPath)
var combined = dev?combiner.obj([
gulp.src(paths.srcPath),
//sourcemaps.init(),
template(tpls),
//sourcemaps.write('./'),
gulp.dest(paths.distDir)
]):combiner.obj([
gulp.src(paths.srcPath),
//sourcemaps.init(),
template({
domainConf: JSON.stringify(config.templateVar[env].domainConf),
environment:config.templateVar[env].environment
}),
uglify(),
//sourcemaps.write('./'),
gulp.dest(paths.distDir)
]);
combined.on('error', handleError);
})
})
gulp.task('uglifyjs', function () {
var combined = dev?combiner.obj([
gulp.src(config.js.src),
//sourcemaps.init(),
template(tpls),
//sourcemaps.write('./'),
gulp.dest(distPath)
]):combiner.obj([
gulp.src(config.js.src),
//sourcemaps.init(),
template({
domainConf: JSON.stringify(config.templateVar[env].domainConf),
environment:config.templateVar[env].environment
}),
uglify(),
//sourcemaps.write('./'),
gulp.dest(distPath)
]);
combined.on('error', handleError)
})
gulp.task('watchsass',function () {
gulp.watch(config.css.src, function (event) {
var paths = watchPath(event, srcPath, distPath)
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' + paths.distPath)
gulp.src(paths.srcPath)
.on('error', function (err) {
console.error('Error!', err.message);
})
//.pipe(sourcemaps.init())
.pipe(automerge({
prefixApplyType: 'all',
prefixText: '@import "{relativePrefix}style/base/_variables.scss"; @import "{relativePrefix}style/base/_mixin.scss"; @import "{relativePrefix}style/base/_animation.scss";',
replaceExt: '.wxml',
regexp:/<import [^>]*src=[\'\"][^\'\"]+?\/template\/([\w-]+)\/\w+\.\w+[\'\"]\s*\/>/g,
appendTpl: '@import "{relativePrefix}template/{name}/index.scss";'
}))
.pipe(sass({outputStyle: 'compressed'}))
.pipe(autoprefixer({
browsers: ['last 2 versions', 'Android >= 4.0', 'iOS >= 8.0'],
cascade: true,
remove:true //是否去掉不必要的前缀 默认:true
}))
.pipe(rename({
extname: ".wxss"
}))
//.pipe(minifycss({advanced:false}))
//.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.distDir))
})
})
gulp.task('sasscss', function () {
gulp.src(config.css.src)
.on('error', function (err) {
console.error('Error!', err.message);
})
//.pipe(sourcemaps.init())
.pipe(automerge({
prefixApplyType: 'all',
prefixText: '@import "{relativePrefix}style/base/_variables.scss"; @import "{relativePrefix}style/base/_mixin.scss"; @import "{relativePrefix}style/base/_animation.scss";',
replaceExt: '.wxml',
regexp:/<import [^>]*src=[\'\"][^\'\"]+?\/template\/([\w-]+)\/\w+\.\w+[\'\"]\s*\/>/g,
appendTpl: '@import "{relativePrefix}template/{name}/index.scss";'
}))
.pipe(sass({outputStyle: 'compressed'}))
.pipe(autoprefixer({
browsers: ['last 2 versions', 'Android >= 4.0', 'iOS >= 8.0'],
cascade: true,
remove:true //是否去掉不必要的前缀 默认:true
}))
.pipe(rename({
extname: ".wxss"
}))
//.pipe(minifycss({advanced:false}))
//.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(distPath))
})
gulp.task('watchimage', function () {
gulp.watch(config.image.src, function (event) {
var paths = watchPath(event, srcPath, distPath)
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' + paths.distPath)
gulp.src(paths.srcPath)
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest(paths.distDir))
})
})
gulp.task('image', function () {
gulp.src(config.image.src)
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest(distPath + 'images'))
})
gulp.task('default', ['clean'], function(){
gulp.start(
// build
'uglifyjs', 'sasscss', 'htmlmin',
// watch
'watchjs', 'watchsass', 'watchhtml'
);
})
gulp.task('build', ['clean'], function(){
gulp.start('uglifyjs', 'sasscss', 'htmlmin','image');
});
gulp.task('watch', ['watchjs', 'watchsass', 'watchhtml'])