This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
109 lines (95 loc) · 3.14 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
'use strict';
////////////////////////////////
//Setup//
////////////////////////////////
// Plugins
const gulp = require('gulp');
const pjson = require('./package.json');
const gutil = require('gulp-util');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const rename = require('gulp-rename');
const del = require('del');
const plumber = require('gulp-plumber');
const pixrem = require('gulp-pixrem');
const uglify = require('gulp-uglify');
const imagemin = require('gulp-imagemin');
const browserSync = require('browser-sync').create();
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const reload = browserSync.reload;
// Relative paths function
let pathsConfig = appName => {
const app = `./${appName || pjson.name}`;
return {
dist: `${app}/static/dist/`,
templates: `${app}/templates`,
css: `${app}/static/dist/css`,
sass: `${app}/static/sass`,
fonts: `${app}/static/fonts`,
images: `${app}/static/images`,
js: `${app}/static/js`,
distJs: `${app}/static/dist/js`,
app
}
};
const paths = pathsConfig();
////////////////////////////////
//Tasks//
////////////////////////////////
// Styles autoprefixing and minification
gulp.task('styles', () => {
return gulp.src(`${paths.sass}/main.scss`)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(plumber()) // Checks for errors
.pipe(autoprefixer({browsers: ['last 2 version']})) // Adds vendor prefixes
.pipe(pixrem()) // add fallbacks for rem units
.pipe(rename({ suffix: '.min' }))
.pipe(cssnano()) // Minifies the result
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest(paths.css));
});
// Javascript minification
gulp.task('scripts', () => {
return gulp.src([`${paths.js}/**/*.js`, `!**/*.min.js`])
.pipe(plumber()) // Checks for errors
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(uglify()) // Minifies the js
.pipe(rename({ suffix: '.min' }))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest(paths.distJs));
});
// Image compression
gulp.task('imgCompression', () => {
return gulp.src(`${paths.images}/*`)
.pipe(imagemin()) // Compresses PNG, JPEG, GIF and SVG images
.pipe(gulp.dest(paths.images))
});
// Browser sync server for live reload
gulp.task('browserSync', () => {
browserSync.init(
[`${paths.css}/*.css`, `${paths.js}/*.js`, `${paths.templates}/*.html`], {
proxy: "localhost:8000",
open: false
});
});
// Default task
gulp.task('default', ['styles', 'scripts', 'imgCompression']);
////////////////////////////////
//Watch//
////////////////////////////////
// Watch
gulp.task('watch', ['default', 'browserSync'], () => {
gulp.watch(`${paths.sass}/**/*.scss`, ['styles']).on("change", reload);
gulp.watch(`${paths.js}/**/*.js`, ['scripts']).on("change", reload);
gulp.watch(`${paths.images}/*`, ['imgCompression']);
gulp.watch(`${paths.templates}/**/*.html`).on("change", reload);
});
// Clear cache Dist Folder
gulp.task('clean', (cb) => {
// Delete static/dist folder
del([paths.dist], cb);
});