forked from emotion-js/emotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
116 lines (107 loc) · 3.15 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
const path = require('path')
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin
global.Babel = require('babel-standalone')
exports.onCreateWebpackConfig = ({ stage, actions, plugins, getConfig }) => {
actions.setWebpackConfig({
// xor and props are for react-live and cosmiconfig is for babel-plugin-macros
plugins: [plugins.ignore(/^(xor|props|cosmiconfig)$/)],
resolve: {
alias: {
assert: 'fbjs/lib/emptyFunction',
'source-map': 'fbjs/lib/emptyFunction',
'@babel/types': path.join(__dirname, './src/utils/babel-types'),
'buble/dist/buble.deps': path.join(__dirname, './src/utils/transform')
}
},
node: {
fs: 'empty',
buffer: 'empty',
assert: 'empty'
}
})
const config = getConfig()
actions.replaceWebpackConfig({
...config,
output: {
...config.output,
// this doesn't seem to always merge correctly with `setWebpackConfig` for some reason
// so i'm setting it here
// this is here because it defaults to window and is used for hot reloading and other stuff
// so if this wasn't here, the web worker would break
// since it would try to access window
globalObject: 'this'
},
module: {
...config.module,
rules: config.module.rules.filter(rule => {
// eslint is annoying
return rule.enforce !== 'pre'
})
}
})
if (stage === 'build-javascript') {
actions.setWebpackConfig({
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: 'static'
})
]
})
}
}
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.sourceNodes = async ({ store, cache, actions, createNodeId }) => {
await createRemoteFileNode({
url: `https://raw.githubusercontent.com/emotion-js/awesome-emotion/master/README.md`,
store,
cache,
createNode: actions.createNode,
createNodeId
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const docs1 = require('./docs-yaml')()
const docTemplate = require.resolve(`./src/templates/doc.js`)
docs1.forEach(({ title, items }) => {
items.forEach(itemName => {
createPage({
path: `docs/${itemName}`,
component: docTemplate,
context: {
slug: itemName
}
})
})
})
}
// Add custom url pathname for blog posts.
exports.onCreateNode = async ({ node, actions, getNode, loadNodeContent }) => {
const { createNodeField } = actions
if (
node.internal.type === `MarkdownRemark` &&
typeof node.slug === `undefined`
) {
const fileNode = getNode(node.parent)
createNodeField({
node,
name: `slug`,
value:
fileNode.name === 'README'
? getNameForPackage(fileNode.absolutePath)
: fileNode.name
})
}
}
function getNameForPackage(absolutePath) {
try {
return require(`${path.parse(absolutePath).dir}/package.json`).name
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
const splitAbsolutePath = absolutePath.split(path.sep)
return splitAbsolutePath[splitAbsolutePath.length - 2]
}
throw e
}
}