-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
120 lines (99 loc) · 2.67 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const postCss = require('gulp-postcss');
const pug = require('gulp-pug');
const ts = require('gulp-typescript');
const terser = require('gulp-terser');
const autoprefixer = require('autoprefixer');
const browserSync = require('browser-sync');
const { sync: del } = require('del');
const { default: fetch } = require('node-fetch');
const tsProject = ts.createProject('tsconfig.json');
const server = browserSync.create();
const paths = {
src: 'src',
build: 'build'
};
const read = path => JSON.parse(require('fs').readFileSync(path).toString());
const me = (async () => {
const response = await fetch('https://en.gravatar.com/gazmull.json');
if (!response.ok) throw new Error('Response was not ok.');
const json = await response.json();
return json.entry[0];
})();
const projects = () => read(paths.src + '/assets/projects.json');
const pugFiles = {
src: paths.src + '/views/**/!(_)*.pug',
build: paths.build
};
const scssFiles = {
src: paths.src + '/scss/index.scss',
build: paths.build + '/css'
};
const tsFiles = {
build: paths.build + '/js'
};
const terserFiles = {
src: tsFiles.build + '/**/*.js',
dest: tsFiles.build
};
gulp.task('pug', async () => {
return gulp.src(pugFiles.src)
.pipe(pug({
locals: {
me: await me,
projects: projects()
},
pretty: false
}))
.pipe(gulp.dest(pugFiles.build))
.pipe(server.stream());
});
gulp.task('sass', () => {
return gulp.src(scssFiles.src)
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(postCss([ autoprefixer() ]))
.pipe(gulp.dest(scssFiles.build))
.pipe(server.stream());
});
gulp.task('ts', () => {
return tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest(tsFiles.build))
.pipe(server.stream());
});
gulp.task('terser', () => {
return gulp.src(terserFiles.src)
.pipe(terser({
compress: true,
mangle: true,
toplevel: true,
ecma: 6
}))
.pipe(gulp.dest(terserFiles.dest));
});
gulp.task('assets', () => {
return gulp.src(paths.src + '/assets/**/*.!(json)')
.pipe(gulp.dest(paths.build + '/assets'));
});
gulp.task('clean', () => {
del([ paths.build ]);
return Promise.resolve(true);
});
function serve (done) {
server.init({
ghostMode: true,
server: {
baseDir: paths.build
},
reloadDelay: 1e3,
open: true
});
done();
}
const commonTasks = [ 'assets', 'pug', 'sass', 'ts' ];
function watch () {
gulp.watch(paths.src, gulp.series(...commonTasks));
}
gulp.task('default', gulp.series(...commonTasks, serve, watch));
gulp.task('build', gulp.series('clean', ...commonTasks, 'terser'));