-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
116 lines (110 loc) · 3.12 KB
/
gatsby-node.ts
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
import {GatsbyNode} from "gatsby";
import path from "path";
import {Compilation} from "webpack";
import {PostLayoutType} from "./src/types";
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({actions: {createTypes}}) => {
createTypes(`
type Mdx implements Node {
frontmatter: MdxFrontmatter
}
type MdxFrontmatter {
slug: String
tags: [String]
title: String
code: String
demo: String
description: String
date: Date @dateformat
featuredImage: File @fileByRelativePath
techs: [String]
}
`);
};
export const createPages: GatsbyNode["createPages"] = async ({graphql, actions}) => {
const {createPage} = actions;
const result = await graphql<PostLayoutType>(`
{
allMdx(sort: {frontmatter: {date: DESC}}) {
edges {
node {
id
frontmatter {
slug
title
date
description
tags
code
code2
demo
featuredImage {
publicURL
}
techs
}
internal {
contentFilePath
}
body
tableOfContents
}
next {
frontmatter {
slug
title
featuredImage {
childImageSharp {
gatsbyImageData(
layout: CONSTRAINED
formats: [AUTO, WEBP]
width: 50
)
}
}
}
}
previous {
frontmatter {
slug
title
featuredImage {
childImageSharp {
gatsbyImageData(
layout: CONSTRAINED
formats: [AUTO, WEBP]
width: 50
)
}
}
}
}
}
}
}
`);
if (result.errors) {
console.log('Error Creating Projects', result.errors);
return;
}
const postTemplate = path.resolve("src/templates/PostLayout.tsx");
const posts = result.data?.allMdx.edges || [];
posts.forEach(({node, next, previous}) => {
const {frontmatter, internal: {contentFilePath}} = node;
createPage({
path: frontmatter.slug, component: `${postTemplate}?__contentFilePath=${contentFilePath}`, context: {node, next, previous},
});
});
};
export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] = ({actions}) => {
actions.setWebpackConfig({
output: {
filename: '[name].[fullhash].js', // Replace [hash] with [fullhash]
}, plugins: [{
apply: (compiler: any) => {
compiler.hooks.compilation.tap('MyPlugin', (compilation: any) => {
compilation.getAssetPath = Compilation.prototype.getAssetPath; // Replace MainTemplate.getAssetPath
});
},
},],
});
};