-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
92 lines (82 loc) · 2 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
const browserSync = require('browser-sync').create();
const del = require('del');
const fs = require('fs');
const gulp = require('gulp');
const { parallel, series } = require('gulp');
const postcss = require('gulp-postcss');
const data = require('gulp-data');
const handlebars = require('gulp-compile-handlebars');
const rename = require('gulp-rename');
const { NODE_ENV } = process.env;
const config = {
src: './src',
dest: './dist',
};
const clean = () => del(config.dest);
const cleanProd = () =>
del([
`${config.dest}/assets/css/*.css`,
`!${config.dest}/assets/css/index.css`,
]);
const compileCSS = () =>
gulp
.src(`${config.src}/assets/css/*.css`)
.pipe(postcss())
.pipe(gulp.dest(`${config.dest}/assets/css`));
const compileHTML = () =>
gulp
.src(`${config.src}/pages/**/*.hbs`)
.pipe(
data(file =>
JSON.parse(fs.readFileSync(file.path.replace('.hbs', '.json')))
)
)
.pipe(data(() => JSON.parse(fs.readFileSync(`${config.src}/config.json`))))
.pipe(
handlebars(
{},
{
ignorePartials: true,
batch: [`${config.src}/partials`],
}
)
)
.pipe(
rename({
extname: '.html',
})
)
.pipe(gulp.dest(config.dest));
const serve = () =>
browserSync.init({
open: true,
notify: true,
files: [`${config.dest}/**/*`],
server: config.dest,
});
const watchers = [
{
match: [`${config.src}/**/*.hbs`, `${config.src}/**/*.json`],
tasks: series(compileHTML),
},
{
match: [
`${config.src}/assets/css/*.css`,
`${config.src}/assets/css/config/*.js`,
],
tasks: series(compileCSS),
},
];
const watch = () =>
watchers.forEach(item => {
gulp.watch(item.match, item.tasks);
});
const defaultTasks =
NODE_ENV === 'production'
? series(parallel([compileHTML, compileCSS]), cleanProd)
: series(
clean,
parallel([compileHTML, compileCSS]),
parallel(serve, watch)
);
exports.default = defaultTasks;