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

perf: skip es-module-lexer if have no dynamic imports #12732

Merged
merged 2 commits into from
Apr 4, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Apr 4, 2023

Description

Do a quick regex check for dynamic imports before using es-module-lexer to parse for dynamic import vars.

Additional context

I'm testing with https://github.com/sapphi-red/performance-compare, but it doesn't yield much improvements because React's HMR code has this: cc @ArnaudBarre

import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
  RefreshRuntime.registerExportsForReactRefresh(__SOURCE__, currentExports);
  import.meta.hot.accept((nextExports) => {
    if (!nextExports) return;
    const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
    if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
  });
});

But for other cases, less files will be transformed with es-module-lexer now, which should make things faster.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added the performance Performance related enhancement label Apr 4, 2023
@stackblitz
Copy link

stackblitz bot commented Apr 4, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@ArnaudBarre
Copy link
Member

Sadly this is a required runtime check that is required to solve all the HMR edge cases for React.

This regex could be done by the React plugin before injecting the hmr code and if no match also inject something like // __vite_safe_skip_dynamic_import

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Nice! It is incredible we still have these unguarded operations to optimize.

@patak-dev patak-dev merged commit 5d07d7c into main Apr 4, 2023
@patak-dev patak-dev deleted the perf-dynamic-import branch April 4, 2023 09:22
@bluwy
Copy link
Member Author

bluwy commented Apr 4, 2023

Sadly this is a required runtime check that is required to solve all the HMR edge cases for React.

This regex could be done by the React plugin before injecting the hmr code and if no match also inject something like // __vite_safe_skip_dynamic_import

That could work. Or maybe the react plugin could use a virtual module to expose the dynamic import too 🤔

import { __hmr_import } from 'virtual:react-hmr-utils'

__hmr_import(import.meta.url).then((currentExports) => {
// ...

I think either ways it would be nice to take advantage of the optimization.

@ArnaudBarre
Copy link
Member

Yeah I can even put this small wrapper in the RefreshRuntime modules. I will try it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants