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

"document" is not defined for xxxx.wasm?url #9879

Closed
7 tasks done
nine-fox opened this issue Aug 28, 2022 · 22 comments · Fixed by #12697
Closed
7 tasks done

"document" is not defined for xxxx.wasm?url #9879

nine-fox opened this issue Aug 28, 2022 · 22 comments · Fixed by #12697
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@nine-fox
Copy link

nine-fox commented Aug 28, 2022

Describe the bug

Hi there,

the following error is fired when I use latest version 3.0.9 to compile the following code in production mode:

import xxxx from "../../xxx.wasm?url";

The error is as follows.
"document" is not defined

So I switch back to old version 2.9.x and the error disappear

After some investigation, I found it is caused by the changeset:

The following code doesn't check document variable:

const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
getResolveUrl(
'${relativePath}', ${ umd ? typeof document === 'undefined' ? location.href : : '' }document.currentScript && document.currentScript.src || document.baseURI
)

So I suggest to add a simple "typeof document !== 'undefined'" in the last sentence.

Reproduction

https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js

run "npm run build" then "vite preview"

System Info

"document" is not defined

Used Package Manager

yarn

Logs

No response

Validations

@github-actions
Copy link

Hello @nine-fox. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@nine-fox
Copy link
Author

Hi, there, it is produced in https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js

@nine-fox
Copy link
Author

Hello @nine-fox. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js

@sapphi-red
Copy link
Member

No error happens with your reproduction.

@sapphi-red
Copy link
Member

It does not happpen with the following config, too.

import { defineConfig } from "vite"

export default defineConfig({
  base: '',
  build: {
    assetsInlineLimit: 0
  }
})

@nine-fox
Copy link
Author

No error happens with your reproduction.

Hi Thanks for your investigation. After run vite build and then vite preview, the error will be in console tab(developer tools). I am using the latest chrome.

My finding is that, document is unrecognized under worker thread. After I change generation from iife to es, the error disappear.

@sapphi-red
Copy link
Member

Yes, I did vite build and vite preview. It does not reproduce.

@nine-fox
Copy link
Author

Yes, I did vite build and vite preview. It does not reproduce.
vite-project.zip

Hi, just found SlackBlitz's source code is incorrect, that's not the latest version I did last night. Not sure why, I guess it is a network issue due to China Firewall.

So I did it again. See this attached file, please.

Sorry for the inconvenience.

@sapphi-red
Copy link
Member

Please provide it with a GitHub repository or StackBlitz.

@nine-fox
Copy link
Author

Please provide it with a GitHub repository or StackBlitz.

Would u like to try https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js again?thx

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: web workers and removed needs reproduction labels Aug 30, 2022
@sapphi-red
Copy link
Member

I was able to reproduce with that one.

@schickling
Copy link
Contributor

It does not happpen with the following config, too.

import { defineConfig } from "vite"

export default defineConfig({
  base: '',
  build: {
    assetsInlineLimit: 0
  }
})

While given this a try, I've noticed that base: '.' also works but base: './' doesn't. Hopefully this is helpful to someone. (Not sure though whether this is intentional behaviour or should be fixed as well.)

@mustafa0x
Copy link

I have a similar error, using vite, svelte, web worker (via comlink).

When I edit the svelte component (so it seems to be caused by HMR) I see the error below:

client.ts:288 Uncaught (in promise) ReferenceError: document is not defined
    at hasErrorOverlay (client.ts:288:3)
    at handleMessage (client.ts:150:28)
    at WebSocket.<anonymous> (client.ts:91:5)

Adding self.document = {querySelectorAll(){return []}} to the web worker silences the error.

@seanaye
Copy link

seanaye commented Nov 17, 2022

Are there any updates for this? The self.document = {querySelectorAll(){return []}} is put after the usage of document by the bundler so it still breaks for me. Inlining is also not an option

@seanaye
Copy link

seanaye commented Dec 2, 2022

for anyone looking best way ive found is create a shim.js which contains self.document = {querySelectorAll(){return []}}

Then in first line of worker, import "./shim.js"

@RSWilli
Copy link

RSWilli commented Jan 5, 2023

for me this wasn't enough, since the subsequent code needed document.baseURI to create an URL, so I added the following to my shim.mts:

// @ts-ignore
self.document = {
    baseURI: location.origin
}

tronical added a commit to slint-ui/slint that referenced this issue Feb 3, 2023
The update to vite 4 triggered the vite 3 bug vitejs/vite#9879
for us, where the wasm-pack generated .js is included as iife and
therefore the use of import.meta.url gets transpiled to some shim that
tries to access the document, assuming that the JS is running in the
browser instead of in a worker environment.

Explicitly set the output type for the worker to ES modules, so that no
changes are needed and import.meta.url remains in use. This matches the
worker invocation anyway, as we pass "type": "module" to the Worker
constructor.
tantaman added a commit to vlcn-io/live-examples that referenced this issue Mar 13, 2023
@seo-rii
Copy link

seo-rii commented Apr 17, 2023

I believe this bug is still happening on 4.3.0-beta.7. Is the patch released?

@patak-dev
Copy link
Member

@seo-rii your prev reproduction seems fine now https://stackblitz.com/edit/vitejs-vite-mmhfr5. Would you open a new issue with another minimal repro if you still see something wrong?

@seo-rii
Copy link

seo-rii commented Apr 17, 2023

Oh, I'm sorry. It was slightly diffrent kind of bug. To describe it, if you "import" something in web worker, some bug like this still happens. I'll submit another issue after making reproduction on stackblitz.

@subframe7536
Copy link

subframe7536 commented Apr 24, 2023

same issue in v4.3.1.

work fine when rollback to v4.2.1

issue: #12970

@patak-dev
Copy link
Member

Please create a new issue with a reproduction

@schickling
Copy link
Contributor

And please link the new issue here. I was facing the same original issue and I'm holding back on upgrading to Vite 4.3 until this situation is resolved.

@github-actions github-actions bot locked and limited conversation to collaborators May 9, 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.

9 participants