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(config): make module resolution async #13974

Merged
merged 8 commits into from
Jun 10, 2022
Merged

core(config): make module resolution async #13974

merged 8 commits into from
Jun 10, 2022

Conversation

connorjclark
Copy link
Collaborator

In preparation for converting lighthouse-core to esm (ref #12689), the config module resolution functions need to become async to accommodate dynamic imports. This PR made requireWrapper async, then made every calling function async.

@connorjclark connorjclark requested a review from a team as a code owner May 6, 2022 21:14
@connorjclark connorjclark requested review from adamraine and removed request for a team May 6, 2022 21:14
* @param {LH.Config.Json=} configJSON
* @param {LH.Flags=} flags
*/
constructor(configJSON, flags) {
static async createConfigFromJson(configJSON, flags) {
Copy link
Collaborator Author

@connorjclark connorjclark May 6, 2022

Choose a reason for hiding this comment

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

Constructors can't be async, so this helper function exists now.

This comment was marked as spam.

@connorjclark connorjclark changed the title core(config): make async core(config): make module resolution async May 6, 2022
@@ -7,7 +7,7 @@
"declarationMap": true,

"target": "es2020",
"module": "es2020",
"module": "es2022",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oops, i used top level await in one place... let's get #13964 done first.


const config = new Config(defaultConfig);
const config = await Config.createConfigFromJson();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The defaultConfig is used by default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

image

@@ -83,10 +83,10 @@ async function legacyNavigation(url, flags = {}, configJSON, userConnection) {
* not present, the default config is used.
* @param {LH.Flags=} flags Optional settings for the Lighthouse run. If present,
* they will override any settings in the config.
* @return {Config}
* @return {Promise<Config>}
*/
function generateConfig(configJson, flags) {
Copy link
Member

Choose a reason for hiding this comment

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

TODO for me: Make this FR config and add a generateLegacyConfig or something.

lighthouse-core/test/config/config-helpers-test.js Outdated Show resolved Hide resolved
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.

Far reaching but much cleaner than I was fearing. Nice one @connorjclark!

* @param {LH.Config.Json=} configJSON
* @param {LH.Flags=} flags
*/
constructor(configJSON, flags) {
static async fromJson(configJSON, flags) {
Copy link
Member

Choose a reason for hiding this comment

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

bikeshed: we call the type Config.Json but it's not necessarily json, and ideally the function name won't mislead. I don't have anything in particular I like instead...initialize() to piggyback on the existing FR name? create()? generate()? Plain old from()?

Copy link
Member

Choose a reason for hiding this comment

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

I like initialize() but this code is deprecated now so 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The static function isn't deprecated, just the constructor. Outside of our tests there really isn't a reason you'd want to use the constructor (with this PR).

.fromX is a pattern that I know is common in dart (which supports multiple constructors), I wonder if that's also a pattern in other languages. so I'm leaning towards from. If we had unlimited characters fromUnresolved would be nice. but just from is ok too (and I'll add a note about the resolving bit in the comment)

lighthouse-core/config/config.js Show resolved Hide resolved
* @deprecated `Config.fromJson` should be used instead.
* @constructor
* @param {LH.Config.Json} configJSON
* @param {{settings: LH.Config.Settings, passes: ?LH.Config.Pass[], audits: ?LH.Config.AuditDefn[]}} opts
Copy link
Member

Choose a reason for hiding this comment

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

wow, ancient typing style

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.

5 participants