/
renderHtmlPage.js
80 lines (70 loc) 路 2.91 KB
/
renderHtmlPage.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
/* @flow */
import Helmet from 'react-helmet';
import serialize from 'serialize-javascript';
import _ from 'lodash';
import type { Store } from '../types';
export default (store: Store, htmlApp: string = '') => {
const head = Helmet.rewind();
const assets = webpackIsomorphicTools.assets();
// Setup html page
return `
<!DOCTYPE html>
<html ${head.htmlAttributes.toString()}>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Language" content="en" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
${head.base.toString()}
${head.title.toString()}
${head.meta.toString()}
${head.link.toString()}
${
// Styles will be presented in production with webpack extract text plugin
_.keys(assets.styles).map(style =>
`<link href="${assets.styles[style]}" media="screen, projection" rel="stylesheet" type="text/css" />`)
.join('\n')
}
${
// Styles will be presented in development mode
// I put all of the styles here to smoothen the flick
_.keys(assets.styles).length === 0 ?
`<style>${
// $FlowFixMe: It's not an issue
require('../theme/normalize.css')._style +
// $FlowFixMe: It's not an issue
require('../containers/App/styles.scss')._style +
// $FlowFixMe: It's not an issue
require('../containers/Home/styles.scss')._style +
// $FlowFixMe: It's not an issue
require('../containers/UserInfo/styles.scss')._style +
// $FlowFixMe: It's not an issue
require('../containers/NotFound/styles.scss')._style +
// $FlowFixMe: It's not an issue
require('../components/UserList/styles.scss')._style +
// $FlowFixMe: It's not an issue
require('../components/UserCard/styles.scss')._style
}</style>` : ''
}
</head>
<body>
<div id="react-view">${htmlApp || ''}</div>
<script type="text/javascript">
${store && `window.__INITIAL_STATE__=${serialize(store.getState())}`}
</script>
<!--[if gte IE 9 ]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-sham.min.js"></script>
<![endif]-->
${
// Reverse the order of scripts for accessing vendor.js first
_.keys(assets.javascript).reverse().map(script =>
`<script src="${assets.javascript[script]}"></script>`)
.join('\n')
}
${head.script.toString()}
</body>
</html>
`;
};