Skip to content

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.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

"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.

import defaultConfig from '../config/default-config.js';

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

* @return {Config}
* @return {Promise<Config>}
*/
function generateConfig(configJson, flags) {
Copy link
Contributor

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.

Copy link
Contributor

@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.Flags=} flags
*/
constructor(configJSON, flags) {
static async fromJson(configJSON, flags) {
Copy link
Contributor

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
Contributor

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)

* @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
Contributor

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