-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
268 lines (249 loc) Β· 7.04 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* if work with html set TRUE, else - FALSE */
var htmlOWp = false,
// set wordpress template folder
wpThemeName = 'sandorik',
// and set wordpress domain
wpDomain = 'wp-plugin.dev';
var AUTOPREFIXER_BROWSERS = [
'ie >= 8', 'ie_mob >= 10', 'ff >= 20', 'chrome >= 24', 'safari >= 5', 'opera >= 12', 'ios >= 7', 'android >= 2.3', '> 1%', 'last 4 versions', 'bb >= 10'
];
if (htmlOWp === true) {
var basePaths = {
src: 'assets/',
dest: './html/'
};
} else {
var basePaths = {
src: 'assets/',
dest: './wordpress/wp-content/plugins/' + wpThemeName + '/'
};
}
var paths = {
images: {
src: basePaths.src + 'img/',
srcimg: basePaths.src + 'img/**/*.{png,jpg,jpeg,gif,svg}',
dest: basePaths.dest + 'img/'
},
scripts: {
src: basePaths.src + 'js/**',
dest: basePaths.dest + 'js/'
},
styles: {
src: basePaths.src + 'scss/',
css: basePaths.src + 'css/',
dest: basePaths.dest + 'css/'
},
fonts: {
src: basePaths.src + 'fonts/**',
dest: basePaths.dest + 'fonts/'
},
sprite: {
src: basePaths.src + 'sprite/*'
}
};
var appFiles = {
styles: paths.styles.src + '**/*.scss',
scripts: [paths.scripts.src],
};
var changeEvent = function(evt) {
gutil.log('File', gutil.colors.cyan(evt.path.replace(new RegExp('/.*(?=/' + basePaths.src + ')/'), '')), 'was', gutil.colors.magenta(evt.type));
};
var spriteConfig = {
imgName: 'super_sprite.png',
cssName: '_super_sprite.scss',
imgPath: '../img/' + 'super_sprite.png'
};
var gulp = require('gulp'),
gutil = require('gulp-util'),
es = require('event-stream'),
runSequence = require('run-sequence'),
browserSync = require('browser-sync'),
reload = browserSync.reload,
pngquant = require('imagemin-pngquant'),
plugins = require("gulp-load-plugins")({
pattern: ['gulp-*', 'gulp.*'],
replaceString: /\bgulp[\-.]/
});
// postcss
var autoprefixer = require('autoprefixer'),
mqpacker = require('css-mqpacker'),
csswring = require('csswring'),
cssnext = require('cssnext');
// Copy web fonts to dist
gulp.task('fonts', function() {
return gulp.src(paths.fonts.src)
.pipe(plugins.newer(paths.fonts.dest))
.pipe(gulp.dest(paths.fonts.dest))
.pipe(plugins.size({
showFiles: true,
title: 'task:fonts'
}));
});
// Optimize images
gulp.task('images', function() {
return gulp.src(paths.images.srcimg)
.pipe(plugins.newer(paths.images.dest))
.pipe(plugins.imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{
removeViewBox: false
}],
use: [pngquant()]
}))
.pipe(plugins.size({
showFiles: true,
title: 'task:images'
}))
.pipe(gulp.dest(paths.images.dest));
});
// Generate sprites
gulp.task('sprite', function() {
var spriteData = gulp.src(paths.sprite.src + '*.png')
.pipe(plugins.spritesmith({
imgName: spriteConfig.imgName,
cssName: spriteConfig.cssName,
imgPath: spriteConfig.imgPath,
cssVarMap: function(sprite) {
sprite.name = 'sprite-' + sprite.name;
}
}))
.pipe(plugins.if('*.png', gulp.dest(paths.images.src)))
.pipe(plugins.if('*.scss', gulp.dest(paths.styles.src)))
.pipe(plugins.size({
showFiles: true,
title: ' task:sprite'
}));
});
// find images in css end encode it to base64
gulp.task('base64', function() {
return gulp.src(paths.styles.src + '_base64.scss')
.pipe(plugins.base64({
extensions: ['svg', 'png', 'gif', 'jpg', /\.jpg#datauri$/i],
maxImageSize: 20 * 1024, // bytes
debug: false
}))
.pipe(gulp.dest(paths.styles.src));
});
// Optimize script
gulp.task('scripts', function() {
gulp.src(appFiles.scripts)
.pipe(plugins.newer(paths.scripts.dest))
.pipe(customPlumber('Error Compiling Scripts'))
.pipe(plugins.sourcemaps.init())
.pipe(plugins.if('*.js', plugins.uglify()))
.pipe(plugins.sourcemaps.write('maps', {
includeContent: true
}))
.pipe(gulp.dest(paths.scripts.dest))
.pipe(plugins.size({
showFiles: true,
title: 'task:scripts:'
}));
});
// Compile and automatically prefix stylesheets
gulp.task('scss', function() {
// For best performance, don't add Sass partials to `gulp.src`
return gulp.src(appFiles.styles)
.pipe(customPlumber('Error Running Sass'))
.pipe(plugins.newer(paths.styles.css))
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass({
outputStyle: 'compressed',
precision: 10,
onError: console.error.bind(console, 'Sass error:')
}))
.pipe(plugins.sourcemaps.write('maps', {
includeContent: true
}))
.pipe(gulp.dest(paths.styles.css))
.pipe(plugins.size({
showFiles: true,
title: 'task:styles'
}));
});
// postcss autoprefix
gulp.task('postcss', function() {
var processors = [
autoprefixer({
browsers: ['ie >= 8', 'ie_mob >= 10', 'ff >= 20', 'chrome >= 24', 'safari >= 5', 'opera >= 12', 'ios >= 7', 'android >= 2.3', '> 1%', 'last 4 versions', 'bb >= 10']
}),
mqpacker,
csswring,
cssnext()
];
return gulp.src(paths.styles.css + '**/*.css')
.pipe(customPlumber('Error Compiling PostCSS'))
.pipe(plugins.newer(paths.styles.dest))
.pipe(plugins.sourcemaps.init())
.pipe(plugins.postcss(processors))
.pipe(plugins.sourcemaps.write('maps', {
includeContent: true
}))
.pipe(gulp.dest(paths.styles.dest))
.pipe(plugins.size({
showFiles: true,
title: 'task:postcss'
}));
});
// clear gulp cache
gulp.task('cache:clear', function(done) {
return plugins.cache.clearAll(done);
});
// Browser Sync
gulp.task('browserSync', function() {
if (htmlOWp === true) {
browserSync({
notify: false,
port: 9080,
server: {
baseDir: basePaths.dest,
}
});
} else {
browserSync({
notify: false,
proxy: wpDomain,
host: wpDomain,
port: 9090
});
}
});
gulp.task('styles', function(callback) {
runSequence(
['scss'], ['postcss'],
callback
)
})
// Consolidated dev phase task
gulp.task('serve', function(callback) {
runSequence(
'cache:clear', ['sprite', 'base64', 'images'], ['scripts'], ['scss', 'fonts'], ['postcss'], ['browserSync', 'watch'],
callback
);
});
gulp.task('watch', function() {
// watch for changes
gulp.watch([
basePaths.dest + '**/*.{html,htm,php}'
]).on('change', reload);
gulp.watch(paths.sprite.src, ['cache:clear', 'sprite', 'images', 'styles', reload]);
gulp.watch(paths.images.srcimg, ['images', reload]);
gulp.watch(paths.styles.src + '/_base64.scss', ['base64', reload]);
gulp.watch(appFiles.styles, ['styles', reload]);
gulp.watch(paths.sprite.src, ['styles', reload]);
gulp.watch(paths.fonts.src, ['fonts', reload]);
gulp.watch(appFiles.scripts, ['scripts', reload]);
});
// Custom Plumber function for catching errors
function customPlumber(errTitle) {
return plugins.plumber({
errorHandler: plugins.notify.onError({
// Customizing error title
title: errTitle || 'Error running Gulp',
message: 'Error: <%= error.message %>',
sound: "Bottle"
})
});
};
module.exports = customPlumber;