-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
280 lines (263 loc) · 7.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
const { src, dest, watch, series, task } = require('gulp'),
gulpsass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
babel = require('gulp-babel'),
concat = require('gulp-concat'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
autoprefixer = require('gulp-autoprefixer'),
cssnano = require('cssnano'),
postcss = require('gulp-postcss'),
rtlcss = require('gulp-rtlcss'),
fileinclude = require('gulp-file-include'),
clean = require('gulp-clean'),
gwatch = require('gulp-watch'),
cleanDest = require('gulp-clean-dest'),
deleteEmpty = require('delete-empty'),
critical = require('critical').stream,
browsersync = require('browser-sync').create();
var paths = {
source: 'src',
destination: 'build'
};
// create dist
function createDist() {
return src([
paths.source + '/**/*.*',
'!' + paths.source + '/*.html',
'!' + paths.source + '/include/**',
'!' + paths.source + '/assets/scss/**',
'!' + paths.source + '/assets/scripts/**'
])
.pipe(dest(paths.destination));
}
// compile scss into css
function sassToCss() {
return src([
paths.source + '/assets/scss/style.scss'
])
.pipe(sourcemaps.init())
.pipe(gulpsass({includePaths:['./node_modules/']}).on('error', gulpsass.logError))
.pipe(autoprefixer({
overrideBrowserslist: ['last 20 versions']
}))
.pipe(dest(paths.destination + '/assets/css'))
.pipe(postcss([cssnano()]))
.pipe(autoprefixer({
overrideBrowserslist: ['last 20 versions']
}))
.pipe(rename('style.min.css'))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.destination + '/assets/css'))
.pipe(browsersync.stream());
}
// css to rtl-css
function rtlCss(){
return src([
paths.source + '/assets/scss/style.scss'
])
.pipe(sourcemaps.init())
.pipe(gulpsass({ includePaths: ['./node_modules/'] }).on('error', gulpsass.logError))
.pipe(autoprefixer({
overrideBrowserslist: ['last 20 versions']
}))
.pipe(rename('style-rtl.css'))
.pipe(rtlcss())
.pipe(dest(paths.destination + '/assets/css'))
.pipe(postcss([cssnano()]))
.pipe(autoprefixer({
overrideBrowserslist: ['last 20 versions']
}))
.pipe(rename('style-rtl.min.css'))
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.destination + '/assets/css'))
}
// minify js
function vendorJs() {
return src([
paths.source + '/assets/scripts/jquery.js',
paths.source + '/assets/scripts/vendors/*.js',
])
.pipe(sourcemaps.init())
.pipe(concat(paths.destination + '/assets/js/vendors.js'))
.pipe(dest('.'))
.pipe(rename('vendors.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.destination + '/assets/js'))
.pipe(browsersync.stream());
}
// minify js
function minifyJs() {
return src([
paths.source + '/assets/scripts/main.js'
])
.pipe(sourcemaps.init())
.pipe(babel({
"presets": [
["env", { "modules": false }]
]
}))
.pipe(concat(paths.destination + '/assets/js/scripts.js'))
.pipe(dest('.'))
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(dest(paths.destination + '/assets/js'))
.pipe(browsersync.stream());
}
// include htmls
function includehtml() {
return src([
paths.source + '/*.html'
])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file',
context: {
css: 'style.min.css',
lang: 'en',
dir: 'ltr',
urlfix: '',
}
}))
.pipe(dest(paths.destination));
}
// rtl html create
function rtlhtml(){
return src([paths.source + '/*.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file',
context: {
css: 'style-rtl.min.css',
lang: 'ar',
dir: 'rtl',
urlfix: 'ar-',
}
}))
.pipe(rename({
prefix: "ar-",
}))
.pipe(dest(paths.destination));
}
// watch folders
function watchallfolders() {
return src([
paths.source + '/**',
'!' + paths.source + '/*.html',
'!' + paths.source + '/include/**',
'!' + paths.source + '/assets/scss/**',
'!' + paths.source + '/assets/scripts/**'
], {base: paths.source})
.pipe(gwatch(paths.source, {base: paths.source}))
.pipe(cleanDest(paths.destination, {allowEmpty: true}))
//.pipe(deleteEmpty(paths.destination))
.pipe(dest(paths.destination))
.pipe(browsersync.stream());
}
// remove dist folder
function distclean() {
return src(paths.destination, {allowEmpty: true})
.pipe(clean({force:true}));
}
// critical plugin ltr
function criticalcss() {
return src([
paths.destination + '/*.html',
'!' + paths.destination + '/ar-*.html'
])
.pipe(critical({
base: paths.destination,
inline: true,
css: paths.destination + '/assets/css/style.min.css',
}))
.on('error', err => {
log.error(err.message);
})
.pipe(dest(paths.destination));
}
// critical plugin rtl
function criticalcssRtl() {
return src(paths.destination + '/ar-*.html')
.pipe(critical({
base: paths.destination,
inline: true,
css: paths.destination + '/assets/css/style-rtl.min.css',
}))
.on('error', err => {
log.error(err.message);
})
.pipe(dest(paths.destination));
}
// copy script folder
function copyUtils(){
return src([
paths.source + '/assets/scripts/utils.js'
])
.pipe(dest(paths.destination + '/assets/js/'))
}
// browsersync
function runbrowser() {
browsersync.init({
server: paths.destination,
port: 4000
});
watch([paths.source + '/assets/scss/**/*.scss']).on('change', () => {
sassToCss();
console.log('css changes');
});
watch([paths.source + '/assets/scripts/main.js']).on('change', () => {
minifyJs();
console.log('script changes');
});
watch([paths.source + '/assets/scripts/vendors/*.js']).on('change', () => {
vendorJs();
console.log('vendor changes');
});
// watch([paths.source + '/assets/scripts/utils.js']).on('change', () => {
// copyUtils();
// console.log('utils js copy');
// });
// watchallfolders();
watch([paths.source + '/**/*.html']).on('change', () => {
includehtml();
browsersync.reload;
console.log('html changes');
});
}
// browsersync
function runbrowserrtl() {
browsersync.init({
server: paths.destination,
port: 4000
});
watch([paths.source + '/assets/scss/**/*.scss']).on('change', () => {
sassToCss();
rtlCss();
console.log('css changes');
});
watch([paths.source + '/assets/scripts/main.js']).on('change', () => {
minifyJs();
console.log('script changes');
});
watch([paths.source + '/assets/scripts/vendors/*.js']).on('change', () => {
vendorJs();
console.log('vendor changes');
});
// watch([paths.source + '/assets/scripts/utils.js']).on('change', () => {
// copyUtils();
// console.log('utils js copy');
// });
// watchallfolders();
watch([paths.source + '/**/*.html']).on('change', () => {
includehtml();
rtlhtml();
browsersync.reload;
console.log('html changes');
});
}
exports.default = series(distclean, createDist, sassToCss, vendorJs, minifyJs, includehtml, runbrowser);
exports.defaultrtl = series(distclean, createDist, sassToCss, rtlCss, vendorJs, minifyJs, includehtml, rtlhtml, runbrowserrtl);
exports.build = series(distclean, createDist, sassToCss, vendorJs, minifyJs, includehtml, criticalcss);
exports.buildrtl = series(distclean, createDist, sassToCss, rtlCss, vendorJs, minifyJs, includehtml, rtlhtml, criticalcss, criticalcssRtl);