1- import puppeteer from 'puppeteer' ;
1+ import puppeteer from 'puppeteer' ;
22import lighthouse from 'lighthouse' ;
33import log from 'lighthouse-logger' ;
4- import chromeLauncher from 'chrome-launcher' ;
54
6- export const getBrowserPath = async ( ) => {
7- const browserFetcher = puppeteer . createBrowserFetcher ( ) ;
8- const revisions = await browserFetcher . localRevisions ( ) ;
9- if ( revisions . length <= 0 ) {
10- throw new Error ( 'Could not find local browser' ) ;
11- }
12- const info = await browserFetcher . revisionInfo ( revisions [ 0 ] ) ;
13- return info . executablePath ;
14- } ;
5+ // export const getBrowserPath = async () => {
6+ // const browserFetcher = new BrowserFetcher ();
7+ // const revisions = await browserFetcher.localRevisions();
8+ // if (revisions.length <= 0) {
9+ // throw new Error('Could not find local browser');
10+ // }
11+ // const info = await browserFetcher.revisionInfo(revisions[0]);
12+ // return info.executablePath;
13+ // };
1514
16- export const runLighthouse = async ( browserPath , url , settings ) => {
15+ export const runLighthouse = async ( url , settings ) => {
1716 let chrome ;
1817 try {
1918 const logLevel = 'error' ;
2019 log . setLevel ( logLevel ) ;
21- chrome = await chromeLauncher . launch ( {
22- chromePath : browserPath ,
23- chromeFlags : [
20+ chrome = await puppeteer . launch ( {
21+ args : [
2422 '--headless' ,
2523 '--no-sandbox' ,
2624 '--disable-gpu' ,
2725 '--disable-dev-shm-usage' ,
26+ '--remote-debugging-port=0'
2827 ] ,
2928 logLevel,
3029 } ) ;
30+
31+ // Get the debugging port from the browser's websocket endpoint
32+ const browserWSEndpoint = chrome . wsEndpoint ( ) ;
33+ const port = parseInt ( browserWSEndpoint . split ( ':' ) [ 2 ] . split ( '/' ) [ 0 ] , 10 ) ;
34+
3135 const results = await lighthouse (
3236 url ,
3337 {
34- port : chrome . port ,
38+ port,
3539 output : 'html' ,
3640 logLevel,
3741 } ,
@@ -40,7 +44,7 @@ export const runLighthouse = async (browserPath, url, settings) => {
4044 return results ;
4145 } finally {
4246 if ( chrome ) {
43- await chrome . kill ( ) ;
47+ await chrome . close ( ) ;
4448 }
4549 }
4650} ;
0 commit comments