-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderStatic.js
104 lines (89 loc) · 2.8 KB
/
renderStatic.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
const fs = require('fs');
const handlebars = require('handlebars');
const minify = require('html-minifier').minify;
require('ignore-styles');
if (!process.env.BROWSER) {
global.window = { ssr: true };
global.document = {
createElement: () =>{return {el:{}, prop:{}, appendChild:()=>{return{};}};},
getElementsByTagName: () => { return {appendChild:()=>{return{};}}; },
appendChild: () => { return {}; },
};
}
require('@babel/register') ({
ignore: [/(node_modules)/],
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
'@babel/syntax-dynamic-import',
'@babel/plugin-transform-runtime',
'@babel/plugin-syntax-object-rest-spread',
'@loadable/babel-plugin',
],
});
const renderPage = require('./renderPage.js');
const normalizeCssStr = fs.readFileSync('./public/css/normalize.min.css', 'utf-8');
const fontAwesomeCss = require('@fortawesome/fontawesome-svg-core').dom.css();
const fontsCss = fs.readFileSync('./public/fonts/fonts.css', 'utf-8');
let additionalStyleTags = `
<style>${normalizeCssStr}</style>
<style>${fontAwesomeCss}</style>
<style>${fontsCss}</style>
`;
const renderHTML = renderData => {
return renderPage.default(renderData);
};
const template = handlebars.compile(fs.readFileSync('./src/index.hbs').toString());
const writeHTML = (rendered, renderData) => {
const html = template({
body: rendered.html,
headTags: rendered.headTags,
scriptTags: rendered.scriptTags,
linkTags: rendered.linkTags,
styleTags: rendered.styleTags,
renderData: renderData,
additionalStyleTags: additionalStyleTags,
production: process.env.NODE_ENV == 'production'
});
let pathUrl = '';
if(renderData.path !== '/') {
pathUrl = renderData.path;
}
if(!fs.existsSync(`./public${pathUrl}`)) {
fs.mkdirSync(`./public${pathUrl}`)
}
fs.writeFileSync(`./public${pathUrl}/index.html`, minify(html, {
removeComments: true,
}), 'utf8');
console.log("Generated HTML", `./public${pathUrl}/index.html`);
};
const routes = [
'/',
'/about',
'/signup',
'/signup-validator',
'/news',
'/videos',
'/privacy-notice',
'/instructions',
'/instructions/mobile-app',
'/instructions/watchers',
'/instructions/what-to-watch-for',
'/instructions/how-to-validate',
'/instructions/how-to-take-photos',
'/ti-glasuvash',
'/izvan-bulgaria',
'/mobile-app-feedback',
'/feedback-defenders',
'/feedback-validators',
];
console.log('\nGENERATING STATIC HTML\n');
for(const route of routes) {
const renderData = {path: route};
const rendered = renderHTML(renderData);
writeHTML(rendered, renderData);
}
console.log('\nDONE\n');
process.exit(0);