-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
gulpfile.js
271 lines (244 loc) · 8.45 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
const del = require('del');
const fs = require("fs");
const gulp = require('gulp');
const gulpAutoPrefixer = require('gulp-autoprefixer');
const gulpBabel = require('gulp-babel');
const gulpBabelMinify = require('gulp-babel-minify');
const gulpCleanCSS = require('gulp-clean-css');
const gulpFileInclude = require('gulp-file-include');
const gulpRename = require('gulp-rename');
const gulpReplace = require('gulp-replace');
const gulpSass = require('gulp-sass')(require('sass'));
const gulpStripComments = require('gulp-strip-comments');
const gulpTokenReplace = require('gulp-token-replace');
const terserWebpackPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const gulpJsonMinify = require('gulp-jsonminify')
const packages = './package.json';
// Clean everything inside ./build directory
gulp.task('clean', () => {
const sources = [
'./build/**'
];
return del(sources, {
force: true,
})
});
// Generate styles
gulp.task('styles', () => {
const sources = [
'./src/assets/styles/*.{css,scss}'
];
return gulp.src(sources)
.pipe(gulpSass())
.pipe(gulp.dest('./build/styles'))
});
// Generate prefixed styles
gulp.task('styles:autoprefixed', () => {
const sources = [
'./build/styles/*.{css}'
];
return gulp.src(sources)
.pipe(gulpAutoPrefixer({
cascade: false
}))
.pipe(gulp.dest('./build/styles'))
});
// Generate minified styles
gulp.task('styles:minified', () => {
const sources = [
'./build/styles/*.{css,scss}'
];
return gulp.src(sources)
.pipe(gulpCleanCSS({
level: {
1: {
specialComments: 0
}
}
}))
.pipe(gulp.dest('./build/styles'))
});
// Generate scripts
gulp.task('scripts', () => {
const sources = [
'./build/scripts/easy-toggle-state.js',
'./build/scripts/lazysizes.js',
'./build/scripts/scripts.js',
];
return gulp.src(sources)
.pipe(webpackStream({
mode: 'production',
output: {
clean: true,
filename: 'bundle.js'
},
optimization: {
minimize: true,
minimizer: [
new terserWebpackPlugin({
extractComments: false,
terserOptions: {
mangle: true,
compress: true,
format: {
comments: false,
},
},
})
],
},
watch: false
}, webpack))
.pipe(gulp.dest('./build/scripts'))
});
gulp.task('scripts:babel', () => {
const sources = [
'./src/assets/scripts/*.js',
'./src/assets/scripts/libraries/*.js',
];
return gulp.src(sources)
.pipe(gulpBabel())
.pipe(gulp.dest('./build/scripts'))
});
// Generate minified scripts
gulp.task('scripts:minified', () => {
const sources = [
'./build/scripts/*.js'
];
return gulp.src(sources)
.pipe(gulpBabelMinify({
mangle: {
keepClassName: false
},
evaluate: true,
builtIns: true,
removeDebugger: true,
removeConsole: true
}))
.pipe(gulp.dest('./build/scripts'))
});
// Generate JSON (Schema)
gulp.task('json:minify', () => {
const sources = [
'./src/assets/scripts/json/*.json'
];
return gulp.src(sources)
.pipe(gulpJsonMinify())
.pipe(gulp.dest('./build/scripts/json'))
});
// Generate Replaced JSON Data
gulp.task('json:replace', () => {
const sources = [
'./build/scripts/json/*.json'
];
return gulp.src(sources)
.pipe(gulpReplace('dataBlogHomepageUrl', '<data:blog.homepageUrl.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogLocaleLanguage', '<data:blog.locale.language.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogMetaDescription', "<b:eval expr='data:blog.metaDescription ? data:blog.metaDescription : (data:post.body snippet {length: 150, links: false, linebreaks: false, ellipsis: true}).jsonEscaped'/>"))
.pipe(gulpReplace('dataBlogSearchUrl', '<data:blog.searchUrl.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogTitle', '<data:blog.title.jsonEscaped/>'))
.pipe(gulpReplace('dataPostAuthorName', '<data:post.author.name.jsonEscaped/>'))
.pipe(gulpReplace('dataPostBodySnippet300', "<b:eval expr='(data:post.body snippet {length: 300, links: false, linebreaks: false, ellipsis: true}).jsonEscaped'/>"))
.pipe(gulpReplace('dataPostDateIso8601', '<data:post.date.iso8601.jsonEscaped/>'))
.pipe(gulpReplace('dataPostFeaturedImage', "<b:eval expr='data:post.featuredImage.isResizable ? resizeImage(data:post.featuredImage, 1200, "1200:630") : "https://lh3.googleusercontent.com/ULB6iBuCeTVvSjjjU1A-O8e9ZpVba6uvyhtiWRti_rBAs9yMYOFBujxriJRZ-A=w1200"'/>"))
.pipe(gulpReplace('dataPostLastUpdatedIso8601', '<data:post.lastUpdated.iso8601.jsonEscaped/>'))
.pipe(gulpReplace('dataPostLabelsFirstName', '<b:eval expr="data:post.labels ? data:post.labels.first.name : data:messages.home" />'))
.pipe(gulpReplace('dataPostLabelsLastName', '<b:eval expr="data:post.labels ? data:post.labels.last.name : data:messages.home" />'))
.pipe(gulpReplace('dataPostLabelsFirstUrl', '<b:eval expr="data:post.labels ? data:post.labels.first.url.canonical : data:blog.homepageUrl.canonical" />'))
.pipe(gulpReplace('dataPostTitle', '<data:post.title.jsonEscaped/>'))
.pipe(gulpReplace('dataPostUrlCanonical', '<data:post.url.canonical.jsonEscaped/>'))
.pipe(gulpReplace('dataMessagesHome', '<data:messages.home/>'))
.pipe(gulp.dest('./build/scripts/json'))
});
// Generate Timestamp
gulp.task('timestamp', (done) => {
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
];
const date = new Date();
const timestamps = `${monthNames[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()} - ${("0" + date.getHours()).slice(-2)}:${("0" + date.getMinutes()).slice(-2)}`;
!((filePath, results) => {
fs.readFile(filePath, (error, fileData) => {
if (error) {
return results && results(error)
} else {
try {
const object = JSON.parse(fileData);
return results && results(null, object);
} catch (errors) {
return results && results(errors)
}
}
})
})(packages, (error, object) => {
if (error) {
return console.log("Error Reading File: ", error)
} else {
object.releasedDate = timestamps;
fs.writeFile(packages, JSON.stringify(object, null, '\t'), {
flag: 'w'
}, (errors) => {
if (errors) {
console.log("Error Writting File: ", errors)
}
})
}
})
return done()
});
// Remove all comments
gulp.task('comments', () => {
const sources = [
'./dist/*.{xml,html}'
];
return gulp.src(sources)
.pipe(gulpStripComments({
trim: true
}))
.pipe(gulp.dest('./dist'))
});
gulp.task('start', () => {
const tokenData = require(packages);
const sources = [
'./src/index.html'
];
return gulp.src(sources)
.pipe(gulpTokenReplace({
global: tokenData
}))
.pipe(gulpFileInclude({
indent: true,
basepath: '@@file',
prefix: '@@'
}))
.pipe(gulpReplace('-tw', '-elcreative'))
.pipe(gulpRename({
basename: 'theme',
extname: '.xml'
}))
.pipe(gulp.dest('./dist'))
})
// Final task: Production Mode
gulp.task('build:production', gulp.series(
'clean',
'styles',
'styles:autoprefixed',
'styles:minified',
'scripts:babel',
'scripts',
'scripts:minified',
'json:minify',
'json:replace',
'timestamp',
'start',
'comments'
));
// Final task: Development Mode
gulp.task('build:development', gulp.series(
'clean',
'styles',
'scripts',
'start'
));