-
Notifications
You must be signed in to change notification settings - Fork 760
/
static.js
331 lines (291 loc) 路 8.97 KB
/
static.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* eslint-disable import/no-dynamic-require, react/no-danger */
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { renderToString } from 'react-dom/server'
import fs from 'fs-extra'
import path from 'path'
import Helmet from 'react-helmet'
import OpenPort from 'openport'
//
import DefaultHtml from './DefaultHtml'
import { pathJoin } from './shared'
import { DIST, ROOT } from './paths'
const defaultEntry = './src/index'
const path404 = '/404'
// Normalize routes with parents, full paths, context, etc.
const normalizeRoutes = routes => {
const flatRoutes = []
const recurse = (route, parent = { path: '/' }) => {
const routePath = route.is404 ? path404 : pathJoin(parent.path, route.path)
if (typeof route.noIndex !== 'undefined') {
console.log(`=> Warning: Route ${route.path} is using 'noIndex'. Did you mean 'noindex'?`)
route.noindex = route.noIndex
}
const normalizedRoute = {
...route,
path: routePath,
noindex: typeof route.noindex === 'undefined' ? parent.noindex : route.noindex,
hasGetProps: !!route.getProps,
}
if (!normalizedRoute.path) {
throw new Error('No path defined for route:', normalizedRoute)
}
if (route.children) {
route.children.forEach(d => recurse(d, normalizedRoute))
}
delete normalizedRoute.children
flatRoutes.push(normalizedRoute)
}
routes.forEach(d => recurse(d))
flatRoutes.forEach(route => {
const found = flatRoutes.find(d => d.path === route.path)
if (found !== route) {
console.warn('More than one route is defined for path:', route.path)
}
})
if (!flatRoutes.find(d => d.is404)) {
flatRoutes.push({
is404: true,
path: path404,
})
}
return flatRoutes
}
export const getConfig = () => {
const config = require(path.resolve(path.join(process.cwd(), 'static.config.js'))).default
return {
getSiteProps: () => ({}),
...config,
siteRoot: config.siteRoot ? config.siteRoot.replace(/\/{0,}$/g, '') : null,
getRoutes: async (...args) => {
const routes = await config.getRoutes(...args)
return normalizeRoutes(routes)
},
}
}
export const writeRoutesToStatic = async ({ config }) => {
const userConfig = getConfig()
const HtmlTemplate = config.Html || DefaultHtml
const Comp = require(path.join(ROOT, userConfig.entry || defaultEntry)).default
const siteProps = await userConfig.getSiteProps({ dev: false })
return Promise.all(
config.routes.map(async route => {
// Fetch initialProps from each route
const initialProps = route.getProps && (await route.getProps({ route, dev: false }))
if (initialProps) {
await fs.outputFile(
path.join(DIST, route.path, 'routeData.json'),
JSON.stringify(initialProps || {}),
)
}
const URL = route.path
// Inject initialProps into static build
class InitialPropsContext extends Component {
static childContextTypes = {
initialProps: PropTypes.object,
siteProps: PropTypes.object,
URL: PropTypes.string,
}
getChildContext () {
return {
initialProps,
siteProps,
URL,
}
}
render () {
return this.props.children
}
}
const ContextualComp = <InitialPropsContext>{Comp}</InitialPropsContext>
let staticMeta = {}
if (config.preRenderMeta) {
// Allow the user to perform custom rendering logic (important for styles and helmet)
staticMeta = {
...staticMeta,
...(await config.preRenderMeta(ContextualComp)),
}
}
const appHtml = renderToString(ContextualComp)
// Extract head calls using Helmet
const helmet = Helmet.renderStatic()
const head = {
htmlProps: helmet.htmlAttributes.toComponent(),
bodyProps: helmet.bodyAttributes.toComponent(),
base: helmet.base.toComponent(),
link: helmet.link.toComponent(),
meta: helmet.meta.toComponent(),
noscript: helmet.noscript.toComponent(),
script: helmet.script.toComponent(),
style: helmet.style.toComponent(),
title: helmet.title.toComponent(),
}
staticMeta.head = head
if (config.postRenderMeta) {
// Allow the user to perform custom rendering logic (important for styles and helmet)
staticMeta = {
...staticMeta,
...(await config.postRenderMeta(appHtml)),
}
}
const Html = ({ children, ...rest }) => (
<html lang="en" {...head.htmlprops} {...rest}>
{children}
</html>
)
const Head = ({ children, ...rest }) => (
<head {...rest}>
{head.base}
{head.link}
{head.meta}
{head.noscript}
{head.script}
{head.style}
{head.title}
{children}
</head>
)
const Body = ({ children, ...rest }) => (
<body {...head.bodyProps} {...rest}>
{children}
<script
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `window.__routeData = ${JSON.stringify({
path: route.path,
initialProps,
siteProps,
})}`,
}}
/>
<script async src="/app.js" />
</body>
)
let html = `<!DOCTYPE html>${renderToString(
<HtmlTemplate
staticMeta={staticMeta}
Html={Html}
Head={Head}
Body={Body}
siteProps={siteProps}
>
<div id="root" dangerouslySetInnerHTML={{ __html: appHtml }} />
</HtmlTemplate>,
)}`
if (config.siteRoot) {
html = html.replace(/(href=["'])(\/[^/])/gm, `$1${config.siteRoot}$2`)
}
console.log(route)
const htmlFilename = route.is404
? path.join(DIST, '404.html')
: path.join(DIST, route.path, 'index.html')
const initialPropsFilename = path.join(DIST, route.path, 'routeData.json')
const writeHTML = fs.outputFile(htmlFilename, html)
const writeJSON = fs.outputFile(
initialPropsFilename,
JSON.stringify({
initialProps,
}),
)
await Promise.all([writeHTML, writeJSON])
}),
)
}
export async function buildXMLandRSS ({ config }) {
if (!config.siteRoot) {
console.log(
`
=> Warning: No 'siteRoot' defined in 'static.config.js'!
=> This is required for both absolute url's and a sitemap.xml to be exported.
`,
)
return
}
const xml = generateXML({
routes: config.routes.filter(d => !d.is404).map(route => ({
permalink: config.siteRoot + route.path,
lastModified: '',
priority: 0.5,
...route,
})),
})
await fs.writeFile(path.join(DIST, 'sitemap.xml'), xml)
function generateXML ({ routes }) {
let xml =
'<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
routes.forEach(route => {
if (route.noindex) {
return
}
xml += '<url>'
xml += `<loc>${`${route.permalink}/`.replace(/\/{1,}$/gm, '/')}</loc>`
xml += route.lastModified ? `<lastmod>${route.lastModified}</lastmod>` : ''
xml += route.priority ? `<priority>${route.priority}</priority>` : ''
xml += '</url>'
})
xml += '</urlset>'
return xml
}
}
export const writeRouteComponentsToFile = async routes => {
const templates = []
routes = routes.filter(d => d.component)
routes.forEach(route => {
if (!templates.includes(route.component)) {
templates.push(route.component)
}
})
const standardRoutes = routes.filter(d => !d.is404)
const notFoundRoute = routes.find(d => d.is404)
const file = `
import React, { Component } from 'react'
import { Switch, Route } from 'react-router-dom'
${templates
.map(
template =>
`import ${template.replace(/[^a-zA-Z]/g, '_')} from '${path.resolve(ROOT, template)}'`,
)
.join('\n')}
export default class Routes extends Component {
render () {
return (
<Switch>
${standardRoutes
.map(
route =>
`<Route exact path={'${route.path}'} component={${route.component.replace(
/[^a-zA-Z]/g,
'_',
)}} />`,
)
.join(',\n')}
${notFoundRoute
? `<Route component={${notFoundRoute.component.replace(/[^a-zA-Z]/g, '_')}} />`
: ''}
</Switch>
)
}
}
`
const filepath = path.resolve(DIST, 'react-static-routes.js')
await fs.remove(filepath)
await fs.writeFile(filepath, file)
const now = Date.now() / 1000
const then = now - 1000
fs.utimesSync(filepath, then, then)
}
export const findAvailablePort = start =>
new Promise((resolve, reject) =>
OpenPort.find(
{
startingPort: start,
endingPort: start + 1000,
},
(err, port) => {
if (err) {
return reject(err)
}
resolve(port)
},
),
)