This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
/
gatsby-node.js
117 lines (110 loc) · 3.57 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
const webpack = require("webpack");
//const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const _ = require("lodash");
const Promise = require("bluebird");
const path = require("path");
const { createFilePath } = require(`gatsby-source-filesystem`);
const { store } = require(`./node_modules/gatsby/dist/redux`);
exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
const { createNodeField } = boundActionCreators;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
const separtorIndex = ~slug.indexOf("--") ? slug.indexOf("--") : 0;
const shortSlugStart = separtorIndex ? separtorIndex + 2 : 0;
createNodeField({
node,
name: `slug`,
value: `${separtorIndex ? "/" : ""}${slug.substring(shortSlugStart)}`
});
createNodeField({
node,
name: `prefix`,
value: separtorIndex ? slug.substring(1, separtorIndex) : ""
});
}
};
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators;
return new Promise((resolve, reject) => {
const postTemplate = path.resolve("./src/templates/PostTemplate.js");
const pageTemplate = path.resolve("./src/templates/PageTemplate.js");
resolve(
graphql(
`
{
allMarkdownRemark(filter: { id: { regex: "//posts|pages//" } }, limit: 1000) {
edges {
node {
id
fields {
slug
prefix
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
// Create posts and pages.
_.each(result.data.allMarkdownRemark.edges, edge => {
const slug = edge.node.fields.slug;
const isPost = /posts/.test(edge.node.id);
createPage({
path: slug,
component: isPost ? postTemplate : pageTemplate,
context: {
slug: slug
}
});
});
})
);
});
};
exports.modifyWebpackConfig = ({ config, stage }) => {
switch (stage) {
case "build-javascript":
{
let components = store.getState().pages.map(page => page.componentChunkName);
components = _.uniq(components);
config.plugin("CommonsChunkPlugin", webpack.optimize.CommonsChunkPlugin, [
{
name: `commons`,
chunks: [`app`, ...components],
minChunks: (module, count) => {
const vendorModuleList = []; // [`material-ui`, `lodash`];
const isFramework = _.some(
vendorModuleList.map(vendor => {
const regex = new RegExp(`[\\\\/]node_modules[\\\\/]${vendor}[\\\\/].*`, `i`);
return regex.test(module.resource);
})
);
return isFramework || count > 1;
}
}
]);
// config.plugin("BundleAnalyzerPlugin", BundleAnalyzerPlugin, [
// {
// analyzerMode: "static",
// reportFilename: "./report/treemap.html",
// openAnalyzer: true,
// logLevel: "error",
// defaultSizes: "gzip"
// }
// ]);
}
break;
}
return config;
};
exports.modifyBabelrc = ({ babelrc }) => {
return {
...babelrc,
plugins: babelrc.plugins.concat([`syntax-dynamic-import`, `dynamic-import-webpack`])
};
};