-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Conversation
...se-core/test/fixtures/config-helpers/lighthouse-plugin-config-helper/plugin-config-helper.js
Outdated
Show resolved
Hide resolved
// |-------------------------------------------|---------------------------------------| | ||
// | absolute paths | 1. | | ||
// | relative to the current working directory | 3. | | ||
// | relative to the config.js file | 4. | |
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.
Thanks for these table breakdowns!
.gitignore
Outdated
!lighthouse-core/test/fixtures/config-helpers/config/node_modules | ||
!lighthouse-core/test/fixtures/config-helpers/node_modules |
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.
!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 |
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.
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?
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.
oh, oops, should just be !lighthouse-core/test/fixtures/config/**/node_modules
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.
Aah! It worked perfectly! Thanks a lot!
@@ -0,0 +1,5 @@ | |||
{ |
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.
Can you rename this folder to just config
(instead of config-helpers
)?
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.
OK. I'll do it!
...thouse-core/test/fixtures/config/config/node_modules/plugin-in-config-directory/package.json
Outdated
Show resolved
Hide resolved
// |-- 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'); |
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.
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); |
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.
Does process.cwd
need to be restored after?
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 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.
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.
ah I missed L14, that will restore process
.
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.
@adamraine I agree with your comment. I will move up beforeEach
.
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.
LGTM
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.
some suggestions to maybe make the comments a little clearer, feel free to take or leave any of them :)
@@ -0,0 +1,5 @@ | |||
{ |
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.
did these files accidentally get double nested in a config directory? lighthouse-core/test/fixtures/config/config/...
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.
@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/ |
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.
// node_modules/ | |
// module in a node_modules/ that is... |
// node_modules/ | ||
// | | Lighthouse globally installed | Lighthouse locally installed | | ||
// |--------------------------------|-------------------------------|------------------------------| | ||
// | in global | 1. | 1. | |
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.
// | in global | 1. | 1. | | |
// | global | 1. | 1. | |
// | | Lighthouse globally installed | Lighthouse locally installed | | ||
// |--------------------------------|-------------------------------|------------------------------| | ||
// | in global | 1. | 1. | | ||
// | current working directory | 2. | 1. | |
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.
// | 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 |
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.
// path to file | |
// module given by a path that is... |
// path to file | ||
// | | Lighthouse globally/locally installed | | ||
// |-------------------------------------------|---------------------------------------| | ||
// | absolute paths | 1. | |
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.
// | 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. |
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.
// 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. |
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.
// 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'); |
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.
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)
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.
Thanks. I move up process.cwd
to upper directory.
@brendankenny I've modified the code, can you check it for me? |
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 for the delay! I'll land the suggestion and then LGTM!
Thanks @koh110! A great fix for what was a big hole in the plugin system. |
Oops, I forget to fix. I appreciate it! Thanks @brendankenny |
Incredible work, @koh110! We really appreciate it. :D |
@paulirish Lighthouse has helped me with both my work and my hobby of code. |
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.
Related Issues/PRs
Ref: #11671