This repository was archived by the owner on Mar 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgatsby-node.js
More file actions
144 lines (134 loc) · 3.94 KB
/
Copy pathgatsby-node.js
File metadata and controls
144 lines (134 loc) · 3.94 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const path = require(`path`)
const fs = require(`fs-extra`)
const process = require(`process`)
const sharp = require(`sharp`)
const { slash } = require(`gatsby-core-utils`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const Papa = require("papaparse")
const { IgnorePlugin } = require("webpack")
const AVATAR_SIZE = 64
const LAZY_SIZE = 96
const PREVIEW_WIDTH = 480
const PREVIEW_HEIGHT = 360
const DEVICE_LIST_WIDTH = 240
const DEVICE_LIST_HEIGHT = 180
const CATALOG_HEIGHT = 600
const CATALOG_WIDTH = 800
const FULL_HEIGHT = 768
const FULL_WIDTH = 1024
async function createVersions() {
await fs.outputFile(
`./public/version.json`,
JSON.stringify({
sha: process.env.GATSBY_GITHUB_SHA,
})
)
}
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
exports.createPages = async ({ graphql, actions, reporter }) => {
await createVersions()
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
//console.log(`${node.internal.type} -> ${node.value}`)
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
if (node.frontmatter && !node.frontmatter.title) {
const heading = /#\s*([^\n]+)/.exec(node.rawBody)
if (heading) node.frontmatter.title = heading[1].trim()
}
}
}
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
const { setWebpackConfig, replaceWebpackConfig } = actions
//console.log({ stage })
const plugins = [
new IgnorePlugin({
resourceRegExp: /^canvas$/,
}),
]
const fallback = {
crypto: false,
util: require.resolve("util/"),
assert: require.resolve("assert/"),
fs: false,
net: false,
webusb: false,
}
if (stage.startsWith("develop")) {
setWebpackConfig({
resolve: {
fallback,
},
plugins,
})
}
if (stage === "build-javascript" || stage === "build-html") {
console.log(`enabling ignore filters`)
setWebpackConfig({
node: {
fs: "empty",
},
resolve: {
fallback,
},
plugins,
})
}
// enable verbose logging
const config = getConfig()
config.stats = "verbose"
config.performance.hints = "warning"
replaceWebpackConfig(config)
// final webpack
//console.log({ webpack: getConfig() })
}
// generate a full list of pages for compliance
exports.onPostBuild = async ({ graphql }) => {
console.log(`compliance step`)
const { data } = await graphql(`
{
pages: allSitePage {
nodes {
path
}
}
}
`)
await fs.writeFile(
path.resolve(__dirname, ".cache/all-pages.csv"),
data.pages.nodes
.map(
node =>
`${
"https://microsoft.github.io/data-science-editor" + node.path
}, ${node.path.slice(1)}`
)
.join("\n")
)
await fs.writeFile(
path.resolve(__dirname, ".cache/top-pages.csv"),
data.pages.nodes
.filter(
node =>
node.path.slice(1).replace(/\/$/, "").split(/\//g).length <
2 &&
node.path.indexOf("offline-plugin-app-shell-fallback") < 0
)
.map(
node =>
`${
"https://microsoft.github.io/data-science-editor" + node.path
}, ${node.path.slice(1)}`
)
.join("\n")
)
}