/
crawler.js
97 lines (75 loc) · 2.39 KB
/
crawler.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
import ejs from 'ejs';
import {join, resolve} from 'path';
import {readFile, writeFile} from 'fs';
import {mkdirp} from 'mkdirp';
import {ChunkExtractor} from '@loadable/server';
import { parse } from 'node-html-parser';
import React from 'react';
import {renderToString} from 'react-dom/server';
const statsFile = resolve('./dist/loadable-stats.json')
import labs from './src/lab';
import work from './src/work';
import {App} from './src/components/App.js';
import {RouterContext} from "@stilva/transitionable-react-router";
readFile('./dist/index.html', (err, buff) => {
const parsedTemplate = parse(buff.toString());
['/', '/lab', '/work'].forEach(path => writeToFile(parsedTemplate, path));
getLabs()
.forEach(path => writeToFile(parsedTemplate, path, true));
getWorks()
.forEach(path => writeToFile(parsedTemplate, path, true));
});
function getWorks() {
return Array
.from(work.keys())
.map(slug => `/work/${slug}`);
}
function getLabs() {
return Array
.from(labs.keys())
.map(slug => `/lab/${slug}`);
}
function writeToFile(tpl, path, isEntry = false) {
const options = {
entrypoints: ["bundle"],
statsFile
};
const extractor = new ChunkExtractor(options);
const jsx = extractor.collectChunks(<RouterContext.Provider value={{currentRoute: path}}>
<App/>
</RouterContext.Provider>);
tpl.querySelector('#app').set_content(renderToString(jsx));
const filename = join(`${path === '/' ? 'index' : path}.html`);
const folderPattern = /^(\/.+\/)?[^\.]+\.(?:html)$/ig;
const match = folderPattern.exec(filename);
const stringified = tpl.toString();
if (!match) return;
pMakeDirOrSkip({prefix: './dist', folder: match[1], uri: filename})
.then(({prefix, folder, uri}) => {
writeFile(join(prefix, uri), stringified, err => {
if (err) {
console.log('Error while writing to file', err.toString())
}
});
});
}
function pMakeDirOrSkip({prefix, folder, uri}) {
return new Promise(resolve => {
if (!folder) {
return resolve({prefix, folder, uri});
}
return pMkdirp({prefix, folder, uri})
.then(resolve);
});
}
function pMkdirp({prefix, folder, uri}) {
return new Promise(resolve => {
mkdirp(`${prefix}${folder}`, function (err) {
if (err) {
console.error("ERROR", err.toString())
process.exit();
}
resolve({prefix, folder, uri});
});
});
}