-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGulpfile.js
171 lines (152 loc) · 4.98 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
const fs = require('fs');
const path = require('path');
const url = require('url');
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const yaml = require('js-yaml');
const markdownIt = require('markdown-it')();
const marked = require('marked');
const cssUrlParser = require('css-url-parser');
const ftp = require('vinyl-ftp');
const imageminJpegoptim = require('imagemin-jpegoptim');
const critical = require('critical').stream;
const _ = require('lodash');
const $ = require('gulp-load-plugins')();
const dist = 'build';
require('dotenv').config({silent: true});
gulp.task('styles', function() {
return gulp.src('styles/main.sass')
.pipe($.sass())
.pipe(gulp.dest(path.join('.tmp', 'styles')));
});
gulp.task('templates', function() {
return gulp.src('index.html')
.pipe($.data(function() {
const files = fs.readdirSync('data');
const data = files.map(function(file) {
let filedata = yaml.load(fs.readFileSync(path.join('data', file)).toString());
filedata.key = path.basename(file, '.yml').replace(/^[0-9]+_?/, '');
return filedata;
});
return {
data: data
};
}))
.pipe($.swig({
defaults: { cache: false },
setup: function(swig) {
const md = function(input) {
return marked(input, {
sanitize: true
});
};
md.safe = true;
swig.setFilter('md', md);
const mdi = function(input) {
return markdownIt.renderInline(input);
};
mdi.safe = true;
swig.setFilter('mdi', mdi);
}
}))
.pipe(gulp.dest('.tmp'));
});
gulp.task('html', ['styles', 'templates'], function() {
return gulp.src('.tmp/index.html')
.pipe($.usemin({
path: './',
css: [
$.cssimport({ includePaths: ['styles'] }),
$.cleanCss(),
$.rev()
],
js: [
$.babel({ presets: ['es2015'] }),
$.minify({
ext: {
min:'.js'
},
noSource: true
}),
$.rev()
]
}))
.pipe($.if('*.html', $.htmlmin({
collapseWhitespace: true,
decodeEntities: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true
})))
.pipe(gulp.dest(dist));
});
const dlFonts = function(dest) {
const urls = cssUrlParser(fs.readFileSync('styles/fonts.css').toString())
.filter(pathStr => !fs.existsSync(path.join(dest, pathStr)))
.map(urlStr => {
const tmpUrl = new URL(url.resolve("http://www1.wdr.de/resources/fonts/", urlStr));
return url.format(tmpUrl, { fragment: false, search: false });
});
if(urls.length == 0) {
return gulp.src([]);
}
return $.download(urls)
.pipe(gulp.dest(dest));
};
gulp.task('fonts', function() {
return dlFonts(path.join(dist, 'fonts'));
});
gulp.task('fonts:develop', function() {
return dlFonts('fonts');
});
gulp.task('serve', ['fonts:develop', 'styles', 'templates'], function() {
browserSync.init({
server: {
baseDir: ['.tmp', "./"]
},
open: false
});
gulp.watch(['*.js'], browserSync.reload);
gulp.watch(['*.html', 'partials/*.html', 'data/*.yml'], ['templates', browserSync.reload]);
gulp.watch('styles/*.sass', ['styles', browserSync.reload]);
});
gulp.task('images', function() {
return gulp.src('images/**/*')
.pipe($.imagemin([
imageminJpegoptim({ max: 70 }),
$.imagemin.optipng({optimizationLevel: 5}),
$.imagemin.svgo({plugins: [{removeViewBox: true}]})
], {
verbose: true
}))
.pipe(gulp.dest(path.join(dist, 'images')));
});
gulp.task('copy:dist', function() {
return gulp.src([
'bower_components/jquery/dist/jquery.min.js',
'bower_components/d3/d3.min.js'
], { base: './' })
.pipe(gulp.dest(dist));
});
gulp.task('assets', ['images', 'fonts', 'html', 'copy:dist']);
gulp.task('critical-css', ['assets'], function() {
return gulp.src(path.join(dist, 'index.html'))
.pipe(critical({
base: dist,
inline: true,
minify: true
}))
.pipe(gulp.dest(dist));
});
gulp.task('build', ['assets', 'critical-css']);
gulp.task('upload', ['build'], function() {
const conn = ftp.create({
host: process.env.FTP_HOST,
user: process.env.FTP_USER,
pass: process.env.FTP_PASS,
log: $.util.log
});
return gulp.src([path.join(dist, '**'), '.htaccess']/*, { buffer: false }*/)
.pipe(conn.dest('/'));
});
gulp.task('default', ['build']);