forked from stisla/stisla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
211 lines (182 loc) · 4.22 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
/**
* Imports
*/
const {src, dest, watch, parallel} = require('gulp');
const notify = require('gulp-notify');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const uglify = require('gulp-uglify-es').default;
const imagemin = require('gulp-imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const nunjucks = require('gulp-nunjucks');
const color = require('gulp-color');
const nodePath = require('path');
/**
* Configuration
* @type {String}
*/
var cssDir = 'assets/css',
jsDir = 'assets/js',
htmlDir = 'sources/pages',
sassDir = 'sources/scss',
imgDir = 'assets/img';
/**
* Helpers
*/
function _compile_html(path, onEnd, log=true, ret=false) {
if(log)
_log('[HTML] Compiling: ' + path, 'GREEN');
let compile_html = src(path, { base: htmlDir })
.pipe(plumber())
.pipe(nunjucks.compile({
version: '2.3.0',
site_name: 'Stisla'
},
/**
* Nunjucks options
*/
{
trimBlocks: true,
lstripBlocks: true,
/**
* Nunjucks filters
* @type {Object}
*/
filters: {
is_active: (str, reg, page) => {
reg = new RegExp(reg, 'gm');
reg = reg.exec(page);
if(reg != null) {
return str;
}
}
}
}))
.on('error', console.error.bind(console))
.on('end', () => {
if(onEnd)
onEnd.call(this);
if(log)
_log('[HTML] Finished', 'GREEN');
})
.pipe(dest('pages'))
.pipe(plumber.stop());
if(ret) return compile_html;
}
function _compile_scss(path, onEnd, log=true, ret=false) {
if(log)
_log('[SCSS] Compiling:' + path, 'GREEN');
let compile_scss = src(path)
.pipe(plumber())
.pipe(sass({
errorLogToConsole: true
}))
.on('error', console.error.bind(console))
.on('end', () => {
if(onEnd)
onEnd.call(this);
if(log)
_log('[SCSS] Finished', 'GREEN');
})
.pipe(rename({
dirname: '',
extname: '.css'
}))
.pipe(postcss([autoprefixer()]))
.pipe(dest(cssDir))
.pipe(plumber.stop());
if(ret) return compile_scss;
}
function _log(str, clr) {
if(!clr) clr = 'WHITE';
console.log(color(str, clr));
}
/**
* End of helper
*/
/**
* Execution
*/
function folder() {
return src('*.*', {read: false})
.pipe(dest('./assets'))
.pipe(dest('./assets/css'))
.pipe(dest('./assets/js'))
.pipe(dest('./assets/img'));
}
function image() {
return src(imgDir + '/**/*.*')
.pipe(plumber())
.pipe(imagemin([
imageminMozjpeg({quality: 80})
]))
.pipe(dest(imgDir))
.pipe(plumber.stop());
}
function compile_scss() {
return _compile_scss(sassDir + '/**/*.scss', null, false, true);
}
function compile_html() {
return _compile_html(htmlDir + '/**/*.html', null, false, true);
}
function watching() {
compile_scss();
compile_html();
/**
* BrowserSync initialization
* @type {Object}
*/
browserSync.init({
server:{
baseDir: "./"
},
startPath: 'pages/index.html',
port: 8080
});
/**
* Watch ${htmlDir}
*/
watch([
htmlDir + '/**/*.html',
sassDir + '/**/*.scss',
jsDir + '/**/*.js',
imgDir + '/**/*.*',
]).on('change', (file) => {
file = file.replace(/\\/g, nodePath.sep);
if(file.indexOf('.scss') > -1) {
_compile_scss(sassDir + '/**/*.scss', () => {
return browserSync.reload();
});
}
if(file.indexOf('layouts') > -1 && file.indexOf('.html') > -1) {
_compile_html(htmlDir + '/*.html', () => {
return browserSync.reload();
});
}else if(file.indexOf('.html') > -1) {
_compile_html(file, () => {
return browserSync.reload();
});
}
if(file.indexOf(jsDir) > -1 || file.indexOf(imgDir) > -1) {
return browserSync.reload();
}
});
}
// Create folder first
exports.folder = folder;
// Minify images
exports.image = image;
// Compile SCSS
exports.scss = compile_scss;
// Compile HTML
exports.html = compile_html;
// Dist
exports.dist = parallel(folder, compile_scss, compile_html);
// Run this command for dev.
exports.default = watching;