-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
281 lines (248 loc) · 8.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
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
/*=====================================================================*/
/* Gulp with Tailwind Utility framework */
/*=====================================================================*/
const {src, dest, task, watch, series, parallel} = require('gulp');
const cacheVersion = { addSuffix: '?v=' + new Date().getTime() }; // Cache destroy for new live releases
const options = require("./package.json").options; //Options : paths and other options from package.json
const browserSync = require('browser-sync').create();
const gulpsmith = require('gulpsmith');
const twig = require('metalsmith-twig');
const handlebars = require('metalsmith-handlebars');
const layouts = require('metalsmith-layouts');
const discoverPartials = require('metalsmith-discover-partials');
const discoverHelpers = require('metalsmith-discover-helpers');
const metalrename = require("metalsmith-rename");
const sitemap = require('metalsmith-mapsite');
const permalinks = require('metalsmith-permalinks');
const collections = require('metalsmith-collections');
gulp_front_matter = require('gulp-front-matter');
assign = require('lodash.assign');
const merge = require('lodash.merge');
const fs = require('fs');
const yaml = require('js-yaml');
const sass = require('gulp-sass'); //For Compiling SASS files
const concat = require('gulp-concat'); //For Concatinating js,css files
const postcss = require('gulp-postcss'); //For Compiling tailwind utilities with tailwind config
const purify = require('gulp-purifycss');//To remove unused CSS
const uglify = require('gulp-uglify');//To Minify JS files
const imagemin = require('gulp-imagemin'); //To Optimize Images
const purgecss = require('gulp-purgecss'); //To Remove Unsued CSS
const minifycss = require('gulp-clean-css');//To Minify CSS files
const del = require('del'); //For Cleaning dist for fresh builds
const logSymbols = require('log-symbols'); //For Symbolic Console logs
const rename = require('gulp-rename'); // Rename files with min suffix
const htmlReplace = require('gulp-html-replace'); //Replace CSS/JS html includes with min versions
const htmlmin = require('gulp-htmlmin'); //To Minify HTML files
// Set the browser that you want to support
const AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
//const config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf-8'));
const config = yaml.load(fs.readFileSync('config.yml', 'utf-8'));
//Load Previews on Browser on dev
task('livepreview', (done) => {
browserSync.init({
server: {
baseDir: options.paths.dist.base
},
port: 1234
});
done();
});
//Reload functions which triggers browser reload
function previewReload(done) {
console.log("\n\t" + logSymbols.info, "Reloading Preview.\n");
browserSync.stream();
done();
}
// Copy html to hbs for layouts or handlebars via metalsmith layouts doesn't work
task('copy-layouts', () => {
return src(options.paths.src.metalsmith.layouts + '/**/*.html')
.pipe(rename(function(path) {
path.extname = '.hbs';
}))
.pipe(dest(options.paths.src.metalsmith.layouts));
});
task('dev-html', () => {
return src([
options.paths.src.metalsmith.pages + '/**/*.html',
options.paths.src.metalsmith.collections + '/**/*.html'
])
.pipe(gulp_front_matter()).on("data", function(file) {
assign(file, file.frontMatter);
delete file.frontMatter;
})
.pipe(
gulpsmith(options.paths.src.metalsmith.root)
.metadata(merge({
site: {
copyright: '© ' + (new Date()).getFullYear()
}
},
config.metalsmith.metadata
))
.use(discoverPartials({
directory: './partials',
pattern: /\.html$/
}))
.use(discoverHelpers({
directory: './helpers',
pattern: /\.js$/
}))
.use(layouts({
"engine": "handlebars",
"engineOptions": {
extname: '.html'
},
"default": "default.hbs",
"directory": "./layouts",
"pattern": "**/*.html",
"partials": './partials',
"partialExtension": ".html",
"rename": true,
"suppressNoFilesError": true
}))
.use(collections(config.metalsmith.collections))
.use(permalinks(config.metalsmith.permalinks))
.use(sitemap(config.metalsmith.sitemap))
)
.pipe(dest(options.paths.dist.base));
});
// Copy Delete hbs for layouts
task('del-layouts', (done) => {
del.sync([
options.paths.src.metalsmith.layouts + '/**/*.hbs',
'!' + options.paths.src.metalsmith.layouts
]);
done();
});
//Production version cache and minification of HTML
task('build-html', () => {
return src(options.paths.dist.base+'/**/*.html')
.pipe(htmlReplace({
css: '/css/styles.min.css' + cacheVersion.addSuffix,
js: '/js/scripts.min.js' + cacheVersion.addSuffix
}))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(dest(options.paths.dist.base));
});
//Compiling styles
task('dev-styles', () => {
var tailwindcss = require('tailwindcss');
return src(options.paths.src.css + '/**/*')
.pipe(sass().on('error', sass.logError))
.pipe(postcss([
tailwindcss(options.config.tailwindjs),
require('autoprefixer')
]))
.pipe(concat({ path: 'styles.css'}))
.pipe(dest(options.paths.dist.css));
});
//Compiling styles
task('build-styles', () => {
return src([
options.paths.dist.css + '/**/*.css',
'!' + options.paths.dist.css + '/**/*.min.css'
])
.pipe(purgecss({
content: ["src/**/*.html","src/**/*.hbs","src/**/.*js"],
whitelist: ['html', 'body'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
}))
.pipe(minifycss({
compatibility: 'ie8'
}))
.pipe(rename({
suffix: '.min'
}))
.pipe(dest(options.paths.dist.css));
});
//merging all script files to a single file
task('dev-scripts', () => {
return src([
options.paths.src.js + '/libs/**/*.js',
options.paths.src.js + '/**/*.js'
])
.pipe(concat({ path: 'scripts.js'}))
.pipe(dest(options.paths.dist.js));
});
//merging all script files to a single file
task('build-scripts', () => {
return src([
options.paths.src.js + '/libs/**/*.js',
options.paths.src.js + '/**/*.js'
])
.pipe(concat({ path: 'scripts.min.js'}))
.pipe(uglify())
.pipe(dest(options.paths.dist.js));
});
task('dev-imgs', (done) => {
src(options.paths.src.img + '/**/*')
.pipe(dest(options.paths.dist.img));
done();
});
task('build-imgs', (done) => {
src(options.paths.src.img + '/**/*')
.pipe(imagemin())
.pipe(dest(options.paths.dist.img));
done();
});
task('dev-assets', (done) => {
src(options.paths.src.assets + '/**/*')
.pipe(dest(options.paths.dist.base));
done();
});
//Watch files for changes
task('watch-changes', (done) => {
//Watching HTML Files edits
watch(options.config.tailwindjs,series('dev-styles', function (done) {
browserSync.reload();
done();
}));
//Watching HTML Files edits
watch(options.paths.src.base+'/**/*.html',series('dev-styles','copy-layouts', 'dev-html', 'del-layouts', function (done) {
browserSync.reload();
done();
}));
//Watching css Files edits
watch(options.paths.src.css+'/**/*',series('dev-styles', function (done) {
browserSync.reload();
done();
}));
//Watching JS Files edits
watch(options.paths.src.js+'/**/*.js',series('dev-scripts', function (done) {
browserSync.reload();
done();
}));
//Watching Img Files updates
watch(options.paths.src.img+'/**/*',series('dev-imgs', function (done) {
browserSync.reload();
done();
}));
console.log("\n\t" + logSymbols.info,"Watching for Changes made to files.\n");
done();
});
//Cleaning dist folder for fresh start
task('clean:dist', () => {
console.log("\n\t" + logSymbols.info, "Cleaning dist folder for fresh start.\n");
return del(['dist']);
});
//series of tasks to run on dev command
task('development', series('clean:dist', 'copy-layouts', 'dev-html', 'del-layouts', 'dev-styles', 'dev-scripts', 'dev-imgs', 'dev-assets', (done) => {
console.log("\n\t" + logSymbols.info, "npm run dev is complete. Files are located at ./dist\n");
done();
}));
task('production', series('development', 'build-styles', 'build-scripts', 'build-imgs', 'build-html', (done) => {
console.log("\n\t" + logSymbols.info, "npm run build is complete.\n");
done();
}));
exports.default = series('development', 'livepreview', 'watch-changes');
exports.build = series('production');