This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.js
executable file
·59 lines (50 loc) · 1.85 KB
/
generate.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
#!/usr/bin/env node
const critical = require('critical');
const { getConfigFile, getClosestHeight } = require('./helpers');
const criticalcssConfig = getConfigFile();
// Disables warnings
process.setMaxListeners(0);
// No configs so stop
if (!criticalcssConfig) {
return;
}
const domain = process.env.DOMAIN || criticalcssConfig?.domain;
const waitBeforeRender = process.env.RENDER_WAIT_TIME || criticalcssConfig?.renderWaitTime || 2500;
const penthouseTimeout = process.env.PENTHOUSE_TIMEOUT || criticalcssConfig?.penthouseTimeout || 30000;
const forceIncludeList = criticalcssConfig?.forceInclude || [];
const forceExcludeList = criticalcssConfig?.forceExclude || [];
// The pages you want the critical css from (multiple pages and themes possible)
const pages = criticalcssConfig?.pages || [];
let dimensions = criticalcssConfig?.dimensions || [];
// The dimensions you want critical css from.
dimensions = dimensions?.map((dimension) => ({
width: dimension?.width,
height: dimension?.height ?? getClosestHeight(dimension?.width)?.height,
}));
console.log('Starting to create critical css bundles...');
function processPage() {
page = pages.shift();
if (page) {
console.log(' > ' + domain + page.src);
critical.generate({
inline: false,
src: domain + page.src,
css: page.css,
target: {
css: page.target,
},
dimensions: dimensions,
penthouse: {
timeout: penthouseTimeout,
renderWaitTime: waitBeforeRender,
forceInclude: forceIncludeList,
forceExclude: forceExcludeList
}
}).then(result => {
console.log(' ... has been generated');
processPage();
});
}
}
processPage();
console.log('Finished generating critical css bundles!');