forked from runelite/runelite.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
167 lines (146 loc) · 3.84 KB
/
config-overrides.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
const {
override,
addWebpackAlias,
useBabelRc,
useEslintRc
} = require('customize-cra')
const fs = require('fs')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const PrerenderSPAPlugin = require('prerender-spa-plugin')
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer
const SitemapPlugin = require('sitemap-webpack-plugin').default
const md = require('markdown-it')
const fm = require('front-matter')
const parser = require('fast-xml-parser')
const hero = require('./src/_data/hero')
const parseBlog = require('./src/parse-blog')
const redirectConfig = require('./redirect')
// Escape html
const escapeHtml = unsafe => {
return unsafe.replace(/[&<"']/g, m => {
switch (m) {
case '&':
return '&'
case '<':
return '<'
case '"':
return '"'
default:
return '''
}
})
}
const feedMapper = fileName => {
// Setup path to file
const filePath = path.join(path.join('src', '_posts'), fileName)
// Read the content of the file
const fileContent = fs.readFileSync(filePath, 'utf-8')
// Extract front-matter context
const frontMatterContext = fm(fileContent)
// Parse blog metadata
const { id, date } = parseBlog(fileName)
// Make marked generate valid XHTML
const mdParser = md({
html: true,
xhtmlOut: true
})
// Extract metadata
const title = escapeHtml(frontMatterContext.attributes.title)
const description = escapeHtml(frontMatterContext.attributes.description)
const author = escapeHtml(frontMatterContext.attributes.author)
const body = mdParser.render(frontMatterContext.body)
const url = `${hero.url}/blog/show/${id}`
// Validate xml
const result = parser.validate('<div>' + body + '</div>')
if (result !== true) {
throw result
}
return {
url,
title,
author,
content: body,
summary: description,
updated: date.toISOString()
}
}
const addSitePlugins = () => config => {
if (process.env.NODE_ENV !== 'production') {
return config
}
const posts = fs.readdirSync(path.join('src', '_posts'))
const routes = ['/', '/features', '/blog', '/tag', '/plugin-hub']
.map(path => ({ path }))
.concat(
posts.map(fileName => {
// Parse blog metadata
const { id, date } = parseBlog(fileName)
return {
path: '/blog/show/' + id,
lastMod: date.toISOString().slice(0, 10)
}
})
)
config.plugins.push(
new PrerenderSPAPlugin({
// Required - The path to the webpack-outputted app to prerender.
staticDir: path.join(__dirname, 'build'),
// Required - Routes to render.
routes: routes.map(({ path }) => path),
// Make prerendering more stable
renderer: new Renderer({
maxConcurrentRoutes: 1,
renderAfterTime: 1000
})
})
)
config.plugins.push(
new SitemapPlugin(hero.url, routes, {
lastMod: true,
changeFreq: 'weekly'
})
)
config.plugins.push(
new HtmlWebpackPlugin({
feed: {
url: hero.url,
title: hero.title,
subtitle: hero.description,
entries: posts.map(feedMapper).reverse()
},
template: 'public/atom.html',
filename: 'atom.xml',
minify: false,
inject: false,
xhtml: false
})
)
for (let key in redirectConfig) {
if (!redirectConfig.hasOwnProperty(key)) {
continue
}
const link = redirectConfig[key]
config.plugins.push(
new HtmlWebpackPlugin({
redirect: {
url: link
},
template: 'redirect.html',
filename: key + '/index.html',
inject: false,
xhtml: false
})
)
}
return config
}
module.exports = override(
useBabelRc(),
useEslintRc(),
addWebpackAlias({
react: 'preact/compat',
'react-dom': 'preact/compat'
}),
addSitePlugins()
)