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: replace dynamic imports with static imports #5821

Merged
merged 4 commits into from
Jul 13, 2023

Conversation

hereje
Copy link
Contributor

@hereje hereje commented Jun 25, 2023

  • Replace await import('path-to-module') with static imports at the beginnunounoing of modules

🎉 Thanks for submitting a pull request! 🎉

Summary

Fixes #5697

Refactor dynamic imports with static imports on ESM (*.mjs) files

For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

- Replace await import('path-to-module') with static imports
at the beginning of modules
@hereje hereje requested a review from a team as a code owner June 25, 2023 02:37
Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

Looks generally good. Some imports were moved that mentioned performance reasons for having them as late-imports, we should try out if importing them statically makes a difference. @lukasholzer could you take a second look? You opened the original issue.

@@ -259,9 +261,6 @@ const detectFrameworkSettings = async ({ projectDir }) => {
}

if (frameworks.length > 1) {
// performance optimization, load inquirer on demand
Copy link
Contributor

Choose a reason for hiding this comment

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

if this was here as a performance optimisation, maybe it should stay as a dynamic import. @lukasholzer what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

TBH I would benchmark it and test it locally if it makes a difference. If not than we can move forward with a static one otherwise I would leave it a dynamic one.

@@ -30,9 +31,6 @@ const buildFunction = async ({ cache, config, directory, func, hasTypeModule, pr
}
const functionDirectory = path.dirname(func.mainFile)

// performance
Copy link
Contributor

Choose a reason for hiding this comment

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

this mentions performance as the reason we're late-importing it, so we should double-check wether we want to make it static.

@@ -175,9 +176,6 @@ export class FunctionsRegistry {
// This function is here so we can mock it in tests
// eslint-disable-next-line class-methods-use-this
async listFunctions(...args) {
// Performance optimization: load '@netlify/zip-it-and-ship-it' on demand.
const { listFunctions } = await import('@netlify/zip-it-and-ship-it')
Copy link
Contributor

Choose a reason for hiding this comment

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

same, mentions perf optimisation

@lukasholzer
Copy link
Collaborator

As previously stated I would do some benchmarks for every dynamic import and see how it influences the execution time.

@Skn0tt
Copy link
Contributor

Skn0tt commented Jul 13, 2023

I ran a very naïve comparison, measuring the time between starting bin/run.mjs, and this line:

const functionDirectory = path.dirname(func.mainFile)

I ran on netlify-react-ui, with BROWSER=none ~/dev/netlify/cli/bin/run.mjs dev --offline. This PR took 3.7s. When I make the imports dynamic, it's the very same. I'm not sure if my comparison is right :/

@github-actions
Copy link

github-actions bot commented Jul 13, 2023

📊 Benchmark results

Comparing with 99da9eb

  • Dependency count: 1,299 (no change)
  • Package size: 270 MB (no change)

Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

Performance doesn't seem impacted, let's merge it!

@Skn0tt
Copy link
Contributor

Skn0tt commented Jul 13, 2023

Thank you so much for the contribution @hereje! And please excuse the long idle time.

@Skn0tt Skn0tt added the automerge Add to Kodiak auto merge queue label Jul 13, 2023
@kodiakhq kodiakhq bot merged commit 47142bd into netlify:main Jul 13, 2023
@hereje hereje deleted the refactor/dynamic-imports-on-esm branch July 27, 2023 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor all dynamic imports of ESM dependencies to static imports
3 participants