-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
103 lines (96 loc) · 2.54 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
const path = require("path");
/** @type {import("gatsby").GatsbyNode["onCreateNode"]} */
exports.onCreateNode = async ({
actions: { createNodeField },
node,
getNode,
}) => {
if (node.internal.type === "Mdx") {
if (!node.parent) {
return;
}
const post =
/** @type {import("gatsby").Node & { frontmatter: { slug?: string }, slug: string }} */ (
node
);
let slug = post.frontmatter.slug;
if (!slug) {
const fileNode = getNode(node.parent);
if (!fileNode) {
return;
}
if (fileNode.name === "index") {
slug = /** @type {string} */ (fileNode.relativeDirectory);
} else {
slug = /** @type {string} */ (fileNode.name);
}
}
const path = `/blog/${slug}`;
createNodeField({ node, name: "urlPath", value: path });
}
};
// Implement the Gatsby API “createPages”. This is called once the
// data layer is bootstrapped to let plugins create pages from data.
/** @type {import("gatsby").GatsbyNode["createPages"]} */
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Query for markdown nodes to use in creating pages.
const result = await graphql(
`
{
allMdx {
nodes {
id
fields {
urlPath
}
}
}
}
`
);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create pages for each markdown file.
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`);
result.data.allMdx.nodes.forEach((post) => {
createPage({
path: post.fields.urlPath,
component: blogPostTemplate,
// In your blog post template's graphql query, you can use pagePath
// as a GraphQL variable to query for data from the markdown file.
context: {
id: post.id,
},
});
});
};
/** @type {import("gatsby").GatsbyNode["onCreateWebpackConfig"]} */
exports.onCreateWebpackConfig = ({ getConfig, stage, loaders, actions }) => {
const config = getConfig();
const newConfig = {
...config,
module: {
...config.module,
rules: [
{
resourceQuery: "?asset",
type: "asset/resource",
},
...config.module.rules,
],
},
};
if (stage === "build-html" || stage === "develop-html") {
newConfig.externals = [
{
canvas: "commonjs canvas",
},
...config.externals,
];
}
actions.replaceWebpackConfig(newConfig);
};