-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add gulp recipe (wip) #1886
add gulp recipe (wip) #1886
Conversation
Just realize that, before, chrome launcher has to be started. It's running in cli stuff. Is it worth to have recipes and stuff here or just in scope of |
I'd be interested to understand the scope of interest in build-tool integration docs :) I need to rev https://github.com/addyosmani/webpack-lighthouse-plugin to latest if we want to reference elsewhere in docs. |
It's really nice to have good recipes/build-tool integration docs for people who wanna implement Lighthouse for CI. For some of them it's critical to have implementation example... |
docs/recipes/gulp/gulpfile.js
Outdated
|
||
function handleOk() { | ||
connect.serverClose(); | ||
process.exit(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can add something here to check the score and pass/fail based on that? This will always exit cleanly if running LH succeeds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that all LH log will be shown in a console.
E.g. in pwmetrics
for pass/fail
we use smth like smokehouse
.
What would you suggest?
Maybe reviewer (if we talking about project where LH will be injected) will look at CI and say it has bad trace, but 😒 , who does that..., so I dunno...
But I think bringing smokehouse as user feature will be kinda overhead...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking from the perspective of a user that wants to "fail the build" if their LH score drops to something too low or if they want to use the score in some way. Maybe just add the results object param to handleOk(results)
and stick a // TODO: use lighthouse results
after connect.serverClose();
?
docs/recipes/gulp/gulpfile.js
Outdated
return connect.server({ | ||
root: '../public', | ||
livereload: true, | ||
port: port |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend to just use the key if the var name is the same:
livereload: true,
port
}
docs/recipes/gulp/gulpfile.js
Outdated
const connect = require('gulp-connect'); | ||
const lighthouse = require('../../../lighthouse-core'); | ||
const perfConfig = require('../../../lighthouse-core/config/perf.json'); | ||
const port = 8080; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const PORT
docs/recipes/gulp/gulpfile.js
Outdated
|
||
const gulp = require('gulp'); | ||
const connect = require('gulp-connect'); | ||
const lighthouse = require('../../../lighthouse-core'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the idea is that we have cut/paste examples, users of this example will write this:
const lighthouse = require('lighthouse');
const perfConfig = require('lighthouse/lighthouse-core/config/perf.json');
package.json
Outdated
@@ -34,7 +34,8 @@ | |||
"chrome": "node ./lighthouse-cli/manual-chrome-launcher.js", | |||
"fast": "npm run start -- --disable-device-emulation --disable-cpu-throttling --disable-network-throttling", | |||
"smokehouse": "node lighthouse-cli/test/smokehouse/smokehouse.js", | |||
"deploy-viewer": "cd lighthouse-viewer && gulp deploy" | |||
"deploy-viewer": "cd lighthouse-viewer && gulp deploy", | |||
"gulp-example": "gulp --gulpfile docs/recipes/gulp/gulpfile.js --cwd docs/recipes/gulp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we leave this as a documented example and not add it to core. Instead, you can create a directory for this recipe (docs/recipes/gulp-example
) and stick a new package.json in there. Having everything in a single directory will also be future proof. I suspect we'll have a bunch more recipes in the future. Having a folder for each would be good.
add pkg in recipes add install recipes script
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good changes. I ran the example and it works great. Just a few more things.
docs/recipes/gulp/gulpfile.js
Outdated
}; | ||
|
||
const launchChrome = function() { | ||
launcher = new (ChromeLauncher.ChromeLauncher || ChromeLauncher)(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to do this? If you const ChromeLauncher = require('lighthouse/lighthouse-cli/chrome-launcher'). ChromeLauncher;
, then you can just use directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno, it was just dumb copy/paste from https://github.com/paulirish/pwmetrics/blob/master/lib/index.js#L122
maybe @paulirish knows more...
docs/recipes/gulp/gulpfile.js
Outdated
return connect.server({ | ||
root: '../public', | ||
livereload: true, | ||
PORT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
port: PORT
docs/recipes/gulp/gulpfile.js
Outdated
const runLighthouse = function() { | ||
const url = `http://localhost:${PORT}/index.html`; | ||
const ligthouseOptions = {}; // available options - https://github.com/GoogleChrome/lighthouse/#cli-options | ||
lighthouse(url, ligthouseOptions, perfConfig) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return lighthouse(url, ligthouseOptions, perfConfig)
docs/recipes/gulp/gulpfile.js
Outdated
}; | ||
|
||
gulp.task('lighthouse', function() { | ||
Promise.resolve(launchChrome()).then(_ => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for Promise.resolve()
. launchChrome()
returns a promise.
launchChrome().then(_ => {
connectServer();
return runLighthouse();
});
docs/recipes/package.json
Outdated
@@ -0,0 +1,12 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still prefer if we put everything in docs/recipes/gulp/
so this example is standalone. When we have more recipes in the future, it'll be less confusing on what npm dependencies, scripts, etc. are required for each.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree 👍 Just misunderstood your point
docs/recipes/package.json
Outdated
"devDependencies": { | ||
"gulp": "^3.9.1", | ||
"gulp-connect": "^5.0.0", | ||
"lighthouse": "^1.6.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe less restrictive on the LH version. ^1.x
might be ok, so users get the latest lighthouse when they try this demo.
package.json
Outdated
@@ -46,6 +47,7 @@ | |||
"google-closure-compiler": "^20161201.0.0", | |||
"gulp": "^3.9.1", | |||
"gulp-concat": "^2.6.1", | |||
"gulp-connect": "^5.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
package.json
Outdated
"install-cli": "cd ./lighthouse-cli && npm install", | ||
"install-extension": "cd ./lighthouse-extension && npm install", | ||
"install-viewer": "cd ./lighthouse-viewer && npm install", | ||
"install-recipes": "cd ./docs/recipes && npm install", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we don't need to necessarily make recipes runnable from the main package. I see them as more as documentation examples that users can copy/paste or run themselves. Installing these extras will also slow down our good buddy Travis.
docs/recipes/gulp/gulpfile.js
Outdated
* Handle ok result | ||
* @param {Object} results - Lighthouse results | ||
*/ | ||
const handleOk = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function(results) {
docs/recipes/gulp/gulpfile.js
Outdated
*/ | ||
const handleOk = function() { | ||
disconnectServer(); | ||
// TODO: use lighthouse results for checking your performance expectations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a console.log(results);
so users see something when they run the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! excited to get a set of recipes together
@@ -0,0 +1,79 @@ | |||
'use strict'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a license header
docs/recipes/gulp/gulpfile.js
Outdated
/** | ||
* Connect to server | ||
*/ | ||
const connectServer = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe startServer
and stopServer
instead of connectServer
and disconnectServer
?
docs/recipes/gulp/gulpfile.js
Outdated
const lighthouseOptions = {}; // available options - https://github.com/GoogleChrome/lighthouse/#cli-options | ||
return lighthouse(url, lighthouseOptions, perfConfig) | ||
.then(handleOk) | ||
.catch(handleError); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also a nit: should .then(handleOk).catch(handleError);
be moved into the gulp.task('lighthouse')
block? So it would be
gulp.task('lighthouse', function() {
launchChrome().then(_ => {
connectServer();
return runLighthouse()
.then(handleOk);
}).catch(handleError);
});
or something? Seems to make more sense there than in runLighthouse
@@ -0,0 +1,17 @@ | |||
<!DOCTYPE html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need license. Can copy the html one from e.g. https://github.com/GoogleChrome/lighthouse/blob/e822cbea89634449d66d50243fba2adce094f1d0/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
docs/recipes/gulp/gulpfile.js
Outdated
@@ -1,5 +1,21 @@ | |||
'use strict'; | |||
|
|||
/** | |||
* Copyright 2016 Google Inc. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, 2017 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no bother :)
docs/recipes/gulp/public/index.html
Outdated
@@ -1,4 +1,19 @@ | |||
<!DOCTYPE html> | |||
<!-- | |||
* Copyright 2016 Google Inc. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM2. Thanks!
If you have (or have ideas for) more helpful recipes, please keep them coming :)
@ebidel @brendankenny thanks for grounded review and merging stuff.
Definitely 👍 |
Actual implementation is done, but I faced same issue
as on
pwmetrics
.Here some debug of this issue if someone is interested - paulirish/pwmetrics#63 (comment)
So, after solving this one it will be ready to merge.
P.S. Maybe @addyosmani will be interested to make webpack recipe :)