forked from piers-sinclair/SSW.Rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
180 lines (168 loc) · 4.7 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const siteConfig = require('./site-config');
const { createFilePath } = require('gatsby-source-filesystem');
const appInsights = require('applicationinsights');
const makePluginData = require('./src/helpers/plugin-data');
const createRewriteMap = require('./src/helpers/createRewriteMap');
const WebpackAssetsManifest = require('webpack-assets-manifest');
const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
const path = require('path');
const Map = require('core-js/features/map');
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY) {
// Log build time stats to appInsights
appInsights
.setup()
.setAutoCollectConsole(true, true) // Enable logging of console.xxx
.start();
} else {
// eslint-disable-next-line no-console
console.warn(
'Missing APPINSIGHTS_INSTRUMENTATIONKEY, this build will not be logged to Application Insights'
);
}
let assetsManifest = {};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode, basePath: '' });
createNodeField({
node,
name: 'slug',
value: slug,
});
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
archivedreason: String
authors: [Author]
related: [String]
redirects: [String]
guid: String
}
type Author {
title: String
url: String
img: String
}
`;
createTypes(typeDefs);
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new WebpackAssetsManifest({
assets: assetsManifest, // mutates object with entries
merge: true,
}),
],
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
plugins: [
new DirectoryNamedWebpackPlugin({
exclude: /node_modules/,
}),
],
},
});
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
categories: allMarkdownRemark(
filter: { frontmatter: { type: { eq: "category" } } }
) {
nodes {
fields {
slug
}
frontmatter {
index
redirects
}
parent {
... on File {
name
relativeDirectory
}
}
}
}
rules: allMarkdownRemark(
filter: {
frontmatter: { type: { nin: ["category", "top-category", "main"] } }
}
) {
nodes {
fields {
slug
}
frontmatter {
uri
archivedreason
related
redirects
}
}
}
}
`);
const categoryTemplate = require.resolve('./src/templates/category.js');
const ruleTemplate = require.resolve('./src/templates/rule.js');
result.data.categories.nodes.forEach((node) => {
createPage({
path: node.parent.name,
component: categoryTemplate,
context: {
slug: node.fields.slug,
index: node.frontmatter.index,
redirects: node.frontmatter.redirects,
},
});
});
result.data.rules.nodes.forEach((node) => {
createPage({
path: node.frontmatter.uri,
component: ruleTemplate,
context: {
slug: node.fields.slug,
related: node.frontmatter.related ? node.frontmatter.related : [''],
uri: node.frontmatter.uri,
redirects: node.frontmatter.redirects,
file: `rules/${node.frontmatter.uri}/rule.md`,
},
});
});
const profilePage = require.resolve('./src/pages/profile.js');
createPage({
path: `${siteConfig.pathPrefix}/people/`,
matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`,
component: profilePage,
});
};
exports.onPostBuild = async ({ store, pathPrefix }) => {
const { pages } = store.getState();
const pluginData = makePluginData(store, assetsManifest, pathPrefix);
const rewrites = Array.from(pages.values())
.filter((page) => page.context.redirects)
.reduce((acc, page) => {
acc = acc.concat(
page.context.redirects.map((redirect) => {
return {
fromPath: pathPrefix + '/' + redirect,
toPath: pathPrefix + page.path,
};
})
);
return acc;
}, []);
const allRewritesUnique = [
...new Map(rewrites.map((item) => [item.fromPath, item])).values(),
];
await createRewriteMap.writeRewriteMapsFile(pluginData, allRewritesUnique);
};