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

fix(legacy): preserve async generator function invocation #15021

Merged
merged 4 commits into from Nov 21, 2023

Conversation

lsdsjy
Copy link
Contributor

@lsdsjy lsdsjy commented Nov 17, 2023

Description

This PR is a continuation of #14968, whose trick works perfectly fine on older versions of vite but fails on vite@>4.4.0 because of the version bump of esbuild to 0.18.2. esbuild@0.18.2 introduces pure annotations for iife expressions, so the annotated async generator function will be dropped (by minifiers I think).

Additional context

A shorter way to preserve the generator function here is:

(async function* g(){import.meta.url;import("_").catch(()=>1)})();

I don't know if that is enough for syntax detecting since the body won't be executed until .next() is called.

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, especially the Pull Request Guidelines.
  • 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).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red
Copy link
Member

sapphi-red commented Nov 17, 2023

We can skip .next() as the legacy browsers doesn't actually execute detectModernBrowserDetector but throws an error when parsing the code. Maybe we should add a comment that described that detectModernBrowserDetector doesn't have to be executed.

Would you add a test for this? I think we can check this by running [/import\s*(/, /import.meta/, /async\s*function\*/].every(r => r.test(code)) for the entry point and the polyfill chunk.

@lsdsjy lsdsjy force-pushed the modern-detector branch 2 times, most recently from 7e5ec40 to d476721 Compare November 20, 2023 13:05
@lsdsjy
Copy link
Contributor Author

lsdsjy commented Nov 21, 2023

I've added a test for modern chunk legacy guard.
Besides, I find that some playground tests for the legacy plugin are not working because of the incorrect match patterns. The modern chunks are like index-{hash}.js, which cannot match /index\./. I've updated them too.

@bluwy
Copy link
Member

bluwy commented Nov 21, 2023

#14968 (comment) notes that updating the CSP hashes is a breaking change. I'm not sure if we exactly cover that in semver, but the right thing to do seems to be cutting another major?

Unfortunately I didn't test that that PR doesn't work in esbuild's minifier either. I'm not really sure what to do at the moment, but thought pointing it out here.

@patak-dev patak-dev changed the title fix(legacy): preserve async generator function invocation fix(legacy)!: preserve async generator function invocation Nov 21, 2023
patak-dev
patak-dev previously approved these changes Nov 21, 2023
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.

I think we can merge this one and release vite-plugin-legacy v6 with it. Even if v5 was released only a week ago, it seems the proper thing to do now.

@sapphi-red
Copy link
Member

sapphi-red commented Nov 21, 2023

I don't think we should say changing the hash is a breaking change. Otherwise, we'll have to bump majors every time the inline script is changed. That will make it difficult to release a bug fix in minor/patches. I wonder if we should remove the actual value from the README and show the command to obtain the values instead and clarify the value might change in patches.

@sapphi-red
Copy link
Member

@lsdsjy Would you remove the .next() part? (#15021 (comment))

Besides, I find that some playground tests for the legacy plugin are not working because of the incorrect match patterns. The modern chunks are like index-{hash}.js, which cannot match /index\./. I've updated them too.

Good catch! Thanks!

@sapphi-red sapphi-red added plugin: legacy p3-minor-bug An edge case that only affects very specific usage (priority) labels Nov 21, 2023
@lsdsjy
Copy link
Contributor Author

lsdsjy commented Nov 21, 2023

@lsdsjy Would you remove the .next() part? (#15021 (comment))

@sapphi-red There're 2 ways to preserve the invocation of the async generator function:

  1. call .next() on the returned AsyncGenerator object;
  2. move the import.meta.url and import() statements into the async generator function (so that it's not an empty function anymore).

I'm taking the first approach here. I can switch to approach 2 but I'm not sure because I think the first one is clearer because these three parallel statements make it immediately apparent that they involve three language features(import.meta, dynamic import and async generator function).

@sapphi-red
Copy link
Member

Ah, my bad, I was misunderstanding it.

@patak-dev
Copy link
Member

I don't think we should say changing the hash is a breaking change. Otherwise, we'll have to bump majors every time the inline script is changed. That will make it difficult to release a bug fix in minor/patches. I wonder if we should remove the actual value from the README and show the command to obtain the values instead and clarify the value might change in patches.

It is technically a breaking change, as it will require a change to work for certain users. I think it isn't a problem that it is in the readme, but it isn't clear when users need to change these. I think it could be valid for us to say that we'll only release changes to these hashes in minors (if we want to avoid major releases), with a note in that section. Users with a strict policy could then set the range to the current minor and still get other patches.

@patak-dev patak-dev changed the title fix(legacy)!: preserve async generator function invocation fix(legacy): preserve async generator function invocation Nov 21, 2023
@patak-dev patak-dev merged commit 47551a6 into vitejs:main Nov 21, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants