This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
gatsby-node.js
65 lines (58 loc) · 1.67 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
const path = require("path");
const { defaultLocale, supportedLanguages } = require("./buildHelpers/i18n");
const {
createDocsPages,
queryFragment: docsQueryFragment,
} = require("./buildHelpers/createDocsPages");
const getLocale = (filename) => {
const nameSections = filename.split(".");
const length = nameSections.length;
// If there are 2+ sections after splitting on `.` (excluding file extension)
// and the last one is in our list of supported languages, that's our locale
if (length >= 2 && supportedLanguages.includes(nameSections[length - 1])) {
return nameSections[length - 1];
}
return defaultLocale;
};
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === "Mdx" && node.fileAbsolutePath) {
const locale = getLocale(path.parse(node.fileAbsolutePath).base);
createNodeField({
node,
name: "locale",
value: locale,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const result = await graphql(
`
{
${docsQueryFragment}
}
`,
);
if (result.errors) {
console.log(result.errors);
return Promise.reject(result.errors);
}
const docs = result.data.docs.edges;
createDocsPages({ actions, docs });
};
// Enable absolute imports from `src/`
exports.onCreateWebpackConfig = ({ actions, plugins }) => {
actions.setWebpackConfig({
resolve: {
modules: ["node_modules", "src"],
},
plugins: [
plugins.define({
"process.env": {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
AMPLITUDE_KEY: JSON.stringify(process.env.AMPLITUDE_KEY),
},
}),
],
});
};