-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-node.js
223 lines (204 loc) · 5.71 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
/* eslint-env node */
// const GracefulFSPlugin = require('graceful-fs-webpack-plugin');
// const autoprefixer = require('autoprefixer');
// const FilterWarningsPlugin = require("webpack-filter-warnings-plugin");
const webpack = require('webpack');
const cryptoModule = require('crypto');
const path = require('path');
exports.onCreateWebpackConfig = ({
stage,
loaders,
plugins,
actions
}) => {
actions.setWebpackConfig({
devtool: 'eval-source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules.(?!@enact|buble|jsonata)|bower_components)/,
use:[loaders.js()]
}
]
},
plugins: [
plugins.define({
// gracefulfs: GracefulFSPlugin,
defineenv: () => new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify((stage.indexOf('develop') >= 0 ? 'development' : 'production'))
}
}),
ignore: () => new webpack.IgnorePlugin(/^(xor|props)$/)
})/* ,
new FilterWarningsPlugin({
exclude:
/mini-css-extract-plugin[^]*Conflicting order. Following module has been added:/
}), */
]
});
};
/*
exports.modifyWebpackConfig = ({config, stage}) => {
config.loader('js', cfg => {
cfg.exclude = /(node_modules.(?!@enact|buble|jsonata)|bower_components)/;
return cfg;
});
config.merge({
devtool: (stage.indexOf('develop') >= 0 ? 'source-map' : false),
postcss: function () {
return [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9' // React doesn't support IE8 anyway
]}
)
];
}
});
config.plugin('ilib', ILibPlugin);
config.plugin('gracefulfs', GracefulFSPlugin);
config.plugin('defineenv', () => new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify((stage.indexOf('develop') >= 0 ? 'development' : 'production'))
}
}));
config.plugin('ignore', () => new webpack.IgnorePlugin(/^(xor|props)$/));
return config;
};
*/
exports.onCreateBabelConfig = ({actions}) => {
actions.setBabelPlugin({
name: '@babel/plugin-transform-react-jsx',
options: {
runtime: 'automatic'
}
});
};
function createSlug ({relativePath}) {
let slug;
const parsedFilePath = path.parse(relativePath);
if (parsedFilePath.name !== 'index' && parsedFilePath.dir !== '') {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`;
} else if (parsedFilePath.dir === '') {
slug = `/${parsedFilePath.name}/`;
} else {
slug = `/${parsedFilePath.dir}/`;
}
return slug;
}
async function onCreateNode ({node, actions, getNode, loadNodeContent}) {
const {createNodeField, createNode, createParentChildLink} = actions;
let slug;
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent);
slug = createSlug(fileNode);
// Add slug as a field on the node.
createNodeField({node, name: 'slug', value: slug});
} else if (node.internal.mediaType === 'application/json') {
const content = await loadNodeContent(node);
const parsedContent = JSON.parse(content);
const packedContent = JSON.stringify(parsedContent);
const contentDigest = cryptoModule
.createHash('md5')
.update(packedContent)
.digest('hex');
const jsonNode = {
id: parsedContent.id ? parsedContent.id : `${node.id} >>> JSON`,
parent: node.id,
children: [],
internal: {
contentDigest,
content: packedContent,
type: 'JsonDoc'
}
};
slug = createSlug(node);
createNode(jsonNode);
createParentChildLink({parent: node, child: jsonNode});
// Add slug as a field on the node.
createNodeField({node: jsonNode, name: 'slug', value: slug});
} else if (node.internal.type === 'JavascriptFrontmatter') {
// For some reason, the parent node is attached and we can get the relative path from there!
slug = createSlug(node.node);
createNodeField({node, name: 'slug', value: slug});
}
}
exports.onCreateNode = onCreateNode;
exports.createPages = ({graphql, actions}) => {
const {createPage} = actions;
// Create a regex that will include siblings and (if applicable) parent's siblings, but not
// the children of the parent's siblings or the children of the current page.
function parentRegexFromSlug (childSlug) {
const parts = childSlug.split('/'),
parentPathParts = parts.slice(0, parts.length - 2);
// Parent will be one level up from current page
return '/' + parentPathParts.join('\\/') + '(\\/[^/]*)?\\/$/';
}
return new Promise((resolve, reject) => {
const markdownPage = path.resolve('src/templates/markdown.js');
const jsonPage = path.resolve('src/templates/json.js');
// Query for all markdown "nodes" and for the slug we previously created.
resolve(
graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
title
}
fields {
slug
}
}
}
},
allJsonDoc {
edges {
node {
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors); // eslint-disable-line no-console
reject(result.errors);
}
// Create markdown pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
path: edge.node.fields.slug, // required
component: markdownPage,
context: {
slug: edge.node.fields.slug,
title: edge.node.frontmatter.title,
parentRegex: parentRegexFromSlug(edge.node.fields.slug)
}
});
});
// Create JSON pages.
result.data.allJsonDoc.edges.forEach(edge => {
createPage({
path: edge.node.fields.slug, // required
component: jsonPage,
context: {
slug: edge.node.fields.slug,
title: edge.node.fields.slug.replace(/\/docs\/modules\/(.*)\//, '$1')
}
});
});
})
);
});
};