-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
170 lines (155 loc) · 3.92 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
*/
/**
* @type {import('gatsby').GatsbyNode['createPages']}
*/
const { createFilePath } = require(`gatsby-source-filesystem`)
const path = require("path")
const contentTemplate = {
blog: path.resolve(`./src/templates/blog-template.tsx`),
portafolio: path.resolve(`./src/templates/portafolio-template.tsx`),
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const contentType = getNode(node.parent).sourceInstanceName
const slug = createFilePath({ node, getNode, basePath: contentType })
createNodeField({
node,
name: `contentType`,
value: contentType,
})
createNodeField({
node,
name: `slug`,
value: `/${contentType}${slug}`,
})
}
}
const createPostTypePages = (result, actions, reporter, context) => {
const { createPage } = actions
if (result.errors) {
reporter.panicOnBuild(`🚨 ERROR: Creating pages for '${context}'`)
console.log(result.errors);
}
const posts = result.data.allMdx.edges
posts.forEach(({ node, previous, next }) => {
createPage({
path: node.fields.slug,
component: `${contentTemplate[node.fields.contentType]}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
slug: node.fields.slug,
id: node.id,
previous: previous ? previous.id : null,
next: next ? next.id : null,
},
})
})
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const resultBlog = await graphql(`
query BlogPostsQuery {
allMdx(
filter: { fields: { contentType: { eq: "blog" } } }
sort: { frontmatter: { date: DESC } }
) {
edges {
node {
id
fields {
slug
contentType
}
internal {
contentFilePath
}
}
previous {
id
}
next {
id
}
}
}
}
`)
createPostTypePages(resultBlog, actions, reporter, "Blog")
const resultPortafolio = await graphql(`
query PortafolioPostsQuery {
allMdx(
filter: { fields: { contentType: { eq: "portafolio" } } }
sort: { frontmatter: { title: ASC } }
) {
edges {
node {
id
fields {
slug
contentType
}
internal {
contentFilePath
}
}
previous {
id
}
next {
id
}
}
}
}
`)
createPostTypePages(resultPortafolio, actions, reporter, "Portafolio")
}
exports.onCreateWebpackConfig = ({
stage, actions, getConfig, rules
}, { rule: ruleProps = {} }) => {
const { include, exclude } = ruleProps
if([
'develop',
'develop-html',
'build-html',
'build-javascript'
].includes(stage)) {
// Add the svg-react-loader rule
actions.setWebpackConfig({
module: {
rules: [
//...
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
],
}
})
const cfg = getConfig()
const imgsRule = rules.images()
const newUrlLoaderRule = (include || exclude) ? {
...imgsRule,
include: exclude,
exclude: include
} : {
...imgsRule,
test: new RegExp(imgsRule.test.toString().replace('svg|', '').slice(1, -1))
}
cfg.module.rules = [
// Remove the base url-loader images rule entirely
...cfg.module.rules.filter(rule => {
if(rule.test) {
return rule.test.toString() !== imgsRule.test.toString()
}
return true
}),
// Put it back without SVG loading
newUrlLoaderRule
]
actions.replaceWebpackConfig(cfg)
}
}