forked from freeCodeCamp/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
173 lines (158 loc) · 4.81 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
require('dotenv').config();
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { dasherize } = require('./utils');
const { blockNameify } = require('./utils/blockNameify');
const { createChallengePages, createIntroPages } = require('./utils/gatsby');
exports.onCreateNode = function onCreateNode({ node, boundActionCreators }) {
const { createNodeField } = boundActionCreators;
if (node.internal.type === 'ChallengeNode') {
const { tests = [], block, title, superBlock } = node;
const slug = `/${dasherize(superBlock)}/${dasherize(block)}/${dasherize(
title
)}`;
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'blockName', value: blockNameify(block) });
createNodeField({ node, name: 'tests', value: tests });
}
if (node.internal.type === 'MarkdownRemark') {
const { frontmatter: { block, superBlock } } = node;
let slug = `/${dasherize(superBlock)}`;
// Without this condition the slug for superblocks ends up as something like
// "/apis-and-microservice/undefined" and what we want instead is just
// "/apis-and-microservice"
if (typeof block !== 'undefined') {
slug = slug + `/${dasherize(block)}`;
}
createNodeField({ node, name: 'slug', value: slug });
}
};
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators;
return new Promise((resolve, reject) => {
// Query for all markdown 'nodes' and for the slug we previously created.
resolve(
graphql(`
{
allChallengeNode(sort: { fields: [superOrder, order, suborder] }) {
edges {
node {
block
challengeType
fields {
slug
}
id
order
required {
link
raw
src
}
suborder
superBlock
superOrder
template
}
}
}
allMarkdownRemark {
edges {
node {
fields {
slug
}
frontmatter {
block
superBlock
title
}
html
}
}
}
}
`).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
// Create challenge pages.
result.data.allChallengeNode.edges.forEach(
createChallengePages(createPage)
);
// Create intro pages
result.data.allMarkdownRemark.edges.forEach(
createIntroPages(createPage)
);
return;
})
);
});
};
const webpack = require('webpack');
const RmServiceWorkerPlugin = require('webpack-remove-serviceworker-plugin');
const generateBabelConfig = require('gatsby/dist/utils/babel-config');
exports.modifyWebpackConfig = ({ config, stage }) => {
const program = {
directory: __dirname,
browserslist: ['> 1%', 'last 2 versions', 'IE >= 9']
};
return generateBabelConfig(program, stage).then(babelConfig => {
config.removeLoader('js').loader('js', {
test: /\.jsx?$/,
/* eslint-disable max-len */
exclude: modulePath => {
return (
/node_modules/.test(modulePath) &&
!(/(ansi-styles|chalk|strict-uri-encode|react-freecodecamp-search)/).test(
modulePath
)
);
},
/* eslint-enable max-len*/
loader: 'babel',
query: babelConfig
});
config.plugin('CopyWebpackPlugin', CopyWebpackPlugin, [
[
{
from: path.resolve(__dirname, './node_modules/monaco-editor/min/vs'),
to: 'vs'
}
]
]);
config.plugin('DefinePlugin', webpack.DefinePlugin, [
{
HOME_PATH: JSON.stringify(
process.env.HOME_PATH || 'http://localhost:3000'
),
STRIPE_PUBLIC_KEY: JSON.stringify(process.env.STRIPE_PULIC_KEY || '')
}
]);
config.plugin('RemoveServiceWorkerPlugin', RmServiceWorkerPlugin, [
{ filename: 'sw.js' }
]);
});
};
/* eslint-disable prefer-object-spread/prefer-object-spread */
exports.modifyBabelrc = ({ babelrc }) =>
Object.assign({}, babelrc, {
plugins: babelrc.plugins.concat([
[
'transform-es2015-arrow-functions',
'transform-imports',
'transform-function-bind',
{
'react-bootstrap': {
transform: 'react-bootstrap/lib/${member}',
preventFullImport: true
},
lodash: {
transform: 'lodash/${member}',
preventFullImport: true
}
}
]
])
});