-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
325 lines (291 loc) · 10.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const gulp = require('gulp');
const del = require('del');
const gulpSass = require('gulp-sass')(require('sass'));
const gulpAutoPrefixer = require('gulp-autoprefixer');
const gulpCleanCSS = require('gulp-clean-css');
const webpackStream = require('webpack-stream');
const terserWebpackPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const gulpBabelMinify = require('gulp-babel-minify');
const gulpJsonMinify = require('gulp-jsonminify');
const gulpReplace = require('gulp-replace');
const gulpTokenReplace = require('gulp-token-replace');
const packages = require('./package.json');
const gulpFileInclude = require('gulp-file-include');
const gulpRename = require('gulp-rename');
const gulpStripComments = require('gulp-strip-comments');
const fs = require('fs');
const gulpBabel = require('gulp-babel');
const gulpHTMLMin = require('gulp-htmlmin');
const generateTimestamp = () => {
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
];
const date = new Date();
const monthName = monthNames[date.getMonth()];
const day = date.getDate();
const year = date.getFullYear();
// const hours = ("0" + date.getHours()).slice(-2);
const hours = ("0" + ((date.getHours() + 11) % 12 + 1)).slice(-2);
const minutes = ("0" + date.getMinutes()).slice(-2);
return `${monthName} ${day}, ${year} - ${hours}:${minutes} ${hours < 12 ? 'AM' : 'PM'}`;
};
const updatePackageTimestamp = () => {
const packagesFile = './package.json';
const timestamp = generateTimestamp();
fs.readFile(packagesFile, (error, fileData) => {
if (error) {
console.log('Error reading file:', error);
return
};
try {
const packages = JSON.parse(fileData);
packages.releasedDate = timestamp;
fs.writeFile(packagesFile, JSON.stringify(packages, null, '\t'), (error) => {
if (error) {
console.log('Error writing file:', error)
}
})
} catch (error) {
console.log('Error parsing JSON:', error);
}
})
};
// 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}',
'!./src/assets/styles/tailwind.css'
];
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 (webpack)
gulp.task('scripts:webpack', () => {
const sources = {
scripts: './src/assets/scripts/scripts.js',
// scriptsHead: './src/assets/scripts/scripts-head.js'
};
const webpackConfig = {
mode: 'production',
entry: sources,
output: {
filename: '[name].min.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: '> 0.25%, not dead',
},
],
],
plugins: [
// your plugins here
],
// add the terserOptions here
compact: true,
comments: false
}
}
]
}
]
},
optimization: {
minimize: true,
minimizer: [
new terserWebpackPlugin({
extractComments: false,
terserOptions: {
mangle: false,
compress: true,
format: {
comments: false,
},
},
})
],
splitChunks: {
chunks: 'all',
},
},
watch: false
};
return gulp.src(sources.scripts)
.pipe(webpackStream(webpackConfig, webpack))
.pipe(gulp.dest('./build/scripts'))
});
// Generate scripts (babel)
gulp.task('scripts:babel', () => {
const sources = [
'./build/scripts/*.js'
];
return gulp.src(sources)
.pipe(gulpBabel())
.pipe(gulp.dest('./build/scripts'))
});
// Generate minified scripts
gulp.task('scripts:minified', () => {
const sources = [
'./build/scripts/*.js',
'./src/assets/scripts/scripts-head.js',
];
return gulp.src(sources)
.pipe(gulpBabelMinify({
mangle: {
keepClassName: true
},
evaluate: false,
builtIns: false,
removeDebugger: true,
removeConsole: false
}))
.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('dataBlogTitle', '<data:blog.title.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogHomepageUrl', '<data:blog.homepageUrl.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogSearchUrl', '<data:blog.searchUrl.jsonEscaped/>'))
.pipe(gulpReplace('dataBlogLocale', '<data:blog.locale/>'))
.pipe(gulpReplace('dataBlogMetaDescription', "<b:eval expr='data:blog.metaDescription ? data:blog.metaDescription.escaped : data:view.description.escaped'/>"))
.pipe(gulpReplace('dataPostUrlCanonical', '<data:post.url.canonical.jsonEscaped/>'))
.pipe(gulpReplace('dataPostTitle', '<data:post.title.jsonEscaped/>'))
.pipe(gulpReplace('dataPostBodySnippet', "<b:eval expr='(data:post.body snippet {length: 150, links: false, linebreaks: false, ellipsis: true}).jsonEscaped'/>"))
.pipe(gulpReplace('dataPostDateIso8601', '<data:post.date.iso8601.jsonEscaped/>'))
.pipe(gulpReplace('dataPostLastUpdatedIso8601', '<data:post.lastUpdated.iso8601.jsonEscaped/>'))
.pipe(gulpReplace('dataPostAuthorName', '<data:post.author.name.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('dataMessagesHome', '<data:messages.home/>'))
.pipe(gulpReplace('dataPostLabelsFirstName', '<b:eval expr="data:post.labels ? data:post.labels.first.name : data:messages.home" />'))
.pipe(gulpReplace('dataPostLabelsFirstUrlCanonical', '<b:eval expr="data:post.labels ? data:post.labels.first.url.canonical : data:blog.homepageUrl.canonical" />'))
.pipe(gulp.dest('./build/scripts/json'))
});
// Remove all comments
gulp.task('comments', () => {
const sources = [
'./dist/*.{xml,html}'
];
return gulp.src(sources)
.pipe(gulpStripComments({
trim: true
}))
.pipe(gulp.dest('./dist'))
});
// Generate Timestamp
gulp.task('timestamp', (results) => {
updatePackageTimestamp();
results()
});
// Minify HTML
gulp.task('html:minify', () => {
const sources = [
// './src/partials/defaultmarkups/Common/defaultIcons.html',
// './src/partials/widgets/LinkList995.html',
];
return gulp.src(sources)
.pipe(gulpHTMLMin({
collapseWhitespace: true,
// collapseBooleanAttributes: true, // collapse boolean attributes to a single value
removeComments: true,
ignoreCustomFragments: [/<[^>]+\/>/, /<[^>]+><\/[^>]+>/], // ignore self-closing tags and empty tags
}))
.pipe(gulp.dest('./build/html'))
})
// Final Tasks
gulp.task('start', () => {
const sources = [
'./src/main.html'
];
return gulp.src(sources)
.pipe(gulpTokenReplace({
global: packages
}))
.pipe(gulpFileInclude({
indent: true,
basepath: '@@file',
prefix: '@@'
}))
.pipe(gulpReplace('-tw', '-elcreative'))
.pipe(gulpRename({
basename: `theme-v${packages.version}`,
// basename: `${packages.names.replace(/\s/g, '-')}-v${packages.version}`,
extname: '.xml'
}))
.pipe(gulp.dest('./dist'))
})
// Build task: Production Mode
gulp.task('build:production', gulp.series(
'clean',
'styles',
'styles:autoprefixed',
'styles:minified',
'scripts:webpack',
// 'scripts:babel',
'scripts:minified',
'json:minify',
'json:replace',
'html:minify',
'timestamp',
'start',
'comments'
));