Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erezrokah/lighthouse-layer
A Lambda layer with all the required dependencies to run Google Lighthouse
https://github.com/erezrokah/lighthouse-layer
Last synced: 3 months ago
JSON representation
A Lambda layer with all the required dependencies to run Google Lighthouse
- Host: GitHub
- URL: https://github.com/erezrokah/lighthouse-layer
- Owner: erezrokah
- License: mit
- Created: 2019-07-22T12:44:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-08T00:54:06.000Z (9 months ago)
- Last Synced: 2024-04-08T02:15:07.640Z (9 months ago)
- Language: JavaScript
- Size: 416 KB
- Stars: 36
- Watchers: 4
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Lighthouse Lambda Layer
A Lambda layer with all the required dependencies to run Google Lighthouse.
## Prerequisites
[Nodejs](https://nodejs.org/en/) (at least version 10)
[Yarn](https://yarnpkg.com/lang/en/)
Amazon AWS account and `awscli` installed and configured:
Serverless [CLI](https://serverless.com/framework/docs/getting-started/)
## Setup
```bash
yarn
```## Deploy
```bash
yarn deploy
```### Usage
In your `serverless.yml`:
```yaml
functions:
functionThatUsesLighthouse:
layers:
- layerArn # You'll have this after you run the deploy command
```In you Lambda function:
```ts
const lighthouse = require('lighthouse');
const log = require('lighthouse-logger');
const chromeLauncher = require('chrome-launcher');let chromePath = undefined;
// this lets us support invoke local
if (!process.env.IS_LOCAL) {
chromePath = '/opt/bin/chromium';// https://github.com/alixaxel/chrome-aws-lambda/blob/3779715fdc197a245af662725977133b2d676bf9/source/index.js#L6
// required for node10 support - makes sure fonts and shared libraries are loaded correctly
if (process.env.FONTCONFIG_PATH === undefined) {
process.env.FONTCONFIG_PATH = '/opt/lib';
}if (
process.env.LD_LIBRARY_PATH &&
process.env.LD_LIBRARY_PATH.startsWith('/opt/lib:') !== true
) {
process.env.LD_LIBRARY_PATH = [
...new Set(['/opt/lib', ...process.env.LD_LIBRARY_PATH.split(':')]),
].join(':');
}
}const chromeFlags = [
'--headless',
'--disable-dev-shm-usage',
'--disable-gpu',
'--no-zygote',
'--no-sandbox',
'--single-process',
'--hide-scrollbars',
];// utility function to run lighthouse
const runLighthouse = async url => {
let chrome = null;
try {
chrome = await chromeLauncher.launch({ chromeFlags, chromePath });const options = {
port: chrome.port,
logLevel: 'info',
};log.setLevel(options.logLevel);
const results = await lighthouse(url, options);
return results;
} finally {
if (chrome) {
await chrome.kill();
}
}
};// do something with runLighthouse
```