forked from ti-broish/public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderStatic.js
95 lines (80 loc) · 2.46 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
const fs = require('fs');
const ejs = require('ejs');
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();
let additionalStyleTags = `
<style>${normalizeCssStr}</style>
<style>${fontAwesomeCss}</style>
`;
const renderHTML = renderData => {
return renderPage.default(renderData);
};
const writeHTML = (rendered, renderData) => {
const template = fs.readFileSync('./src/index.ejs').toString();
const html = ejs.render(template, {
body: rendered.html,
headTags: rendered.headTags,
scriptTags: rendered.scriptTags,
linkTags: rendered.linkTags,
styleTags: rendered.styleTags,
renderData: renderData,
additionalStyleTags: additionalStyleTags
});
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,
}));
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-take-photos',
];
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);