-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
179 lines (157 loc) · 5.45 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
const {series, watch, src, dest, parallel, task} = require('gulp');
const pump = require('pump');
const path = require('path');
const inquirer = require('inquirer');
// gulp plugins and utils
const livereload = require('gulp-livereload');
const postcss = require('gulp-postcss');
const zip = require('gulp-zip');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const beeper = require('beeper');
const fs = require('fs');
// postcss plugins
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const easyimport = require('postcss-easy-import');
// fetch für externes cards-Stylesheet
const axios = require('axios');
// penthouse laden
const penthouse = require('penthouse');
const REPO = 'hutt/spectre';
const REPO_READONLY = 'hutt/spectre';
const CHANGELOG_PATH = path.join(process.cwd(), '.', 'changelog.md');
function serve(done) {
livereload.listen();
done();
}
const handleError = (done) => {
return function (err) {
if (err) {
beeper();
}
return done(err);
};
};
function hbs(done) {
pump([
src(['*.hbs', 'partials/**/*.hbs']),
livereload()
], handleError(done));
}
function css(done) {
pump([
src('assets/css/screen.css', {sourcemaps: true}),
postcss([
easyimport,
autoprefixer(),
cssnano()
]),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
pump([
src('assets/css/print.css', {sourcemaps: true}),
postcss([
easyimport,
autoprefixer(),
cssnano()
]),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}
async function generateCriticalCSS(done, category, url) {
try {
// Fetch the external CSS
const externalCssResponse = await axios.get('http://hegel.hutt/public/cards.min.css');
const externalCss = externalCssResponse.data;
// Read the main stylesheet
const mainCss = fs.readFileSync('assets/built/screen.css', 'utf8');
// Combine the stylesheets
const combinedCss = mainCss + '\n' + externalCss;
// Generate critical CSS using the combined stylesheet
const criticalCss = await penthouse({
url: url,
cssString: combinedCss, // Use cssString instead of css file path
width: 390,
height: 1280,
keepLargerMediaQueries: true,
forceInclude: ['/\.youtube\-player[.\w\s]*/g', '/\.gh\-navigation[\.\-\w\s]*/g', '/\#gh\-navigation/g', '/\.gh\-content[\.\-\>\*\+\[\]\w\s]*/g', '/\.kg\-blockquote\-alt/g', '/\.kg\-header\-card/g', '/\.kg\-header\-card\-content/g', '/\.kg\-header\-card\-image/g'],
renderWaitTime: 1000,
blockJSRequests: false,
});
// Process the critical CSS
const regex = /url\((?:'|"|)(\.\.\/)([^)'"]+)(?:'|"|)\)/g;
const cleanedCss = criticalCss.replace(regex, (match, p1, p2) => `url('/assets/${p2}')`);
fs.writeFileSync(`partials/css/${category}.critical.css.hbs`, `<style>\n${cleanedCss}\n</style>`);
} catch (err) {
console.error(err);
done(err);
} finally {
done();
}
}
function critical(done) {
const categories = [
{ name: 'post', url: 'http://hegel.hutt/blog/die-ost-tour-geht-weiter/' },
{ name: 'page', url: 'http://hegel.hutt/' },
{ name: 'tag', url: 'http://hegel.hutt/tag/gewerkschaft/' },
{ name: 'index', url: 'http://hegel.hutt/blog/' }
];
categories.forEach(category => {
generateCriticalCSS(done, category.name, category.url);
});
}
function js(done) {
pump([
src([
'assets/js/lib/*.js',
'assets/js/*.js',
'!assets/js/lib/opentype.js', // exclude opentype.js
'!assets/js/lib/text-to-svg.js', // exclude text-to-svg.js
'!assets/js/dielinke-logo-generator.js' // exclude dielinke-logo-generator.js
], {sourcemaps: true}),
concat('source.js'),
uglify(),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
// handle dielinke-logo-generator.js seperately
pump([
src([
'assets/js/lib/opentype.js',
'assets/js/lib/text-to-svg.js',
'assets/js/dielinke-logo-generator.js'
], {sourcemaps: true}),
concat('dielinke-logo-generator.js'),
uglify(),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}
function zipper(done) {
const filename = require('./package.json').name + '.zip';
pump([
src([
'**',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!yarn-error.log',
'!yarn.lock',
'!gulpfile.js'
]),
zip(filename),
dest('dist/')
], handleError(done));
}
const cssWatcher = () => watch(['assets/css/screen.css', 'assets/css/print.css'], css);
//const criticalCssWatcher = () => watch('assets/css/screen.css', critical);
const jsWatcher = () => watch('assets/js/**', js);
const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], hbs);
const watcher = parallel(cssWatcher, jsWatcher, hbsWatcher);
const build = series(css, js, critical);
exports.build = build;
exports.zip = series(build, zipper);
exports.critical = critical;
exports.default = series(build, serve, watcher);