-
Notifications
You must be signed in to change notification settings - Fork 15
/
gatsby-node.js
226 lines (208 loc) · 5.39 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const isDevMode = process.env.NODE_ENV !== 'production';
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const { toTitleCase } = require('./src/utils/toTitleCase.js');
const getSlugParents = (slug) => {
const slugParentString = slug.substring(1, slug.length);
return slugParentString.split('/');
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (
node.internal.type === 'SitePage' &&
node.context &&
node.context.frontmatter &&
node.context.frontmatter.scriptTags
) {
createNodeField({
name: 'scriptTags',
value: node.context.frontmatter.scriptTags,
node,
});
}
if (node.internal.type === 'SitePage' && node.context && node.context.id) {
createNodeField({
name: 'Mdx___NODE',
value: node.context.id,
node,
});
}
if (node.internal.type === 'Mdx') {
const slug = createFilePath({
node,
getNode,
basePath: 'pages',
trailingSlash: false,
});
const slugParentsArr = getSlugParents(slug);
createNodeField({
node,
name: 'slug',
value: slug,
});
createNodeField({
node,
name: 'navLevel1',
value: toTitleCase(slugParentsArr[0]),
});
createNodeField({
node,
name: 'navLevel2',
value: toTitleCase(slugParentsArr.length > 1 ? slugParentsArr[1] : ''),
});
createNodeField({
node,
name: 'navLevel3',
value: toTitleCase(slugParentsArr.length > 2 ? slugParentsArr[2] : ''),
});
createNodeField({
node,
name: 'lastAuthorTime',
value: require('child_process')
.execSync(
`git log --pretty=format:%aI -- ${node.fileAbsolutePath} | head -1 | tr -d '\n'`
)
.toString(),
});
createNodeField({
node,
name: 'lastAuthor',
value: require('child_process')
.execSync(
`git log --pretty=format:%aN -- ${node.fileAbsolutePath} | head -1 | tr -d '\n'`
)
.toString(),
});
}
};
// exports.createPages = async ({ graphql, actions }) => {
// const { createPage } = actions;
// const { data, errors } = await graphql(`
// {
// allMdx {
// edges {
// node {
// id
// fields {
// slug
// }
// }
// }
// }
// }
// `);
// if (errors) throw errors;
// const defaultLayout = path.resolve(`./src/components/layout.js`);
// data.allMdx.edges.forEach(({ node }) => {
// createPage({
// path: node.fields.slug,
// component: defaultLayout,
// context: { id: node.id },
// context: { mdxId: node.id },
// });
// });
// };
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
if (stage === 'build-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /rocketbelt*.js/,
use: loaders.null(),
},
],
},
});
}
actions.setWebpackConfig({
plugins: [
new StylelintPlugin({
lintDirtyModulesOnly: true,
failOnWarning: false,
failOnError: false,
files: ['**/*.s?(a|c)ss', '**/*.jsx?', '!**/vendor/**/*'],
}),
],
});
actions.setWebpackConfig({
plugins: [
new WriteFilePlugin({
// Make sure HMR chunks aren't written to disk.
// See https://github.com/gaearon/react-hot-loader/issues/456#issuecomment-273216602
test: /^(?!.*(commons\.js|hot|rocketbelt.icons)).*/,
}),
new CopyPlugin(
[
{
from: `${__dirname}/src/rocketbelt/**/rocketbelt*.js`,
to: `${__dirname}/public/scripts/`,
flatten: true,
force: true,
},
{
from: `${__dirname}/src/rocketbelt/**/rocketbelt.icons.svg`,
to: `${__dirname}/public/icons/`,
flatten: true,
force: true,
},
{
from: `${__dirname}/src/images/404/404*`,
to: `${__dirname}/public/images/404`,
flatten: true,
force: true,
},
]
// { logLevel: 'debug' }
),
],
});
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.(scss|sass)$/i,
use: [
isDevMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
},
},
{
loader: 'postcss-loader',
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevMode,
},
},
],
},
],
},
plugins: [
plugins.define({
__DEVELOPMENT__: stage === 'develop' || stage === 'develop-html',
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'css/[name].css',
hmr: process.env.NODE_ENV === 'development',
}),
],
});
};