-
Notifications
You must be signed in to change notification settings - Fork 11
/
prerender.js
77 lines (69 loc) · 1.48 KB
/
prerender.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
const { resolve } = require('path')
/**
* @param outputDirectory
* @param params
*/
module.exports = function prerender(outputDirectory, params) {
params = params || {}
const entry = `./${outputDirectory}/ssr-bundle.js`,
url = params.url || '/'
global.window = {
addEventListener() {},
document: {
createElement() {
return {}
},
location: {
hash: `#${url}`,
href: url,
pathname: url,
replace() {},
search: '',
},
},
history: {},
navigator: { userAgent: '' },
pushState: {},
removeEventListener() {},
requestIdleCallback() {},
setTimeout() {},
}
global.navigator = global.window.navigator
global.window.location = global.window.document.location
global.history = global.window.history
global.document = global.window.document
global.location = global.window.location
global.chrome = {
runtime: {
sendMessage() {},
},
storage: {
local: {
get() {},
},
onChanged: {
addListener() {},
},
sync: {
get() {},
},
},
}
window.IntersectionObserver = class {}
window.ResizeObserver = class {}
let m
try {
m = require(entry)
} catch (e) {
console.error(e)
}
const app = (m && m.default) || m
if (typeof app !== 'function') {
console.warn('Entry does not export a Component function/class, aborting prerendering.')
return ''
}
const preact = require('preact')
const renderToString = require(`preact-render-to-string`)
return renderToString(preact.h(app))
}
//console.log(prerender('dist'))