forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
214 lines (197 loc) · 5.74 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
require('dotenv').config();
const { createFilePath } = require('gatsby-source-filesystem');
const { dasherize } = require('./utils');
const { blockNameify } = require('./utils/blockNameify');
const {
createChallengePages,
createBlockIntroPages,
createSuperBlockIntroPages,
createGuideArticlePages
} = require('./utils/gatsby');
const createByIdentityMap = {
guideMarkdown: createGuideArticlePages,
blockIntroMarkdown: createBlockIntroPages,
superBlockIntroMarkdown: createSuperBlockIntroPages
};
exports.onCreateNode = function onCreateNode({ node, actions, getNode }) {
const { createNodeField } = actions;
if (node.internal.type === 'ChallengeNode') {
const { tests = [], block, title, superBlock } = node;
const slug = `/learn/${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 slug = createFilePath({ node, getNode });
if (!slug.includes('LICENSE')) {
const {
frontmatter: { component = '' }
} = node;
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'component', value: component });
}
}
};
exports.createPages = function createPages({ graphql, actions }) {
const { createPage } = actions;
return new Promise((resolve, reject) => {
// Query for all markdown 'nodes' and for the slug we previously created.
resolve(
graphql(`
{
allChallengeNode(
sort: { fields: [superOrder, order, challengeOrder] }
) {
edges {
node {
block
challengeType
fields {
slug
}
id
order
required {
link
src
}
challengeOrder
superBlock
superOrder
template
}
}
}
allMarkdownRemark {
edges {
node {
fields {
slug
nodeIdentity
component
}
frontmatter {
block
superBlock
title
}
htmlAst
id
excerpt
}
}
}
}
`).then(result => {
if (result.errors) {
console.log(result.errors);
return reject(result.errors);
}
// Create challenge pages.
result.data.allChallengeNode.edges.forEach(
createChallengePages(createPage)
);
// Create intro pages
result.data.allMarkdownRemark.edges.forEach(edge => {
const {
node: { frontmatter, fields }
} = edge;
if (!fields) {
return null;
}
const { slug, nodeIdentity } = fields;
if (slug.includes('LICENCE')) {
return null;
}
try {
const pageBuilder = createByIdentityMap[nodeIdentity](createPage);
return pageBuilder(edge);
} catch (e) {
console.log(`
ident: ${nodeIdentity} does not belong to a function
${frontmatter ? JSON.stringify(edge.node) : 'no frontmatter'}
`);
}
return null;
});
return null;
})
);
});
};
const RmServiceWorkerPlugin = require('webpack-remove-serviceworker-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
exports.onCreateWebpackConfig = ({ stage, rules, plugins, actions }) => {
actions.setWebpackConfig({
module: {
rules: [
rules.js({
/* 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*/
})
]
},
node: {
fs: 'empty'
},
plugins: [
plugins.define({
HOME_PATH: JSON.stringify(
process.env.HOME_PATH || 'http://localhost:3000'
),
STRIPE_PUBLIC_KEY: JSON.stringify(process.env.STRIPE_PUBLIC_KEY || ''),
ROLLBAR_CLIENT_ID: JSON.stringify(process.env.ROLLBAR_CLIENT_ID || ''),
ENVIRONMENT: JSON.stringify(process.env.NODE_ENV || 'development'),
PAYPAL_SUPPORTERS: JSON.stringify(process.env.PAYPAL_SUPPORTERS || 404)
}),
new RmServiceWorkerPlugin()
]
});
if (stage !== 'build-html') {
actions.setWebpackConfig({
plugins: [new MonacoWebpackPlugin()]
});
}
if (stage === 'build-html') {
actions.setWebpackConfig({
plugins: [
plugins.normalModuleReplacement(
/react-monaco-editor/,
require.resolve('./src/__mocks__/monacoEditorMock.js')
)
]
});
}
};
exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: '@babel/plugin-proposal-function-bind'
});
actions.setBabelPlugin({
name: '@babel/plugin-proposal-export-default-from'
});
actions.setBabelPlugin({
name: 'babel-plugin-transform-imports',
options: {
'@freecodecamp/react-bootstrap': {
transform: '@freecodecamp/react-bootstrap/lib/${member}',
preventFullImport: true
},
lodash: {
transform: 'lodash/${member}',
preventFullImport: true
}
}
});
};