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

refactor(plugins): switched from require to await import() when loading plugins #2558

Merged
merged 6 commits into from Sep 17, 2022

Conversation

travi
Copy link
Member

@travi travi commented Sep 12, 2022

to isolate the conversion ahead of #2543

…oading plugins

to isolate the conversion ahead of #2543
@travi travi requested a review from gr2m September 12, 2022 04:44
@travi
Copy link
Member Author

travi commented Sep 12, 2022

this obviously still needs clean up, but hoping to get some feedback. i'm not 100% i have my head around all that needs to change here, but assuming we change from require to await import(), the majority of the work should be handling the additional promises that are introduced. i think i have most of that handled, but may have missed some.

however, the part that i did not anticipate is that the result does not appear to satisfy _.isPlainObject() the same way that loading with require did. it does not appear to be an issue with the added default property either.

logging one of the example plugins from the tests shows the following, which i would expect to both satisfy _.isPlainObject (at least because of the null prototype) and satisfy the expected shape if it were to make it far enough through the logic

[Module: null prototype] {
  analyzeCommits: [AsyncFunction: analyzeCommits],
  default: { analyzeCommits: [AsyncFunction: analyzeCommits] }
}

but it appears that the loaded module has some detail that i havent yet figured out that prevents it from satisfying _.isPlainObject()

@travi
Copy link
Member Author

travi commented Sep 12, 2022

this also got me thinking about how this situation of loading plugins potentially gets more complicated as we progress further. currently, only cjs plugins are expected. once we move to esm, we will want to support loading plugins that are also esm. loading with await import() likely normalizes that situation for us, but we will want to confirm it once we are past this part.

this partially comes to mind because i did consider if it could be worth considering using module.createRequire to avoid all of the complexity, but that would prevent supporting esm plugins. i dont think that even makes sense as an intermediate step since we will want to convert official plugins to esm as soon as converting core to esm unblocks converting the plugins.

Copy link
Member

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

changes look good to me thus far

Comment on lines 20 to 22
console.log({plugins: options.plugins})
console.dir(plugin, {depth: null})
console.log(isPlainObject(plugin))
Copy link
Member

Choose a reason for hiding this comment

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

just a reminder to remove the debug logs when the PR is ready :)

Copy link
Member Author

Choose a reason for hiding this comment

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

yep 👍🏼

const [name, config] = parseConfig(plugin);
plugin = isString(name) ? loadPlugin(context, name, pluginsPath) : name;
? await castArray(options.plugins).reduce(async (plugins, plugin) => {
const plugins2 = await plugins;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there is a better name for plugins2? Maybe normalizedPlugins or awaitedPlugins?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, this is definitely part of the clean up that is necessary once this is working :)

@travi travi marked this pull request as draft September 13, 2022 14:43
@travi
Copy link
Member Author

travi commented Sep 16, 2022

i was able to resolve the previous issues by fixing the paren wrapping that i broke (this was the main problem) and using the .default property of the imported module (which i had tried previously, but wasnt working fully because of the former problem

@travi
Copy link
Member Author

travi commented Sep 16, 2022

however, xo now reports a lint issue for the dynamic import expression:

lib/plugins/utils.js:52:43
  ✖  52:43  import() expressions are not supported yet.  node/no-unsupported-features/es-syntax

however, all tests, including integration tests pass in our supported node range from my checking. all scenarios within the dynamic import expression seem to be covered by tests as well.

based on mysticatea/eslint-plugin-node#250, it sounds like the rule does not report correctly for this situation and is unlikely to be fixed soon. i hate to disable a rule like this and lose protection from other scenarios, but i'm unsure of a better approach for now. i'm open to suggestions

@travi
Copy link
Member Author

travi commented Sep 17, 2022

i went ahead and disabled the rule for that particular usage, since it seems like the best option, but open to alternative suggestions

@travi travi marked this pull request as ready for review September 17, 2022 04:12
@gr2m
Copy link
Member

gr2m commented Sep 17, 2022

i hate to disable a rule like this and lose protection from other scenarios, but i'm unsure of a better approach for now. i'm open to suggestions

+1 for disabling. We can create a follow up issue to review once we concluded the ESM transition?

Great work @travi 👏🏼

@gr2m gr2m merged commit 4cd3641 into master Sep 17, 2022
@gr2m gr2m deleted the async-import branch September 17, 2022 21:10
adityahex27 pushed a commit to hextrust/semantic-release that referenced this pull request Oct 31, 2022
@github-actions
Copy link

🎉 This PR is included in version 20.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

🎉 This PR is included in version 20.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

None yet

2 participants