-
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
Changes from 5 commits
a95661d
ebf7e3c
055372f
4b48002
267caed
be29b12
6d47db0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -176,6 +176,21 @@ function requireAudits(audits, configDir) { | |||||
* @throws {Error} | ||||||
*/ | ||||||
function resolveModule(moduleIdentifier, configDir, category) { | ||||||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | current working directory | 2. | 1. | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | relative to config.js file | 5. | - | | ||||||
|
||||||
// path to file | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | | Lighthouse globally/locally installed | | ||||||
// |-------------------------------------------|---------------------------------------| | ||||||
// | absolute paths | 1. | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | 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 commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for these table breakdowns! |
||||||
|
||||||
// 1. | ||||||
// First try straight `require()`. Unlikely to be specified relative to this | ||||||
// file, but adds support for Lighthouse modules from npm since | ||||||
// `require()` walks up parent directories looking inside any node_modules/ | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// ex: lighthouse https://test.com | ||||||
// | ||||||
// working directory/ | ||||||
// |-- node_modules/ | ||||||
// |-- package.json | ||||||
try { | ||||||
return require.resolve(moduleIdentifier, {paths: [process.cwd()]}); | ||||||
} catch (e) {} | ||||||
|
||||||
// 3. | ||||||
// See if the module resolves relative to the current working directory. | ||||||
// Most useful to handle the case of invoking Lighthouse as a module, since | ||||||
// then the config is an object and so has no path. | ||||||
|
@@ -202,14 +229,28 @@ function resolveModule(moduleIdentifier, configDir, category) { | |||||
throw new Error(errorString); | ||||||
} | ||||||
|
||||||
// Finally, try looking up relative to the config file path. Just like the | ||||||
// 4. | ||||||
// Try looking up relative to the config file path. Just like the | ||||||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// ex: lighthouse https://test.com --config-path=./config/config.js | ||||||
// | ||||||
// working directory/ | ||||||
// |-- config/ | ||||||
// |-- node_modules/ | ||||||
// |-- config.js | ||||||
// |-- package.json | ||||||
try { | ||||||
return require.resolve(moduleIdentifier, {paths: [relativePath]}); | ||||||
brendankenny marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} catch (requireError) {} | ||||||
|
||||||
throw new Error(errorString + ` | ||||||
${relativePath}`); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env jest */ | ||
|
||
const {resolveModule} = require('../../config/config-helpers.js'); | ||
const assert = require('assert').strict; | ||
const path = require('path'); | ||
|
||
jest.mock('process', () => ({ | ||
cwd: () => jest.fn(), | ||
})); | ||
|
||
describe('resolveModule', () => { | ||
const configFixturePath = path.resolve(__dirname, '../fixtures/config'); | ||
|
||
beforeEach(() => { | ||
process.cwd = jest.fn(() => configFixturePath); | ||
}); | ||
|
||
it('lighthouse and plugins are installed in the same path', () => { | ||
const pluginName = 'chrome-launcher'; | ||
const pathToPlugin = resolveModule(pluginName, null, 'plugin'); | ||
assert.equal(pathToPlugin, require.resolve(pluginName)); | ||
}); | ||
|
||
describe('plugin paths to a file', () => { | ||
it('relative to the current working directory', () => { | ||
const pluginName = 'lighthouse-plugin-config-helper'; | ||
const pathToPlugin = resolveModule(pluginName, null, 'plugin'); | ||
assert.equal(pathToPlugin, require.resolve(path.resolve(configFixturePath, pluginName))); | ||
}); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. this one isn't testing the relative-to-the- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I move up |
||
assert.equal(pathToPlugin, require.resolve(path.resolve(configFixturePath, pluginName))); | ||
}); | ||
}); | ||
|
||
describe('lighthouse and plugins are installed by npm', () => { | ||
const pluginsDirectory = path.resolve(__dirname, '../fixtures/config/'); | ||
|
||
// working directory/ | ||
// |-- node_modules/ | ||
// |-- package.json | ||
it('in current working directory', () => { | ||
const pluginName = 'plugin-in-working-directory'; | ||
const pluginDir = `${pluginsDirectory}/node_modules/plugin-in-working-directory`; | ||
process.cwd = jest.fn(() => pluginsDirectory); | ||
|
||
const pathToPlugin = resolveModule(pluginName, null, 'plugin'); | ||
|
||
assert.equal(pathToPlugin, require.resolve(pluginName, {paths: [pluginDir]})); | ||
}); | ||
|
||
// working directory/ | ||
// |-- config directory/ | ||
// |-- node_modules/ | ||
// |-- config.js | ||
// |-- package.json | ||
it('relative to the config path', () => { | ||
const pluginName = 'plugin-in-config-directory'; | ||
const configDirectory = `${pluginsDirectory}/config`; | ||
process.cwd = jest.fn(() => '/usr/bin/node'); | ||
|
||
const pathToPlugin = resolveModule(pluginName, configDirectory, 'plugin'); | ||
|
||
assert.equal(pathToPlugin, require.resolve(pluginName, {paths: [configDirectory]})); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
extends: 'lighthouse:default', | ||
plugins: ['plugin-in-config-directory'], | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "lighthouse-plugin-config", | ||
"private": true, | ||
"main": "./plugin-config-helper.js" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** @type {LH.Config.Plugin} */ | ||
module.exports = { | ||
groups: { | ||
'new-group': { | ||
title: 'New Group', | ||
}, | ||
}, | ||
audits: [ | ||
{path: 'redirects'}, | ||
{path: 'user-timings'}, | ||
], | ||
category: { | ||
title: 'Config', | ||
auditRefs: [ | ||
{id: 'redirects', weight: 1, group: 'new-group'}, | ||
], | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.