-
Notifications
You must be signed in to change notification settings - Fork 4
/
static.config.js
187 lines (176 loc) · 4.91 KB
/
static.config.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
import axios from 'axios';
import { createClient } from 'contentful';
import 'dotenv/config';
import { highlightAuto } from 'highlight.js';
import ImageminPlugin from 'imagemin-webpack-plugin';
import marked from 'marked';
import path from 'path';
import React, { Component } from 'react';
// TODO: import slugTag from 'src/lib/slugTag';
import { ServerStyleSheet } from 'styled-components';
import { generateSW } from 'workbox-build';
const slugTag = text =>
text
.toLowerCase()
.trim()
.replace(/[\s\-]+/g, '-');
marked.setOptions({
highlight: function(code) {
return highlightAuto(code).value;
},
});
const typescriptWebpackPaths = require('./webpack.config.js');
export default {
siteRoot: 'https://www.dadoune.com',
entry: path.join(__dirname, 'src', 'index.tsx'),
getSiteData: () => ({
author: 'Reed Dadoune',
googleAnalytics: 'UA-48078670-1',
host: 'www.dadoune.com',
siteName: 'Reed Dadoune',
}),
getRoutes: async () => {
const { data: repos } = await axios.get(
'https://api.github.com/users/reedd/repos?type=owner&sort=pushed',
);
const { data: colors } = await axios.get(
'https://raw.githubusercontent.com/doda/github-language-colors/master/colors.json',
);
const client = createClient({
space: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
});
const { items } = await client.getEntries({
content_type: process.env.CONTENTFUL_BLOG_CONTENT_ID,
});
const blogs = items
.map(i => ({
...i.fields,
body: marked(i.fields.body),
publishedAt: i.fields.publishedAt || i.sys.createdAt,
tags: i.fields.tags || [],
}))
.sort((a, b) => a.publishedAt < b.publishedAt);
const tags = new Set();
blogs.forEach(blog => {
blog.tags.forEach(tag => tags.add(slugTag(tag)));
});
return [
{
path: '/',
component: 'src/containers/Home',
},
{
path: '/projects',
component: 'src/containers/Projects',
getData: () => ({
repos: repos
.filter(r => !r.fork)
.map(r => ({ ...r, language_color: colors[r.language] })),
}),
},
{
path: '/blog',
component: 'src/containers/Blog',
getData: () => ({
blogs,
}),
children: Array.from(tags)
.map(tag => ({
path: `/tag/${tag}`,
component: 'src/containers/Blog',
getData: () => ({
blogs: blogs.filter(blog => {
const tags = new Set();
blog.tags.forEach(tag => tags.add(slugTag(tag)));
return tags.has(tag);
}),
}),
}))
.concat(
blogs.map(blog => ({
path: `/${blog.slug}`,
component: 'src/containers/Post',
getData: () => ({
blog,
}),
})),
),
},
{
is404: true,
component: 'src/containers/404',
},
];
},
renderToHtml: (render, Comp, meta) => {
const sheet = new ServerStyleSheet();
const html = render(sheet.collectStyles(<Comp />));
meta.styleTags = sheet.getStyleElement();
return html;
},
Document: class CustomHtml extends Component {
render() {
const { Html, Head, Body, children, renderMeta } = this.props;
return (
<Html>
<Head>
{renderMeta.styleTags}
</Head>
<Body>{children}</Body>
</Html>
);
}
},
webpack: (config, { defaultLoaders }) => {
// Add .ts and .tsx extension to resolver
config.resolve.extensions.push('.ts', '.tsx');
defaultLoaders.fileLoader.query.limit = 1;
// We replace the existing JS rule with one, that allows us to use
// both TypeScript and JavaScript interchangeably
config.module.rules = [
{
oneOf: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: defaultLoaders.jsLoader.exclude, // as std jsLoader exclude
use: [
{
loader: 'babel-loader',
},
{
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
},
},
],
},
defaultLoaders.fileLoader,
],
},
];
config.plugins.push(
new ImageminPlugin({
disable: process.env.NODE_ENV !== 'production', // Disable during development
pngquant: {
quality: '95-100',
},
}),
);
return config;
},
onBuild: async () => {
await generateSW({
globDirectory: './dist',
globPatterns: ['static/*', '**/*.js'],
swDest: 'dist/sw.js',
runtimeCaching: [
{
handler: 'cacheFirst',
urlPattern: /routeInfo|\/[^\.]*$/i,
},
],
});
},
};