-
Notifications
You must be signed in to change notification settings - Fork 26
/
JSDOMDomProvider.js
79 lines (70 loc) · 2.16 KB
/
JSDOMDomProvider.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
import { JSDOM, VirtualConsole } from 'jsdom';
const { VERBOSE } = process.env;
const MAX_ERROR_DETAIL_LENGTH = 200;
export default class JSDOMDomProvider {
constructor(jsdomOptions, { width, height, webpackBundle }) {
const virtualConsole = new VirtualConsole();
virtualConsole.on('jsdomError', (e) => {
const { stack, detail = '' } = e;
if (VERBOSE || typeof detail !== 'string' || detail.length < MAX_ERROR_DETAIL_LENGTH) {
console.error(stack, detail);
} else {
const newDetail = `${(detail || '').slice(0, MAX_ERROR_DETAIL_LENGTH)}...
To see the full error, run happo with "VERBOSE=true")`;
console.error(stack, newDetail);
}
});
virtualConsole.sendTo(console, { omitJSDOMErrors: true });
this.dom = new JSDOM(
`
<!DOCTYPE html>
<html>
<head>
<script src='file://${webpackBundle}'></script>
</head>
<body>
</body>
</html>
`.trim(),
Object.assign(
{
runScripts: 'dangerously',
resources: 'usable',
url: 'http://localhost',
virtualConsole,
beforeParse(win) {
win.outerWidth = win.innerWidth = width;
win.outerHeight = win.innerHeight = height;
Object.defineProperties(win.screen, {
width: { value: width },
availWidth: { value: width },
height: { value: height },
availHeight: { value: height },
});
win.requestAnimationFrame = (callback) => setTimeout(callback, 0);
win.cancelAnimationFrame = clearTimeout;
},
},
jsdomOptions,
),
);
}
async init({ targetName }) {
await new Promise((resolve) => {
this.dom.window.onBundleReady = resolve;
});
return this.dom.window.happoProcessor.init({ targetName });
}
next() {
return this.dom.window.happoProcessor.next();
}
processCurrent() {
return this.dom.window.happoProcessor.processCurrent();
}
extractCSS() {
return this.dom.window.happoProcessor.extractCSS();
}
close() {
this.dom.window.close();
}
}