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

Abnormal behaviors in building worker modules. #15310

Closed
7 tasks done
XiSenao opened this issue Dec 11, 2023 · 4 comments · Fixed by #15321
Closed
7 tasks done

Abnormal behaviors in building worker modules. #15310

XiSenao opened this issue Dec 11, 2023 · 4 comments · Fixed by #15321
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 11, 2023

Describe the bug

When handling worker modules, Vite uses Rollup for building, and the default target output is iife. However, an interesting issue #15305 was discovered when the target output is iife and there is an exception in handling the statement by Rollup.

// Before compiling worker.js 
const t = import.meta && import.meta.url && new URL(import.meta.url)

// After compiling worker.js
var e = typeof document < "u" ? document.currentScript : null;
const t = {
    url: e && e.src || new URL("assets/worker-mLIARZvR.js",document.baseURI).href
} && self.location.href && new URL(self.location.href);

There will definitely be an issue in worker modules because the document instance cannot be accessed within worker. It behaves correctly when the target output is es. This also creates a differentiation between the development stage and the build stage, so some additional handling may be required in this case.

Reproduction

https://stackblitz.com/edit/vitejs-vite-bryjvn?file=index.html

Steps to reproduce

pnpm install && pnpm build && pnpm preview

System Info

System:
    OS: macOS 14.1.2
    CPU: (8) arm64 Apple M1
    Memory: 48.72 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.12.0 - ~/.nvm/versions/node/v18.12.0/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.0/bin/npm
    pnpm: 8.11.0 - ~/.nvm/versions/node/v18.12.0/bin/pnpm
    bun: 1.0.14 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.71
    Safari: 17.1.2
  npmPackages:
    @vitejs/plugin-react-swc: ^3.5.0 => 3.5.0 
    vite: ^5.0.0 => 5.0.7

Used Package Manager

pnpm

Logs

No response

Validations

Copy link

stackblitz bot commented Dec 11, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Dec 11, 2023

It sounds similar to this closed issue and the corresponding PR:

It looks like that PR handled the exact expression import.meta.url but referencing import.meta directly might be still broken?

@XiSenao
Copy link
Collaborator Author

XiSenao commented Dec 12, 2023

Yeah, perhaps we need to do additional processing for import.meta.

@vishnukolla
Copy link

may be inconsistency in variable names . give it a try though and even import.meta.url needs to be processed furthur

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: web workers labels Dec 12, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants