-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
executable file
·247 lines (222 loc) · 5.22 KB
/
gulpfile.babel.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
import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';
import browserSync from 'browser-sync';
import plumber from 'gulp-plumber';
import notify from 'gulp-notify';
import inlineSource from 'gulp-inline-source';
import inlineCss from 'gulp-inline-css';
import replace from 'gulp-replace';
import imagemin from 'gulp-imagemin';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import autoprefixer from 'gulp-autoprefixer';
import cleanCSS from 'gulp-clean-css';
import ftp from 'vinyl-ftp';
import mail from 'gulp-mail';
import zip from 'gulp-zip';
import { folderConf, emConf, ftpConf, smtpConf } from './config';
const server = browserSync.create();
const folder = folderConf.folder;
const paths = {
buildDir: folderConf.folder + '/build/',
srcDir: folderConf.folder + '/src/',
distDir: folderConf.folder + '/dist/',
srcHtml: folderConf.folder + '/src/index.html',
distHtml: folderConf.folder + '/dist/index.html',
srcImages: folderConf.folder + '/src/*.{jpg,jpeg,png,gif}',
distImages: folderConf.folder + '/dist/*.{jpg,jpeg,png,gif}',
srcStyles: [
folderConf.folder + '/src/scss/**/*.scss',
folderConf.folder + '/src/main.scss'
],
distStyles: folderConf.folder + '/dist/main.css'
};
const email = {
emailUrl: emConf.emailUrl,
emailDir: emConf.emailDir,
emailSender: emConf.emailSender,
emailRecipient: emConf.emailRecipient,
emailSubject: emConf.emailSubject
};
const conn = ftp.create({
host: ftpConf.host,
user: ftpConf.user,
password: ftpConf.password,
parallel: ftpConf.parallel
});
const smtp = {
auth: smtpConf.auth,
host: smtpConf.host,
secureConnection: smtpConf.secureConnection,
port: smtpConf.port
};
const compressFiles = [paths.distHtml, paths.distImages];
const onError = (err) => {
notify.onError({
title: folder,
subtitle: 'Error!',
message: '<%= error.message %>'
})(err);
this.emit('end');
};
// Delete App folder Task
export const clear = () => del([paths.distDir]);
// Styles Task
export function styles() {
return gulp
.src(paths.srcStyles)
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer())
.pipe(cleanCSS())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.distDir))
.pipe(gulp.dest(paths.srcDir));
}
// Compress Images Task
export function images() {
return gulp
.src(paths.srcImages, {
since: gulp.lastRun(images)
})
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(
imagemin({
optimizationLevel: 5,
progressive: true,
interlaced: true,
svgoPlugins: [
{
removeViewBox: false
}
]
})
)
.pipe(gulp.dest(paths.distDir));
}
// Html inline style Task
export function htmlinline() {
return gulp
.src(paths.srcHtml)
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(inlineSource())
.pipe(
inlineCss({
preserveMediaQueries: true,
distlyWidthAttributes: true
})
)
.pipe(gulp.dest(paths.distDir));
}
// Html absolute url for test email Task
export function htmlreplace() {
return gulp
.src(paths.distHtml)
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(replace('url("', 'url("' + email.emailUrl))
.pipe(replace("url('", "url('" + email.emailUrl))
.pipe(replace('src="', 'src="' + email.emailUrl))
.pipe(replace('background="', 'background="' + email.emailUrl))
.pipe(rename('mail.html'))
.pipe(gulp.dest(paths.distDir));
}
// FTP Upload Task
export function upload() {
return gulp
.src(paths.distDir + '**', {
buffer: false
})
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(conn.dest(email.emailDir));
}
// Sendmail Task
export function sendmail() {
return gulp.src(paths.distDir + 'mail.html').pipe(
mail({
subject: email.emailSubject,
to: [email.emailRecipient],
from: email.emailSender,
smtp: smtp
})
);
}
// Compress in zip Task
function compress() {
return gulp
.src(compressFiles)
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(zip(folder + '.zip'))
.pipe(gulp.dest(paths.buildDir));
}
// BrowserSync Reload Task
function reload(done) {
server.reload();
done();
}
// BrowserSync Serve Task
function serve(done) {
server.init({
server: {
baseDir: paths.srcDir
},
open: false,
notify: false
});
done();
}
// Watch Task
function watch() {
gulp.watch(paths.srcStyles, gulp.series(styles, reload));
gulp.watch(paths.srcImages, gulp.series(images, reload));
gulp.watch(paths.srcHtml, gulp.series(htmlinline, htmlreplace, reload));
}
const dev = gulp.series(
clear,
styles,
images,
htmlinline,
htmlreplace,
serve,
watch
);
gulp.task('dev', dev);
const build = gulp.series(styles, images, htmlinline, htmlreplace, compress);
gulp.task('build', build);
const deploy = gulp.series(styles, images, htmlinline, htmlreplace, upload);
gulp.task('deploy', deploy);
const send = gulp.series(
styles,
images,
htmlinline,
htmlreplace,
upload,
sendmail
);
gulp.task('send', send);
export default dev;