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

core: support local plugins from global Lighthouse #11696

Merged
merged 7 commits into from
Dec 11, 2020

Conversation

koh110
Copy link
Contributor

@koh110 koh110 commented Nov 22, 2020

Summary

Lighthouse CLI will be able to loading plugins in node_modules/.

bugfix

Lighthouse CLI installed in global failed loading plugins in current working directory.

$ lighthouse https://www.google.com --config-path=./config.js
Runtime error encountered: Unable to locate plugin: `lighthouse-plugin-field-performance`.
     Tried to require() from these locations:
     ...
Error: Unable to locate plugin: `lighthouse-plugin-field-performance`.
     Tried to require() from these locations:

$ which lighthouse
/usr/local/bin/lighthouse
$ cat package.json
{
  "private": true,
  "dependencies": {
    "lighthouse-plugin-field-performance": "^2.2.1"
  }
}

Related Issues/PRs

Ref: #11671

@koh110 koh110 requested a review from a team as a code owner November 22, 2020 16:02
@koh110 koh110 requested review from adamraine and removed request for a team November 22, 2020 16:02
@google-cla google-cla bot added the cla: yes label Nov 22, 2020
@connorjclark connorjclark changed the title core(config-helper): load plugins in node_modules/ core(config): load plugins in node_modules/ Nov 23, 2020
// |-------------------------------------------|---------------------------------------|
// | absolute paths | 1. |
// | relative to the current working directory | 3. |
// | relative to the config.js file | 4. |
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for these table breakdowns!

@connorjclark connorjclark changed the title core(config): load plugins in node_modules/ core(config): load plugins from node_modules/ Nov 23, 2020
.gitignore Outdated
Comment on lines 35 to 36
!lighthouse-core/test/fixtures/config-helpers/config/node_modules
!lighthouse-core/test/fixtures/config-helpers/node_modules
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
!lighthouse-core/test/fixtures/config-helpers/config/node_modules
!lighthouse-core/test/fixtures/config-helpers/node_modules
!lighthouse-core/test/fixtures/config-helpers/**/*/node_modules

Copy link
Contributor Author

@koh110 koh110 Nov 24, 2020

Choose a reason for hiding this comment

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

Thanks for your suggestion!
When I tried, the rule seems to ignore lighthouse-core/test/fixtures/config-helpers/node_modules.

-!lighthouse-core/test/fixtures/config-helpers/config/node_modules
-!lighthouse-core/test/fixtures/config-helpers/node_modules
+!lighthouse-core/test/fixtures/config/**/*/node_modules
deleted:    lighthouse-core/test/fixtures/config-helpers/node_modules/plugin-in-working-directory/package.json
deleted:    lighthouse-core/test/fixtures/config-helpers/node_modules/plugin-in-working-directory/plugin-in-working-directory.js
renamed:    lighthouse-core/test/fixtures/config-helpers/config/config.js -> lighthouse-core/test/fixtures/config/config/config.js
renamed:    lighthouse-core/test/fixtures/config-helpers/config/node_modules/plugin-in-config-directory/package.json -> lighthouse-core/test/fixtures/config/config/node_modules/plugin-in-config-directory/package.json
renamed:    lighthouse-core/test/fixtures/config-helpers/config/node_modules/plugin-in-config-directory/plugin-in-config-directory.js -> lighthouse-core/test/fixtures/config/config/node_modules/plugin-in-config-directory/plugin-in-config-directory.js
renamed:    lighthouse-core/test/fixtures/config-helpers/lighthouse-plugin-config-helper/package.json -> lighthouse-core/test/fixtures/config/lighthouse-plugin-config-helper/package.json
renamed:    lighthouse-core/test/fixtures/config-helpers/lighthouse-plugin-config-helper/plugin-config-helper.js -> lighthouse-core/test/fixtures/config/lighthouse-plugin-config-helper/plugin-config-helper.js

Am I doing something wrong?

Copy link
Collaborator

@connorjclark connorjclark Nov 24, 2020

Choose a reason for hiding this comment

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

oh, oops, should just be !lighthouse-core/test/fixtures/config/**/node_modules

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aah! It worked perfectly! Thanks a lot!

@@ -0,0 +1,5 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you rename this folder to just config (instead of config-helpers)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I'll do it!

// |-- package.json
it('relative to the config path', () => {
const pluginName = 'plugin-in-config-directory';
const pluginDir = path.join(pluginsDirectory, 'config', 'node_modules', 'plugin-in-config-directory');
Copy link
Collaborator

@connorjclark connorjclark Nov 24, 2020

Choose a reason for hiding this comment

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

You can just do

`${pluginsDirectory}/config/node_modules/plugin-in-config-directory`

const configFixturePath = path.resolve(__dirname, '../fixtures/config');

beforeEach(() => {
process.cwd = jest.fn(() => configFixturePath);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does process.cwd need to be restored after?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so. The idea was to provide a default cwd so tests which set cwd don't affect other tests.

@koh110 I think this beforeEach should apply to every test not just this block.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah I missed L14, that will restore process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adamraine I agree with your comment. I will move up beforeEach.

Copy link
Member

@adamraine adamraine left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

some suggestions to maybe make the comments a little clearer, feel free to take or leave any of them :)

@@ -0,0 +1,5 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

did these files accidentally get double nested in a config directory? lighthouse-core/test/fixtures/config/config/...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brendankenny
No. it is not accident.
It was named lighthouse-core/test/fixtures/config-helpers/config at first.
I was suggested renaming this folder.
#11696 (comment)

Do you think I should rename to another name?

@@ -176,6 +176,21 @@ function requireAudits(audits, configDir) {
* @throws {Error}
*/
function resolveModule(moduleIdentifier, configDir, category) {
// node_modules/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// node_modules/
// module in a node_modules/ that is...

// node_modules/
// | | Lighthouse globally installed | Lighthouse locally installed |
// |--------------------------------|-------------------------------|------------------------------|
// | in global | 1. | 1. |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// | in global | 1. | 1. |
// | global | 1. | 1. |

// | | Lighthouse globally installed | Lighthouse locally installed |
// |--------------------------------|-------------------------------|------------------------------|
// | in global | 1. | 1. |
// | current working directory | 2. | 1. |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// | current working directory | 2. | 1. |
// | in current working directory | 2. | 1. |

// | current working directory | 2. | 1. |
// | relative to config.js file | 5. | - |

// path to file
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// path to file
// module given by a path that is...

// path to file
// | | Lighthouse globally/locally installed |
// |-------------------------------------------|---------------------------------------|
// | absolute paths | 1. |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// | absolute paths | 1. |
// | absolute | 1. |

@@ -184,6 +199,18 @@ function resolveModule(moduleIdentifier, configDir, category) {
return require.resolve(moduleIdentifier);
} catch (e) {}

// 2.
// Lighthouse globally installed, node_modules/ in current working directoriy.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lighthouse globally installed, node_modules/ in current working directoriy.
// Lighthouse globally installed, node_modules/ in current working directory.

// relative path passed to `require()` is found relative to the file it's
// in, this allows module paths to be specified relative to the config file.
const relativePath = path.resolve(configDir, moduleIdentifier);
try {
return require.resolve(relativePath);
} catch (requireError) {}

// 5.
// Lighthouse globally installed, node_modules/ in config directoriy.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lighthouse globally installed, node_modules/ in config directoriy.
// Lighthouse globally installed, node_modules/ in config directory.


it('relative to the config path', () => {
const pluginName = 'lighthouse-plugin-config-helper';
const pathToPlugin = resolveModule(pluginName, configFixturePath, 'plugin');
Copy link
Member

Choose a reason for hiding this comment

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

this one isn't testing the relative-to-the-configDir case because the default process.cwd set in beforeEach is also that same path. Need to set process.cwd to something else to test it (I assume that's what the '/usr/bin/node' below is doing for the other "config path" test)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I move up process.cwd to upper directory.

@koh110
Copy link
Contributor Author

koh110 commented Dec 10, 2020

@brendankenny I've modified the code, can you check it for me?

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

sorry for the delay! I'll land the suggestion and then LGTM!

@brendankenny brendankenny changed the title core(config): load plugins from node_modules/ core: support local plugins from global Lighthouse Dec 11, 2020
@devtools-bot devtools-bot merged commit 70106be into GoogleChrome:master Dec 11, 2020
@brendankenny
Copy link
Member

Thanks @koh110! A great fix for what was a big hole in the plugin system.

@koh110 koh110 deleted the plugin branch December 12, 2020 15:13
@koh110
Copy link
Contributor Author

koh110 commented Dec 12, 2020

Oops, I forget to fix. I appreciate it! Thanks @brendankenny
#11696 (comment)

@paulirish
Copy link
Member

Incredible work, @koh110! We really appreciate it. :D

@koh110
Copy link
Contributor Author

koh110 commented Dec 14, 2020

@paulirish Lighthouse has helped me with both my work and my hobby of code.
So I'm happy if my contribution can help you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants