-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
51 lines (45 loc) · 1.86 KB
/
index.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
const readdirp = require('readdirp')
const critical = require('critical')
const getHtmlFiles = async (directory, inputs = {}) => {
const files = await readdirp.promise(directory, {
fileFilter: inputs.fileFilter,
directoryFilter: inputs.directoryFilter
})
return files.map((file) => file.fullPath)
}
module.exports = {
onPostBuild: async ({ inputs, constants, utils }) => {
const htmlFiles = await getHtmlFiles(constants.PUBLISH_DIR, inputs)
try {
// Ignore penthouse/puppeteer max listener warnings.
// See https://github.com/pocketjoso/penthouse/issues/250.
// One penthouse call is made per page and per screen resolution.
process.setMaxListeners(inputs.dimensions.length + 1)
// Process each page in sequence to avoid lingering processes and memory
// issues, at the cost of a slower execution.
// `critical` might offer this feature at some point:
// https://github.com/addyosmani/critical/issues/111
for (const filePath of htmlFiles) {
await critical.generate({
base: constants.PUBLISH_DIR,
// Overwrite files by passing the same path for `src` and `target`.
src: filePath,
target: filePath,
inline: true,
extract: inputs.extract,
dimensions: inputs.dimensions,
// Force critical to run penthouse only on a single page at a time to
// avoid timeout issues.
concurrency: 1,
// Bump penthouse’s page load timeout to 2 minutes to avoid crashes
// which could cause lingering processes as it’s possible some pages
// can take a long time to load.
penthouse: { timeout: 120000 }
})
}
console.log('Critical CSS successfully inlined!')
} catch (error) {
return utils.build.failBuild('Failed to inline critical CSS.', { error })
}
}
}