-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
173 lines (154 loc) · 5.86 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
/*
* @Author: zhangxinliang
* @Date: 2016-07-04 14:13:20
* @Last modified by: zhangxinliang
* @Last modified time: 2016-09-09 14:02:12
*/
'use strict'
var gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
cssnano = require('gulp-cssnano'),
minify = require('gulp-minify-css'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
cache = require('gulp-cache'),
del = require('del'),
rollup = require('rollup').rollup,
babel = require('gulp-babel'),
uglify = require('gulp-uglify'),
source = require('vinyl-source-stream'),
browserSync = require('browser-sync'),
plumber = require('gulp-plumber'),
notify = require('gulp-notify'),
spriter = require('gulp-css-spriter'),
through = require('through2')
var projects = require('./project.config.js')
projects.forEach(function (item) {
var pname = item.projectdir,
port = item.port,
version = item.version,
basedir = 'app/' + pname,
browser = browserSync.create(),
reload = browser.reload
//styles-scss to css
gulp.task(pname + ':styles', function () {
return gulp.src(basedir + '/src/sass/*.scss')
.pipe(sass({ style: 'expanded' }))
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(autoprefixer('last 4 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(gulp.dest(basedir + '/src/css'))
})
//mincss-css to min.css
gulp.task(pname + ':mincss', function () {
return gulp.src(basedir + '/src/css/**/*.css')
.pipe(cssnano())
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(gulp.dest(basedir + '/dist/css'))
})
//scripts-es6 to es5
gulp.task(pname + ':babel', function () {
return gulp.src(basedir + '/src/js/bundle.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(uglify())
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(gulp.dest(basedir + '/dist/js/'))
})
gulp.task(pname + ':rollup', function () {
return rollup({
entry: basedir + '/src/scripts/app.js',
plugins: [
babel({
exclude: 'node_modules/**',
presets: [
[
'es2016',
{
'modules': false
}
]
],
}),
]
}).then(function(bundle) {
bundle.write({
format: 'es6',
globals: {
moment: 'moment'
},
dest: basedir + "/src/js/bundle.js"
})
}).catch(function (err) {
console.log(err)
})
})
//images-images to min img
gulp.task(pname + ':images', function() {
return gulp.src(basedir + '/src/images/**/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(gulp.dest(basedir + '/dist/img'))
})
//html-src html to html
gulp.task(pname + ':html', function () {
return gulp.src(basedir + '/src/html/**/*.html')
.pipe(gulp.dest(basedir + '/dist/'))
})
// clean-delete dist
gulp.task(pname + ':clean', function(cb) {
del([basedir + '/dist/'], cb)
})
var cssfiles = []
gulp.task(pname + ':filenames', function () {
return gulp.src(basedir + '/dist/css/**/*.css')
.pipe(through.obj(function(file,enc,cb){
cssfiles.push(file.relative)
cb()
}))
})
function spriterGroup (pathArr) {
cssfiles.forEach(function (item) {
var name = item.replace('.css', '')
gulp.src(basedir + '/dist/css/' + item)
.pipe(spriter({
'spriteSheet': basedir + '/dist/img/spriteSheet_' + name + '.png',
'pathToSpriteSheetFromCSS': '../img/spriteSheet_' + name + '.png'
}))
.pipe(minify())
.pipe(gulp.dest(basedir + '/dist/css/'))
})
}
gulp.task(pname + ':spirter', [pname + ':filenames'],function(){
spriterGroup(cssfiles)
})
gulp.task(pname + ':dev', [pname + ':styles', pname + ':mincss', pname + ':rollup', pname + ':babel', pname + ':images', pname + ':html', pname + ':serve'])
gulp.task(pname + ':js-watch', [pname + ':rollup', pname + ':babel'], function () {
browser.reload()
})
gulp.task(pname + ':sass-watch', [pname + ':styles'])
gulp.task(pname + ':css-watch', [pname + ':mincss'], function () {
browser.reload()
})
gulp.task(pname + ':img-watch', [pname + ':images'], browser.reload)
gulp.task(pname + ':html-watch', [pname + ':html'], function () {
browser.reload()
})
//serve-start browsersync
gulp.task(pname + ':serve', [], function () {
browser.init({
server: {
baseDir: basedir + '/dist/'
},
port: port
})
gulp.watch(basedir + '/src/sass/**/*.scss', [pname + ':sass-watch'])
gulp.watch(basedir + '/src/css/**/*.css', [pname + ':css-watch'])
gulp.watch(basedir + '/src/scripts/**/*.js', [pname + ':js-watch'])
gulp.watch(basedir + '/src/images/**/*', [pname + ':img-watch'])
gulp.watch(basedir + '/src/html/**/*.html', [pname + ':html-watch'])
})
})