-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Comments
Hello @nine-fox. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
Hi, there, it is produced in https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js |
https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js |
No error happens with your reproduction. |
It does not happpen with the following config, too. import { defineConfig } from "vite"
export default defineConfig({
base: '',
build: {
assetsInlineLimit: 0
}
}) |
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. |
Yes, I did |
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. |
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 |
I was able to reproduce with that one. |
While given this a try, I've noticed that |
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:
Adding |
Are there any updates for this? The |
for anyone looking best way ive found is create a Then in first line of worker, |
for me this wasn't enough, since the subsequent code needed // @ts-ignore
self.document = {
baseURI: location.origin
} |
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.
I believe this bug is still happening on 4.3.0-beta.7. Is the patch released? |
@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? |
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. |
same issue in v4.3.1. work fine when rollback to v4.2.1 issue: #12970 |
Please create a new issue with a reproduction |
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. |
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:
import.meta.url
for relative assets if output is not ESM (fixes plugin-legacy Unexpected token import #9297) (fix: avoid usingimport.meta.url
for relative assets if output is not ESM (fixes #9297) #9381)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
The text was updated successfully, but these errors were encountered: