Skip to content
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

cli: add --plugins flag to load from the command line #7407

Merged
merged 5 commits into from
Mar 7, 2019

Conversation

brendankenny
Copy link
Member

@brendankenny brendankenny commented Mar 7, 2019

Should make e.g. the recipe a lot clearer since you don't need a config file.

Old:

{
  extends: 'lighthouse:default',
  plugins: ['lighthouse-plugin-preload-as'],
}

lighthouse --config-path=my-config.json ...

New:
lighthouse --plugins=lighthouse-plugin-preload-as ...


Two things:

  • --plugins or --plugin? --plugin seems like it might be more natural (--plugin=lighthouse-plugin-preload-as) and it's what e.g. eslint does, but technically you can have multiple values (--plugin=plugin1 plugin2), plugins is the property name in the config, and we use the plural for other possibly single things, e.g. --only-categories.
  • Since plugins isn't a setting (it's a top level field in the config), it can't go through our normal flags/settings pipeline and instead has to be yet another optional argument to run.runLighthouse() and the lighthouse() module interface. This is ugly enough that I almost don't want to do it, but we've already talked about switching to an options object for v5, so maybe it's fine for now? (see below)

const getFlags = require('../../cli-flags').getFlags;

describe('CLI run', function() {
it('runLighthouse completes a LH round trip', () => {
const url = 'chrome://version';
describe('LH round trip', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split this up as it seems like there are a few different things being tested (and channel is about to be another check added to this)


it('returns results that match the saved results', () => {
const {lhr} = passedResults;
assert.equal(fileResults.audits.viewport.rawValue, false);

// passed results match saved results
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why this is being checked this way, but going with it :)

assert.strictEqual(groupIds[groupIds.length - 1], 'lighthouse-plugin-simple-new-group');
});

it('should append and use plugin-prefixed groups', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jburger424 I split the should append audits and a group in two because I was having trouble following it all in one :) Should be exactly the same functionality

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this is cleaner

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since plugins isn't a setting (it's a top level field in the config), it can't go through our normal flags/settings pipeline and instead has to be yet another optional argument to run.runLighthouse() and the lighthouse() module interface.

I think we should avoid adding plumbing through lighthouse index/runLighthouse. AFAICT, plugins are purely a config concern that don't have any other impacts to core, so it's certainly possible to contain the impact to CLI and config.

What if instead, CLI could call into Config.mergePlugins if the plugins CLI flag was set? It's already doing the work to figure out configJSON based on a set of flags that affect the config, so adding one more conditional to check for plugins seems like a small price to pay for nuking all the other complexity to our entrypoints that would be introduced otherwise.

WDYT?

@brendankenny
Copy link
Member Author

What if instead, CLI could call into Config.mergePlugins if the plugins CLI flag was set? It's already doing the work to figure out configJSON based on a set of flags that affect the config, so adding one more conditional to check for plugins seems like a small price to pay for nuking all the other complexity to our entrypoints that would be introduced otherwise.

We could do this now (generateConfig is already exposed for --print-config, and might be a nice spot for this?), though the other entry points are going to have to do something like this too (joining categoryIds as an option passed in) to support plugins, so is it better to just centralize now?

@brendankenny
Copy link
Member Author

I think we'll want node module users to be able to use this too, but that just made me realize I made this way too complicated (I think).

We don't want it in settings, but it can still be on flags, which is passed into the config constructor. I think this can be dealt with entirely within config.js.

@patrickhulce
Copy link
Collaborator

We don't want it in settings, but it can still be on flags, which is passed into the config constructor. I think this can be dealt with entirely within config.js.

Perfect 👍

@brendankenny
Copy link
Member Author

ah, so much better


fs.unlinkSync(filename);
it('merged the plugin into the config', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is well beyond just stretching the "unit" part of "unit tests", but this check has trivial overhead and until we have a smoketest for plugins I'd like to leave this in

@@ -19,42 +19,73 @@ const fastConfig = {
},
};

// Map plugin name to fixture since not actually installed in node_modules/.
jest.mock('lighthouse-plugin-simple', () => {
return require('../../../lighthouse-core/test/fixtures/config-plugins/lighthouse-plugin-simple');
Copy link
Collaborator

@patrickhulce patrickhulce Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.js of the real file?

or we are making it work like a package so it's ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we are making it work like a package so it's ok?

I was making it like a package (I added a / to make it clear it's a directory), but can also make it the js file if you'd rather

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, it's cool

},
};
const baseFlags = {configPath: configFixturePath};
const simplePlugin = 'lighthouse-plugin-simple';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we go with PluginName here? just easier to remember down below it's a string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

* @param {string=} configDir
* @return {LH.Config.Json}
*/
static mergePlugins(configJSON, configDir) {
const pluginNames = configJSON.plugins;
static mergePlugins(configJSON, flags, configDir) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are like the only meaningful changes, right?

so clean ✨ 😃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are like the only meaningful changes, right?

yep, that's it! soooo much better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, I also just realized this was the original plan all along, so at least we got back to it :)

so it seems ok to add this to the handful of special-cased flag/config mergings in config.js when we add support for the CLI flag.

#6959 (comment)

@brendankenny brendankenny merged commit a3b9cf3 into master Mar 7, 2019
@brendankenny brendankenny deleted the cliplugins branch March 7, 2019 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants