This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
163 lines (151 loc) · 6.84 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
//////////////////////////////////////////////////////
// Calling required packages
//////////////////////////////////////////////////////
var gulp = require('gulp'), // https://npmjs.com/package/gulp
sass = require('gulp-sass'), // https://npmjs.com/package/gulp-sass
browserSync = require('browser-sync').create(), // https://npmjs.com/package/browser-sync
concat = require('gulp-concat'), // https://npmjs.com/package/gulp-concat
uglify = require('gulp-uglifyjs'), // https://npmjs.com/package/gulp-uglify
htmlmin = require('gulp-htmlmin'), // https://npmjs.com/package/gulp-htmlmin
cssnano = require('gulp-cssnano'), // https://npmjs.com/package/gulp-cssnano
rename = require('gulp-rename'), // https://npmjs.com/package/gulp-rename
imagemin = require('gulp-imagemin'), // https://npmjs.com/package/gulp-imagemin
pngquant = require('imagemin-pngquant'), // https://npmjs.com/package/imagemin-pngquant
autoprefixer = require('gulp-autoprefixer'), // https://npmjs.com/package/gulp-autoprefixer
jade = require('gulp-jade'), // https://npmjs.com/package/gulp-jade
sourcemaps = require('gulp-sourcemaps'), // https://npmjs.com/package/gulp-sourcemaps
del = require('del'); // https://npmjs.com/package/del
//////////////////////////////////////////////////////
// Declaring variables
//////////////////////////////////////////////////////
var appDirectory = 'app'
buildDirectory = 'build',
taskClean = 'clean',
taskJade = 'jade',
taskHtmlMin = 'htmlminify',
taskSass = 'sass',
taskCssnano = 'cssnano',
taskUglifyjs = 'uglifyjs',
taskImg = 'img',
taskBrowserSync = 'browser-sync',
taskWatch = 'watch',
taskBuild = 'build';
//////////////////////////////////////////////////////
// The task for deleting a build directory.
//////////////////////////////////////////////////////
gulp.task (taskClean, function () {
return del.sync( buildDirectory );
});
//////////////////////////////////////////////////////
// The task for converting the jade code into html.
//////////////////////////////////////////////////////
gulp.task (taskJade, function () {
var YOUR_LOCALS = {};
return gulp.src
([
appDirectory + '/jade/*.jade',
'!' + appDirectory + '/jade/includes'
])
.pipe(jade({ locals: YOUR_LOCALS }))
.pipe(gulp.dest( appDirectory ))
.pipe(browserSync.reload({ stream: true }))
});
//////////////////////////////////////////////////////
// The task for minify your html.
// Pre-executed taskJade.
//////////////////////////////////////////////////////
gulp.task (taskHtmlMin, [taskJade], function () {
return gulp.src( appDirectory + '/**/*.html' )
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest( appDirectory ))
.pipe(browserSync.reload({ stream: true }))
});
//////////////////////////////////////////////////////
// The task for converting the sass code into css.
//////////////////////////////////////////////////////
gulp.task (taskSass, function () {
return gulp.src( appDirectory + '/sass/main.sass' )
.pipe(sass())
.pipe(gulp.dest( appDirectory + '/css' ))
.pipe(browserSync.reload({ stream: true }))
});
//////////////////////////////////////////////////////
// The task for minify your css and add sourcemaps.
// Pre-executed taskSass.
//////////////////////////////////////////////////////
gulp.task (taskCssnano, [taskSass], function () {
return gulp.src( appDirectory + '/css/main.css')
.pipe(sourcemaps.init())
// .pipe(autoprefixer(['last 15 versions'], {cascade: true}))
.pipe(cssnano())
.pipe(sourcemaps.write())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest( appDirectory + '/css'))
.pipe(browserSync.reload({ stream: true }))
});
//////////////////////////////////////////////////////
// The task for minify your js and add sourcemaps.
//////////////////////////////////////////////////////
gulp.task (taskUglifyjs, function () {
return gulp.src( appDirectory + '/js/main.js')
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest( appDirectory + '/js'))
.pipe(browserSync.reload({ stream: true }))
});
//////////////////////////////////////////////////////
// The task for minify your images
//////////////////////////////////////////////////////
gulp.task (taskImg, function () {
return gulp.src( appDirectory + '/src/images/**/*')
.pipe(imagemin({
interlaced: true,
progressive: true,
svgoPlugins: [{ removeViewBox: false }],
use: [pngquant()]
}))
.pipe(gulp.dest( appDirectory + '/src/images' ));
});
//////////////////////////////////////////////////////
// The task is to activate synchronization between
// your browser and browsers on the local network.
// The ip will be available on the link after launch.
//////////////////////////////////////////////////////
gulp.task (taskBrowserSync, function () {
browserSync.init({
server: {
baseDir: appDirectory
},
open: false,
notify: false
});
});
//////////////////////////////////////////////////////
// The task for tracking changes in specified
// directories and calling the specified tasks.
// Pre-executed taskBrowserSync.
//////////////////////////////////////////////////////
gulp.task (taskWatch, [taskBrowserSync], function () {
gulp.watch( appDirectory + '/jade/**/*.jade', [taskHtmlMin] );
gulp.watch( appDirectory + '/sass/**/*.sass', [taskCssnano] );
gulp.watch( appDirectory + '/js/**/main.js', [taskUglifyjs] );
});
//////////////////////////////////////////////////////
// The task for building a project.
// Pre-execution tasks: taskClean, taskHtmlMin,
// taskUglifyjs, taskCssnano, taskImg.
//////////////////////////////////////////////////////
gulp.task (taskBuild, [taskClean, taskHtmlMin, taskUglifyjs, taskCssnano, taskImg] , function () {
let buildCSS = gulp.src( appDirectory + '/css/**/*.css' ).pipe(gulp.dest( buildDirectory + '/css')),
buildFonts = gulp.src( appDirectory + '/fonts/**/*' ).pipe(gulp.dest( buildDirectory + '/fonts')),
buildJS = gulp.src( appDirectory + '/js/**/*.js' ).pipe(gulp.dest( buildDirectory + '/js')),
buildHtml = gulp.src([ appDirectory + '/index.html', appDirectory + '/robots.txt', appDirectory + '/sitemap.xml', appDirectory + '/favicon.ico' ]).pipe(gulp.dest( buildDirectory )),
buildSrc = gulp.src( appDirectory + '/src/**/*' ).pipe(gulp.dest( buildDirectory + '/src')); });
//////////////////////////////////////////////////////
// The task for development is called by the command
// 'gulp', for this you need write next:
// 'npm i -g gulp'
//////////////////////////////////////////////////////
gulp.task ('default', [taskHtmlMin, taskUglifyjs, taskCssnano, taskImg, taskWatch]);