-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby-node.js
147 lines (134 loc) · 4.08 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
//const webpack = require("webpack");
const _ = require("lodash");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const path = require("path");
const Promise = require("bluebird");
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode });
const fileNode = getNode(node.parent);
const source = fileNode.sourceInstanceName;
const separtorIndex = ~slug.indexOf("--") ? slug.indexOf("--") : 0;
const shortSlugStart = separtorIndex ? separtorIndex + 2 : 0;
if (source !== "parts") {
createNodeField({
node,
name: `slug`,
value: `${separtorIndex ? "/" : ""}${slug.substring(shortSlugStart)}`
});
}
createNodeField({
node,
name: `prefix`,
value: separtorIndex ? slug.substring(1, separtorIndex) : ""
});
createNodeField({
node,
name: `source`,
value: source
});
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const sprintTemplate = path.resolve("./src/templates/SprintTemplate.js");
// Do not create draft post files in production.
let activeEnv = process.env.ACTIVE_ENV || process.env.NODE_ENV || "development"
console.log(`Using environment config: '${activeEnv}'`)
let filters = `filter: { fields: { slug: { ne: null } } }`;
if (activeEnv == "production") filters = `filter: { fields: { slug: { ne: null } , prefix: { ne: null } } }`
resolve(
graphql(
`
{
allMarkdownRemark(
` + filters + `
sort: { fields: [fields___prefix], order: DESC }
limit: 1000
) {
edges {
node {
id
fields {
slug
prefix
source
}
frontmatter {
title
category
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
const items = result.data.allMarkdownRemark.edges;
// Create category list
const categorySet = new Set();
items.forEach(edge => {
const {
node: {
frontmatter: { category }
}
} = edge;
if (category && category !== null) {
categorySet.add(category);
}
});
// Create sprints
const sprints = items
.filter(item => item.node.fields.source === "sprints")
.sort((a, b) => {
if (a.node.fields.slug > b.node.fields.slug) {
return 1;
}
if (a.node.fields.slug < b.node.fields.slug) {
return -1;
}
return 0;
});
sprints.forEach(({ node }, index) => {
const slug = node.fields.slug;
const next = index === 0 ? undefined : sprints[index - 1].node;
const prev = index === sprints.length - 1 ? undefined : sprints[index + 1].node;
const source = node.fields.source;
createPage({
path: slug,
component: sprintTemplate,
context: {
slug,
prev,
next,
source
}
});
});
})
);
});
};
exports.onCreateWebpackConfig = ({ stage, actions }, options) => {
switch (stage) {
case `build-javascript`:
actions.setWebpackConfig({
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: "./report/treemap.html",
openAnalyzer: true,
logLevel: "error",
defaultSizes: "gzip"
})
]
});
}
};