-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
134 lines (115 loc) · 3.28 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
const fs = require('fs')
const path = require('path')
const gulp = require('gulp')
const frontMatter = require('gulp-front-matter')
const remark = require('gulp-remark')
const html = require('remark-html')
const slug = require('remark-slug')
const breaks = require('remark-breaks')
const plumber = require('gulp-plumber')
const stylus = require('gulp-stylus')
const postcss = require('gulp-postcss')
const rollup = require('gulp-rollup')
const rollupYaml = require('rollup-plugin-yaml')
const rollupBabel = require('rollup-plugin-babel')
const pug = require('pug')
const imagemin = require('gulp-imagemin')
const sourcemaps = require('gulp-sourcemaps')
const yaml = require('js-yaml')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
const through = require('through2')
const del = require('del')
const BS = require('browser-sync')
// TODO: Add paths object here
const browserSync = BS.create()
const getData = (path) => {
try {
return yaml.safeLoad(fs.readFileSync(path, 'utf8'))
} catch (e) {
console.error(e) // eslint-disable-line
}
}
const applyTemplate = () => through.obj((file, enc, callback) => {
const layout = file.headers.layout || 'layout/index.pug'
const data = getData('./data.yml')
const locals = Object.assign(data, {
headers: file.headers,
content: file.contents,
})
const html = pug.renderFile(layout, locals)
const basename = path.basename(file.relative, path.extname(file.relative))
const dirname = basename !== 'index' ? basename : ''
file.contents = new Buffer(html)
file.path = path.join(file.base, dirname, 'index.html')
return callback(null, file)
})
gulp.task('pages', () =>
gulp.src('pages/**/*.md')
.pipe(plumber())
.pipe(frontMatter({
property: 'headers',
remove: true,
}))
.pipe(remark().use(html).use(slug).use(breaks))
.pipe(applyTemplate())
.pipe(gulp.dest('dist'))
)
gulp.task('styles', () => {
const processors = [
autoprefixer,
cssnano,
]
return gulp.src('styles/[!_]*.styl')
.pipe(sourcemaps.init())
.pipe(plumber())
.pipe(stylus())
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist/styles'))
})
gulp.task('scripts', () =>
gulp.src('scripts/**/*.js')
.pipe(rollup({
input: 'scripts/index.js',
output: { format: 'cjs' },
plugins: [
rollupYaml(),
rollupBabel()
],
allowRealFiles: true,
}))
// .pipe(babel())
.pipe(gulp.dest('dist/scripts'))
)
gulp.task('images', () =>
gulp.src('images/**/*.*')
.pipe(imagemin())
.pipe(gulp.dest('dist/images'))
)
gulp.task('copy', () =>
gulp.src(['static/**/*', 'CNAME'])
.pipe(gulp.dest('dist'))
)
gulp.task('clean', () => del('dist'))
gulp.task('build', gulp.series(
'clean',
gulp.parallel(
'pages',
'styles',
'scripts',
'images',
'copy'
)
))
gulp.task('watch', () => {
browserSync.init({
server: 'dist',
})
gulp.watch(['layout/**/*.pug', 'pages/**/*.md', '*.yml'], gulp.series('pages'))
gulp.watch('styles/**/*.styl', gulp.series('styles'))
gulp.watch('scripts/**/*.js', gulp.series('scripts'))
gulp.watch('images/**/*.*', gulp.series('images'))
browserSync.watch('dist/**/*.*').on('change', browserSync.reload)
})
gulp.task('default', gulp.series('build', 'watch'))