forked from happo/happo-plugin-puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PuppeteerDomProvider.js
59 lines (52 loc) · 1.53 KB
/
PuppeteerDomProvider.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
const puppeteer = require('puppeteer');
module.exports = class PuppeteerDomProvider {
constructor({ launchOptions }, { width, height, webpackBundle }) {
this.viewport = { width, height };
this.webpackBundle = webpackBundle;
this.launchOptions = launchOptions;
}
init({ targetName } = {}) {
return puppeteer
.launch(this.launchOptions)
.then(browser => {
this.browser = browser;
})
.then(() => this.browser.newPage())
.then(page => {
this.page = page;
this.page.on('console', async msg => {
// serialize my args the way I want
const args = await Promise.all(
msg.args().map(arg =>
arg.executionContext().evaluate(arg => {
if (arg instanceof Error) {
return arg.stack;
}
return arg;
}, arg),
),
);
console.log(...args);
});
})
.then(() => this.page.setViewport(this.viewport))
.then(() => this.page.addScriptTag({ path: this.webpackBundle }))
.then(() =>
this.page.evaluate(
`window.happoProcessor.init({ targetName: "${targetName}" })`,
),
);
}
next() {
return this.page.evaluate('window.happoProcessor.next()');
}
processCurrent() {
return this.page.evaluate('window.happoProcessor.processCurrent()');
}
extractCSS() {
return this.page.evaluate('window.happoProcessor.extractCSS()');
}
close() {
return this.browser?.close();
}
};