-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
246 lines (223 loc) · 5.64 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
const { src, dest, parallel, watch, series } = require('gulp');
const gulpif = require('gulp-if');
const fileinclude = require('gulp-file-include');
const rename = require('gulp-rename');
const htmlmin = require('gulp-htmlmin');
const inliner = require('gulp-inline-css');
const replace = require('gulp-replace');
const mqpacker = require('css-mqpacker');
const gulpSCSS = require('gulp-sass');
const postcssGulp = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const beautify = require('gulp-jsbeautifier');
const del = require('del');
const pug = require('gulp-pug');
const clearingHTML = require('gulp-email-remove-unused-css');
const vftp = require('vinyl-ftp');
const fs = require('fs');
const PRODUCTION = process.env.NODE_ENV === 'production';
const config = require('./src/config');
const pkg = require('./package.json');
const Browser = require('browser-sync');
const browser = Browser.create();
// Start SCSS
// ******************************************
{
function scss() {
return src(['./src/scss/styles.scss'])
.pipe(gulpSCSS())
.pipe(postcssGulp([autoprefixer(), mqpacker()]))
.pipe(dest('./build'))
.pipe(gulpif(!PRODUCTION, browser.stream()));
}
exports.scss = scss;
// END SCSS
// ******************************************
}
// Start server
// ******************************************
{
function server() {
let config = {
server: {
ghostMode: false,
baseDir: './build',
serveStaticOptions: {
extensions: ['html']
}
},
injectChanges: true,
serveStatic: ['build'],
port: 7777,
browser: 'google chrome'
};
browser.init(config);
}
exports.server = server;
}
// End server
// ******************************************
// Copy static
// ******************************************
{
function copyStatic() {
return src('./src/static/**/*').pipe(dest('./build'));
}
exports.copyStatic = copyStatic;
}
// End Copy static
// ******************************************
// Start HTML
// ******************************************
{
function html() {
return src('./src/template/*.pug')
.pipe(
gulpif(
!PRODUCTION,
pug(),
pug({
data: {
final: true
}
})
)
)
.pipe(dest('./build'));
}
exports.html = html;
}
// End HTML
// ******************************************
// Start inline styles
// ******************************************
{
function inlineStyle() {
return src(['./build/*.html'])
.pipe(
fileinclude({
prefix: '@@',
basepath: '@file'
})
)
.pipe(
inliner({
preserveMediaQueries: true
})
)
.pipe(
clearingHTML({
whitelist: [
'.ExternalClass',
'.ReadMsgBody',
'.yshortcuts',
'.Mso*',
'.maxwidth-apple-mail-fix',
'#outlook',
'.module-*'
]
})
)
.pipe(
gulpif(
config.minifyResultHtml,
htmlmin({ collapseWhitespace: true, minifyCSS: true }),
beautify({
indent_char: '\t',
indent_size: 1,
max_preserve_newlines: 0
})
)
)
.pipe(
gulpif(!!config.urlImageHost, replace(/img\//g, config.urlImageHost))
)
.pipe(
fileinclude({
prefix: '##',
basepath: '@file'
})
)
.pipe(
rename({
suffix: '-inline'
})
)
.pipe(dest('./build'));
}
exports.inlineStyle = inlineStyle;
}
// End inline styles
// ******************************************
// Watch
// ******************************************
{
if (!PRODUCTION) {
watch(
['./src/scss/**/*.scss'],
{ events: ['change', 'add'], delay: 100 },
scss
);
watch(['./src/**/*.pug'], { events: ['change', 'add'], delay: 100 }, html);
watch(['./build/*.html']).on('change', () => browser.reload());
}
}
// End Watch
// ******************************************
// Start clear directory
// ******************************************
{
function clearBuildDir() {
return del(['./build/**/*']);
}
exports.clearBuildDir = clearBuildDir;
function clearTempFiles() {
const arrayDel = [
'./build/*.html',
'!./build/*-inline.html',
'./build/*.css'
];
return del(arrayDel);
}
exports.clearTempFiles = clearTempFiles;
}
// End clear directory
// ******************************************
// Upload images to ftp server
// ******************************************
{
function ftp(cb) {
fs.access('./.ftppass.json', function(error) {
const ftpPass = error
? require('./.ftppass-temp.json')
: require('./.ftppass.json');
const configFTP = {
host: ftpPass.host,
user: ftpPass.auth.user,
pass: ftpPass.auth.pass,
port: ftpPass.port,
parallel: 1,
remotePath: `${ftpPass.remotePath}/${pkg.name}/`
};
const gulpFTP = vftp.create(configFTP);
src('./build/**', { buffer: false })
.pipe(gulpFTP.dest(configFTP.remotePath))
.on('end', function() {
console.log(`Upload done - ${config.urlImageHost}`);
cb();
});
});
}
exports.ftp = ftp;
}
// End Upload images to ftp server
// ******************************************
// Complex tasks
// ******************************************
exports.default = series(parallel(html, scss, copyStatic), server);
exports.build = series(
clearBuildDir,
parallel(html, scss, copyStatic),
inlineStyle,
clearTempFiles
);