-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
102 lines (87 loc) · 2.64 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
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
const fs = require(`fs`)
const workboxBuild = require(`workbox-build`)
const path = require(`path`)
const slash = require(`slash`)
const _ = require(`lodash`)
const os = require('os')
const getResourcesFromHTML = require(`./get-resources-from-html`)
exports.createPages = ({ actions }) => {
if (process.env.NODE_ENV === `production`) {
const { createPage } = actions
createPage({
path: `/offline-plugin-app-shell-fallback/`,
component: slash(path.resolve(`${__dirname}/app-shell.js`)),
})
}
}
let s
const readStats = () => {
if (s) {
return s
} else {
s = JSON.parse(
fs.readFileSync(`${process.cwd()}/public/webpack.stats.json`, `utf-8`)
)
return s
}
}
const getAssetsForChunks = (chunks) => {
const files = _.flatten(
chunks.map((chunk) => readStats().assetsByChunkName[chunk])
)
return _.compact(files)
}
exports.onPostBuild = (args, pluginOptions) => {
const program = args.store.getState().program
const rootDir = `public`
// Get exact asset filenames for app and offline app shell chunks
const files = getAssetsForChunks([
`app`,
`webpack-runtime`,
`component---node-modules-gatsby-plugin-offline-app-shell-js`,
])
const criticalFilePaths = _.uniq(
_.concat(
getResourcesFromHTML(`${process.cwd()}/${rootDir}/index.html`),
getResourcesFromHTML(`${process.cwd()}/${rootDir}/404.html`),
getResourcesFromHTML(
`${process.cwd()}/${rootDir}/offline-plugin-app-shell-fallback/index.html`
)
)
)
const globPatterns = files.concat([
`index.html`,
`offline-plugin-app-shell-fallback/index.html`,
...criticalFilePaths,
])
const manifests = [`manifest.json`, `manifest.webmanifest`]
manifests.forEach((file) => {
if (fs.existsSync(`${rootDir}/${file}`)) globPatterns.push(file)
})
const options = {
globDirectory: rootDir,
globPatterns,
modifyUrlPrefix: {
rootDir: ``,
// If `pathPrefix` is configured by user, we should replace
// the default prefix with `pathPrefix`.
'': args.pathPrefix || ``,
},
}
// Set the swPath to the path passed orthe default one
const swPath = pluginOptions.swPath || `src/sw.js`
const swDest = `public/sw.js`
let swSrc = path.join(program.directory, swPath)
// Windows specifics
if (os.platform() == `win32`) {
swSrc = swSrc.split(`\\`).join(`\\\\`)
}
return workboxBuild
.injectManifest({ swDest, swSrc, ...options })
.then(({ count, size, warnings }) => {
if (warnings) warnings.forEach(console.warn)
console.log(
`Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.`
)
})
}