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(worker): prevent load vite client #12995

Closed
wants to merge 4 commits into from
Closed

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Apr 25, 2023

Description

fix #12970

Make sure __vite__injectQuery util does not bring in /@vite/client in worker file context. This PR makes __vite__injectQuery it's own virtual module to prevent it.

I also reverted the worker guards introduce in #9064 (Preserve for now to not break things as they could still be loaded by HMR code)

Additional context


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 p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 25, 2023
@stackblitz
Copy link

stackblitz bot commented Apr 25, 2023

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

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci

This comment was marked as outdated.


load(id) {
if (id === importAnalysisHelperId) {
return 'export ' + __vite__injectQuery.toString()
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this is genius or if this language has way to much meta programming 😄

@ArnaudBarre
Copy link
Member

For info this is fixing only some of the cases. If a plugin adds HMR code to a file imported by a worker, this will still load the client

@patak-dev
Copy link
Member

/ecosystem-ci run nuxt

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Apr 25, 2023

📝 Ran ecosystem CI: Open

suite result
nuxt ❌ failure

@patak-dev
Copy link
Member

/ecosystem-ci run nuxt

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Apr 25, 2023

📝 Ran ecosystem CI: Open

suite result
nuxt ❌ failure

@patak-dev
Copy link
Member

It seems the Nuxt issue is caused by this PR, it is working fine against main: https://github.com/vitejs/vite-ecosystem-ci/actions/runs/4797522268/jobs/8534613140

@bluwy maybe this change is a bit big for a patch. We could merge #12988 to avoid the regression and then queue this PR for Vite 4.4 (and maybe we start it soon)

@bluwy
Copy link
Member Author

bluwy commented Apr 26, 2023

Arnaud might be right that it's causing the failure in Nuxt. I think I'll revert that for now and preserve the extra guards.

bluwy and others added 2 commits April 26, 2023 15:00
Co-authored-by: patak <matias.capeletto@gmail.com>
@bluwy
Copy link
Member Author

bluwy commented Apr 26, 2023

/ecosystem-ci run nuxt

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Apr 26, 2023

📝 Ran ecosystem CI: Open

suite result
nuxt ❌ failure

@bluwy
Copy link
Member Author

bluwy commented Apr 26, 2023

Didn't notice the fail. I'm not really sure what's going on, but I'm fine merging #12988 first then revisiting this again.

@@ -734,7 +768,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {

if (needQueryInjectHelper) {
str().prepend(
`import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`,
`import { __vite__injectQuery } from "${wrapppedImportAnalysisHelperId}";`,
Copy link
Contributor

Choose a reason for hiding this comment

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

(As mentioned in issue) Inlining the function could help solve #11266. If we don't want to inline the function for all files – we could detect classic workers via the import suffixes.

e.g.

const importSuffixes = new URL(importer, 'http://vitejs.dev').searchParams
const isClassicWorker = importSuffixes.has('worker_type') && importSuffixes.has('type', 'classic')

if (isClassicWorker) {
   // Inlines the injectQuery function to the top of the worker file
   str().prepend(__vite__injectQuery.toString())
} else {
   // prepend import string
}

@bluwy
Copy link
Member Author

bluwy commented Nov 9, 2023

Going to close this in favour of #14918. It will fix #11266 instead.

For #12970, I think I've come around that /@vite/client in workers could be something we can continue to pursue, especially if we were to support HMR in workers one day.

@bluwy bluwy closed this Nov 9, 2023
@bluwy bluwy deleted the prevent-worker-client-code branch December 7, 2023 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

document is not defined when using xxxx.wasm?url or ?worker in vite 4.3.1
4 participants