-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
page-hot-reloader.js
72 lines (62 loc) · 1.7 KB
/
page-hot-reloader.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
const _ = require(`lodash`)
const { emitter, store } = require(`../redux`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const { boundActionCreators } = require(`../redux/actions`)
const { deletePage, deleteComponentsDependencies } = boundActionCreators
let pagesDirty = false
let graphql
emitter.on(`CREATE_NODE`, action => {
if (action.payload.internal.type !== `SitePage`) {
pagesDirty = true
}
})
emitter.on(`DELETE_NODE`, action => {
if (action.payload.internal.type !== `SitePage`) {
pagesDirty = true
}
})
emitter.on(`API_RUNNING_QUEUE_EMPTY`, () => {
if (pagesDirty) {
runCreatePages()
}
})
const runCreatePages = async () => {
pagesDirty = false
const plugins = store.getState().plugins
// Test which plugins implement createPagesStatefully so we can
// ignore their pages.
const statefulPlugins = plugins
.filter(p => {
try {
const gatsbyNode = require(`${p.resolve}/gatsby-node`)
if (gatsbyNode.createPagesStatefully) {
return true
} else {
return false
}
} catch (e) {
return false
}
})
.map(p => p.id)
const timestamp = new Date().toJSON()
await apiRunnerNode(`createPages`, {
graphql,
traceId: `createPages`,
waitForCascadingActions: true,
})
// Delete pages that weren't updated when running createPages.
Array.from(store.getState().pages.values()).forEach(page => {
if (
!_.includes(statefulPlugins, page.pluginCreatorId) &&
page.updatedAt < timestamp
) {
deleteComponentsDependencies([page.path])
deletePage(page)
}
})
emitter.emit(`CREATE_PAGE_END`)
}
module.exports = graphqlRunner => {
graphql = graphqlRunner
}