-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
191 lines (169 loc) · 5.67 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
//
// Interview gulp config file
//
// Project.
var project = 'IDI Interview';
var projectHost = 'localhost';
var projectPort = '3000';
var productURL = './';
// HTML.
var htmlSRC = './src/**/*.html';
var htmlDestination = './build/';
// Styles.
var styleSRC = './src/styles/styles.less';
var styleDestination = './build/';
// Fonts.
var fontsSRC = './src/fonts/*';
var fontsDestination = './build/fonts/';
// Images.
var imagesSRC = './src/images/**/*.{png,jpg,gif,svg}';
var imagesDestination = './build/images/';
// Watch files paths.
var styleWatchFiles = './src/styles/**/*.less';
var projectHTMLWatchFiles = './src/**/*.html';
var jsWatchFiles = './src/**/*.js';
// Js
var jsSRC = ['./src/js/**/*'];
var jsDestination = './build/js/';
const AUTOPREFIXER_BROWSERS = [
'last 2 version',
'> 1%',
'ie >= 9',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4',
'bb >= 10'
];
/**
* Load Gulp Plugins.
*/
var gulp = require('gulp'); // Gulp of-course
// HTML plugins
var htmlmin = require('gulp-htmlmin');
// CSS plugins.
var less = require('gulp-less'); // Gulp pluign for Sass compilation.
var minifycss = require('gulp-uglifycss'); // Minifies CSS files.
var autoprefixer = require('gulp-autoprefixer'); // Autoprefixing magic.
var mmq = require('gulp-merge-media-queries'); // Combine matching media queries into one media query definition.
// Image plugins.
var imagemin = require('gulp-imagemin'); // Minify PNG, JPEG, GIF and SVG images with imagemin.
// Utility plugins.
var rename = require('gulp-rename'); // Renames files E.g. style.css -> style.min.css
var lineec = require('gulp-line-ending-corrector'); // Consistent Line Endings for non UNIX systems. Gulp Plugin for Line Ending Corrector (A utility that makes sure your files have consistent line endings)
var filter = require('gulp-filter'); // Enables you to work on a subset of the original files by filtering them using globbing.
var notify = require('gulp-notify'); // Sends message notification to you
var browserSync = require('browser-sync').create(); // Reloads browser and injects CSS. Time-saving synchronised browser testing.
var reload = browserSync.reload; // For manual browser reload.
var del = require('del');
var vinylPaths = require('vinyl-paths');
var plumber = require('gulp-plumber');
// Error notifications
var notify = {
html: { errorHandler: notify.onError('HTML: BUILD FAILED!\n' + 'Error:\n<%= error.message %>') },
styles: { errorHandler: notify.onError('STYLES: BUILD FAILED!\n' + 'Error:\n<%= error.message %>') },
font: { errorHandler: notify.onError('FONTS: SOMETHING WRONG!\n' + 'Error:\n<%= error.message %>') },
img: { errorHandler: notify.onError('IMAGES: SOMETHING WRONG!\n' + 'Error:\n<%= error.message %>') }
};
/**
* Task: `html`.
* Copies all html files that are in ./src/** maintaining directory structure
*/
gulp.task('html', function() {
return gulp
.src(htmlSRC, {base: './src/'})
.pipe(plumber(notify.html))
// .pipe(htmlmin({collapseWhitespace: true, includeAutoGeneratedTags: false}))
.pipe(gulp.dest(htmlDestination))
.pipe(browserSync.reload({ stream: true }))
});
/**
* Task: `js`.
* Copies all js files that are in ./src/** maintaining directory structure
*/
gulp.task('js', function() {
return gulp
.src(jsSRC )
.pipe(plumber(notify.js))
.pipe(gulp.dest(jsDestination))
.pipe(browserSync.reload({ stream: true }))
});
/**
* Task: `styles`.
* Compiles Less, Autoprefixes it and Minifies CSS.
*/
gulp.task('styles', function () {
return gulp
.src(styleSRC)
.pipe(plumber(notify.styles))
.pipe(less())
.pipe(autoprefixer(AUTOPREFIXER_BROWSERS ))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(styleDestination ))
.pipe(mmq()) // Merge Media Queries only for .min.css version.
.pipe(vinylPaths(del))
.pipe(rename('style.css'))
.pipe(minifycss({
maxLineLen: 10,
keepSpecialComments : 1
}))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(styleDestination))
.pipe(browserSync.reload({ stream: true }))
});
/**
* Task: `fonts`.
* Copies fonts from ./src to destination
*/
gulp.task('fonts', function() {
return gulp
.src(fontsSRC )
.pipe(plumber(notify.font))
.pipe(gulp.dest(fontsDestination))
.pipe(browserSync.reload({ stream: true }))
})
/**
* Task: `images`.
* Minifies PNG, JPEG, GIF and SVG images.
*/
gulp.task('images', function() {
return gulp
.src(imagesSRC )
.pipe(plumber(notify.img))
.pipe(imagemin({
progressive: true,
optimizationLevel: 3, // 0-7 low-high
interlaced: true,
svgoPlugins: [{removeViewBox: false}]
}))
.pipe(gulp.dest(imagesDestination))
.pipe(browserSync.reload({ stream: true }))
});
/**
* Task `clean`.
* Clean build directory before building
*/
gulp.task('clean', function (cb) {
return gulp
.src([ htmlDestination ])
.pipe(vinylPaths(del));
});
/**
* Task: `browser-sync`.
* Auto reloads, CSS injections, Localhost tunneling.
*/
gulp.task( 'browser-sync', function() {
browserSync.init({
server: {
baseDir: htmlDestination
}
});
gulp.watch(projectHTMLWatchFiles, ['html']);
gulp.watch(styleWatchFiles, ['styles']);
gulp.watch(jsWatchFiles, ['js']);
});
gulp.task('build', ['html', 'styles', 'fonts', 'images', 'js']);
gulp.task( 'default', ['build', 'browser-sync']);