This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
314 lines (281 loc) · 8.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// default
// build
// critical
const envVars = {
development: {
API_HOST: '',
// USE_LOCALHOST: true,
},
production: {
API_HOST: '',
}
}
const { src, dest, parallel, series, watch } = require('gulp');
var sourcemaps = require('gulp-sourcemaps'),
svgSprite = require('gulp-svg-sprite'),
postHTML = require('gulp-posthtml'),
// imagemin = require('gulp-imagemin'), // compresses images
// pngquant = require('imagemin-pngquant'), // plugin for above
critical = require('critical').stream,
webpack = require('webpack-stream'),
postcss = require('gulp-postcss'),
connect = require('gulp-connect'), // server
plumber = require('gulp-plumber'), // debugger
rename = require('gulp-rename'),
gulpif = require('gulp-if'),
notify = require('gulp-notify'),
cache = require('gulp-cached'),
clean = require('gulp-clean'); // removing folders :)
// webp = require('gulp-webp'); // webp converter
const postHtmlPlugins = [
// require('posthtml-webp')({replaceExtension: true}),
// require('posthtml-beautify')({rules: {
// indent: 2,
// blankLines: false
// }}),
];
const postHtmlOptions = { parser: require('posthtml-pug')({ pretty: true, locals: {}, basedir: './src/' }) };
var autoprefixer = require('autoprefixer'),
postCssImport = require('postcss-import'), // import of components
modernCSS = require('postcss-preset-env'), // lets you convert modern CSS
comments = require('postcss-strip-inline-comments'),
flexbugs = require('postcss-flexbugs-fixes'),
reporter = require('postcss-reporter'), // errors log
cssnano = require('cssnano'), // minify css
nested = require('postcss-nested'), // nesting of styles
objfit = require('postcss-object-fit-images'),
mixins = require('postcss-mixins'),
color = require('postcss-color-function'), // scss color functions
scss = require('postcss-scss'),
short = require('postcss-short'); // allows you to write in short form
var path = {
dist: { // Here we define where to put ready files after assembly
html: 'dist/',
js: 'dist/js/',
css: 'dist/css/',
img: 'dist/assets/images/',
svg: 'dist/assets/svg/',
svgSprite: 'src/assets/',
fonts: 'dist/fonts/'
},
src: { // Paths to get sources from
html: 'src/pages/*.pug', // The src/*.ext syntax tells gulp that we want to take all files with .ext extension
js: 'src/main.js', // In styles and scripts we need only main files
css: 'src/main.scss',
img: 'src/assets/images/**/*.{gif,jpeg,jpg,png,svg,webp}', // The img/**/*.* syntax means: take all files of all extensions from a folder and from subdirectories
svg: 'src/assets/svg/**/*.svg',
fonts: 'src/assets/fonts/**/*.*'
},
watch: { // Here we indicate the change in which files we want to be updated.
html: 'src/**/*.pug',
js: 'src/**/*.js',
css: 'src/**/*.{css,scss}',
img: 'src/assets/images/**/*.*',
svg: 'src/assets/svg/**/*.*',
fonts: 'src/assets/fonts/**/*.*'
},
clean: 'dist'
};
var processors = [
comments(),
autoprefixer(),
modernCSS({
importFrom: ['./src/vars.css'],
preserve: false
}),
short(),
mixins(),
nested(),
color(),
flexbugs,
objfit(),
cssnano({
reduceIdents: false
}),
reporter()
];
var notifyTemplate = notify.onError({
title: '<%= error.plugin %>',
// message: '<%= error.fileName %>'
})
function css() {
return src(path.src.css)
.pipe(plumber({
errorHandler: notifyTemplate
}))
.pipe( gulpif(process.env.NODE_ENV == 'development', sourcemaps.init() ) )
// .pipe(notify("» css <%= file.relative %>"))
.pipe(postcss([
postCssImport({
root: "src/css",
path: ["../css", "../css/components", "../css/data"]
})
]), {syntax: scss})
.pipe(cache('css'))
.pipe(postcss(processors, {syntax: scss}))
.pipe(rename({ extname: ".css" }))
.pipe( gulpif(process.env.NODE_ENV == 'development', sourcemaps.write() ) )
.pipe(dest(path.dist.css))
.pipe(connect.reload());
}
function html() {
let stream = src(path.src.html) // Get files
stream
.pipe(plumber({
errorHandler: notifyTemplate
}))
stream
.pipe(postHTML(postHtmlPlugins, postHtmlOptions))
.pipe(rename({ extname: ".html" }))
.pipe(dest(path.dist.html)) // Put them in the dist folder
.pipe(connect.reload());
return stream;
}
function js() {
console.log(process.env.NODE_ENV);
return src(path.src.js) // Find our main file
.pipe(plumber({
errorHandler: notifyTemplate
}))
.pipe( gulpif(process.env.NODE_ENV == 'development', sourcemaps.init() ) ) // init sourcemaps
.pipe(webpack(require('./webpack.config.js')))
.pipe( gulpif(process.env.NODE_ENV == 'development', sourcemaps.write() ) ) // set sourcemaps
.pipe(dest(path.dist.js)) // Put the completed file in the dist folder
.pipe(connect.reload());
}
function img() {
return src(path.src.img)
.pipe(plumber())
.pipe(cache('images'))
.pipe(dest(path.dist.img)) // Save original
// .pipe(webp())
.pipe(dest(path.dist.img)); // Save generated webp
}
// function imgMinified() {
// return src(path.src.img)
// .pipe(plumber())
// .pipe(imagemin({
// progressive: true,
// use: [pngquant()],
// interlaced: true
// }))
// .pipe(dest(path.dist.img)) // Save minified original
// // .pipe(webp())
// .pipe(dest(path.dist.img)); // Save generated webp
// };
function fonts() {
return src(path.src.fonts)
.pipe(plumber())
.pipe(cache('fonts'))
.pipe(dest(path.dist.fonts));
}
function svg() {
return src(path.src.svg)
.pipe(plumber())
.pipe(dest(path.dist.svg))
.pipe(svgSprite({
mode: {
symbol: true
},
shape: {
dimension: {
attributes: true
}
}
}))
.on('error', function(error) {
console.log('error: ', error);
})
.pipe(dest(path.dist.svgSprite));
}
function cleanDist() {
console.log('cleanDist: ');
return src(path.clean, { read: false, allowEmpty: true })
.pipe(plumber())
.pipe(clean())
// .pipe(notify("» clean dist "));
}
function copyStaticFiles() {
return src(['public/**/*'])
.pipe(plumber())
.pipe(dest('dist/'));
}
function generateCritical() {
return src('dist/*.html')
.pipe(plumber())
// .pipe(notify("critical! <%= file.relative %>"))
.pipe(critical({ base: 'dist/', width: 1300, height: 900, inline: false, minify: true }))
.pipe(rename({ extname: ".css" }))
.pipe(dest('critical/'));
}
function watchAll() {
watch([path.watch.html], series(html, function watcher(cb) {
cb()
console.log("changed html");
}));
watch([path.watch.css], series(css, function watcher(cb) {
cb()
console.log("changed css");
}));
watch([path.watch.js], series(js, function watcher(cb) {
cb()
console.log("changed js");
}));
watch([path.watch.img], series(img, function watcher(cb) {
cb()
console.log("changed img");
}));
watch([path.watch.svg], series(svg, html, function watcher(cb) {
cb()
console.log("changed svg");
}));
watch([path.watch.fonts], series(fonts, function watcher(cb) {
cb()
console.log("changed fonts");
}));
}
var net = require('net');
function getNetworkIP(callback) {
var socket = net.createConnection(80, 'www.google.com');
socket.on('connect', function() {
callback(undefined, socket.address().address);
socket.end();
});
socket.on('error', function(e) {
callback(e, 'error');
});
}
function server() {
getNetworkIP(function (error, ip) {
if (error || process.env.USE_LOCALHOST) {
if (process.env.USE_LOCALHOST) console.log('process.env.USE_LOCALHOST: ', process.env.USE_LOCALHOST);
if (error) console.log('error:', error);
ip = '127.0.0.1';
console.log('Used Local IP:');
} else {
console.log('Used External IP:');
}
console.log(ip);
connect.server({
port: 4444,
host: ip,
root: './dist',
https: false,
livereload: true
});
});
}
function production(bool) {
return (cb)=>{
process.env.NODE_ENV = bool ? 'production' : 'development';
process.env = Object.assign(process.env, envVars[process.env.NODE_ENV]);
cb();
}
}
exports.default = (function defaultTask() {
return series(production(false), cleanDist, svg, parallel(img, js, fonts, html, css, copyStaticFiles), parallel(watchAll, server));
})();
exports.build = (function() {
return series(production(true), cleanDist, svg, parallel(img, js, fonts, copyStaticFiles, css, html));
})()
exports.critical = series(parallel(html, css), generateCritical);