-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgatsby-node.js
47 lines (46 loc) · 1.32 KB
/
gatsby-node.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
const path = require('path')
const slash = require('slash')
const fs = require('fs-extra')
const globby = require('globby')
const html2amp = require('html2amp')
exports.onPostBuild = (_, pluginOptions) => {
const defaultOptions = {
files: ['**/*.html'],
publicPath: 'public',
gaConfigPath: null,
dist: null,
serviceWorker: null,
optimize: false,
htmlPlugins: [],
cssPlugins: []
}
const { files, publicPath, gaConfigPath, dist, serviceWorker, optimize, htmlPlugins, cssPlugins } = {
...defaultOptions,
...pluginOptions
}
const absolutePaths = files.map(file =>
slash(path.join(process.cwd(), publicPath, file))
)
const htmls = globby.sync(absolutePaths)
const config = {
gaConfigPath,
cwd: slash(path.join(process.cwd(), publicPath)),
serviceWorker,
optimize,
htmlPlugins,
cssPlugins
}
const promises = htmls.map(async html => {
const buffer = fs.readFileSync(html)
const amp = await html2amp(buffer.toString(), config)
if (dist) {
const basePath = slash(path.join(process.cwd(), publicPath))
const ampPath = slash(path.join(process.cwd(), dist))
const newFilePath = html.replace(basePath, ampPath)
fs.outputFileSync(newFilePath, amp)
} else {
fs.writeFileSync(html, amp)
}
})
return Promise.all(promises)
}