Giter Club home page Giter Club logo

webpack-lighthouse-plugin's Introduction

npm version npm

Webpack Lighthouse Plugin

This plugin allows you to run Lighthouse from a Webpack build.

Installation

npm install --save-dev webpack-lighthouse-plugin

Setup

In webpack.config.js:

const WebpackLighthousePlugin = require('webpack-lighthouse-plugin');

module.exports = {
  ...
  plugins: [
    new WebpackLighthousePlugin({
      url: 'http://localhost:9001'
    })
  ],
  ...
}

Example

Insert into your webpack.config.js:

const WebpackLighthousePlugin = require('webpack-lighthouse-plugin');

module.exports = {
  entry: 'sample.js',
  output: {
    filename: 'test.js'
  },
  plugins: [
    new WebpackLighthousePlugin({
      url: 'https://airhorner.com'
    })
  ],
};

API

  • url - the URL to run Lighthouse audits against
  • perf - only report Lighthouse performance audits (instead of the full Progressive Web App audits)
  • disableDeviceEmulation - disables device emulation (false by default)
  • disableCPUThrottling - disables cpu throttling (true by default)
  • disableNetworkThrottling - disables network throttling (false by default)
  • saveAssets - save the trace contents & screenshots to disk
  • saveArtifacts - save all gathered artifacts to disk

Examples

Performance metrics only

Just get the time to first meaningful paint, time-to-interactive and perceptual speed-index:

plugins: [
  new WebpackLighthousePlugin({
    url: 'https://airhorner.com',
    perf: true
  })
],

Test with CPU, network throttling and device emulation

plugins: [
  new WebpackLighthousePlugin({
    url: 'https://airhorner.com',
    disableCPUThrottling: false
  })
],

Save build assets (screenshots, trace and report):

plugins: [
  new WebpackLighthousePlugin({
    url: 'https://airhorner.com',
    saveAssets: true
    })
],

If you require even more data, you can also pass saveArtifacts: true.

Running webpack-lighthouse-plugin with real mobile devices

Similar to the Lighthouse module, this plugin can also be used with real phones. It works over remote debugging using the Android command line tools.

Before running the plugin as part of your Webpack build, run the following commands:

$ adb kill-server
$ adb devices -l
$ adb forward tcp:9222 localabstract:chrome_devtools_remote

You can then run webpack against your build and instead of firing up a Chrome instance on desktop, it'll do this with your mobile device Chrome instead. You will want to disable a few flags to improve the accuracy of your metrics:

plugins: [
  new WebpackLighthousePlugin({
    url: 'https://localhost:9000', // Port you are locally serving on
    disableDeviceEmulation: true,
    disableCPUThrottling: true,
    disableNetworkThrottling: true // Only if you're going to use real 3G
  })
],

Webpack Dev Server

Note: Webpack Dev Server targets development builds rather than production. Although you can run Lighthouse against a dev build, it's best run against builds closer to prod.

If you're trying to use webpack-dev-server with this plugin, first run it against your local build using the webpack-dev-server CLI:

$ webpack-dev-server build/
  http://localhost:8080/webpack-dev-server/

Then make sure to reference the webpack-dev-server URL in your WebpackLighthousePlugin config:

plugins: [
 new WebpackLighthousePlugin({
   url: 'http://localhost:8080/webpack-dev-server/'
 })
],

Developing

If opening a pull request, create an issue describing a fix or feature. Have your pull request point to the issue by writing your commits with the issue number in the message.

Make sure you lint your code by running npm run lint and you can build the library by running npm run build.

License

Released under an Apache-2.0 license.

webpack-lighthouse-plugin's People

Contributors

addyosmani avatar brandly avatar evenstensberg avatar patrickhulce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webpack-lighthouse-plugin's Issues

support for chrome launcher options

there is the need to configure webpack dev-server to use https in order to work with service worker. webpack dev-server can be configured to serve self-signed certificate. as a result, chromium needs to launched with --ignore-certificate-errors and --unsafely-treat-insecure-origin-as-secure=https://localhost:8080 arguments, but there is no straightforward way to provide chromium arguments to lighthouse-plugin.

chrome-launcher options supports chromeFlags which is utilised by lighthouse.

  1. is there any way to provide the arguments for chrome-launcher?
  2. is there any way to control the plugin execution only when the webpack dev-server kicks in and not for webpack in general (for instance, not to run when only bundeling takes place)?

Add ability to set saveAssets/saveArtifacts/advanced config

Lighthouse currently doesn't provide a way to override the flags object when consuming this API via https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-cli/bin.ts#L201. Instead, it lets you specify config but the flags object is just consumed from whatever is configured at a CLI level.

Short term we can either PR in a fix for this upstream or host our own copy of bin.js that does allow you to customize the flags object. That would allow our defaultOptions to be fully applied (right now I'm pretty sure only the url and configPath parts are respected.

Greater Clarity about how this might work in a CI/CD pipeline

Thanks so much for your work on web perf in general and Lighthouse in particular. The tools are super useful, and I'm hoping to figure out a way to generate Lighthouse reports in my CI environment on every build. I currently generate and archive Istanbul LCOV and Webpack Bundle Size reports in this manner. However, I'm not really clear on how this might work with this plugin.

The README shows that I need to preconfigure the plugin with an endpoint to test. Assuming that Webpack is generating is building the production bundle that I'm serving, I'm a bit unclear about how I might locally serve this new production bundle, execute the plugin, and then tear everything down.

Am I perhaps misunderstanding the use case for this plugin? If not, do you have any suggestions about how best to achieve this?

TypeError: Cannot read property 'afterEmit' of undefined

After installing the plugin via yarn, and importing it into my development.js config, I get this error TypeError: Cannot read property 'afterEmit' of undefined when I run webpack-dev-server --hot

I am using this plugin in a rails app using webpacker.

My webpacker version: "@rails/webpacker": "^3.5.3"

And here's my development.js config

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')
const WebpackLighthousePlugin = require('webpack-lighthouse-plugin')

environment.plugins.append(
  'WebpackLighthousePlugin',
  new WebpackLighthousePlugin({
    url: 'http://localhost:3000'
  })
)

module.exports = environment.toWebpackConfig()

Do I need to upgrade webpacker? Saw a similar issue over here for another plugin

Is this still needed?

I think we could archive this repo. Users can run webpack and then lighthouse themselves.

Performance metrics only does not work

The perf option seems to be ignored, running the following two configs creates the same report:

plugins: [
  new WebpackLighthousePlugin({
    url: 'https://airhorner.com',
    perf: true
  })
]
plugins: [
  new WebpackLighthousePlugin({
    url: 'https://airhorner.com'
  })
]

Additional output options

It'd be nice to have the ability to configure options like the output format (JSON, HTML, other) and to specify the output location.
Lighthouse offers options of:

  • --output
  • --output-path

output Json

Hello,

I am investigating how to run performance audits locally with lighthouse and allow for performance budgets much like what webpack does with size budgets but giving the power to the user.

I am aware that running performance audits in a "dev environment" is less than ideal but this would be a first opt-in perf indicator and a tool to be improved upon.

The first step to doing this would be by retrieving the lighthouse results in JSON format and then evaluate them against user defined values.

Lighthouse as a standalone already enables JSON outputs but I am unable to retrieve those values from this plugin. Is it possible and an adequate functionality for this plugin?

Thanks for reading and for this OSS work :)

unable to connect to Chrome --skip-autolaunch

After installing this plugin and configuring it in my webpack conf, when starting webpack, it will fail on the plugin with the following output

Unable to connect to Chrome
If you're using lighthouse with --skip-autolaunch, make sure you're running some other Chrome with a debugger.                                                                        
error Command failed with exit code 1.

What would be the proper way to fix this issue ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.