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
use import.meta in worker got error: document is not defined #4646
Comments
I think this issue relate with https://github.com/vitejs/vite/pull/2494/files#r598314757 in #2494. |
I think this is a problem / feature of rollup see https://www.rollupjs.org/guide/en/#resolveimportmeta
So we need to tell rollup to handle I will work on a solution on that for a plugin with workers that I wrote and post it here if I found something! |
Simple plugin where {
name: "worker-env",
resolveImportMeta(prop, ctx) {
if (prop !== "url") return null;
return `new URL('${ctx.chunkId}', location.origin + '${config.base[0] == '/' ? '' : '/'}${config.base}').href`;
},
} When this is added to the plugin-list here: This would fix this problem. |
This seems to have already been addressed in rollup. rollup/rollup#4186 |
@JarekToro Is this still a problem? That fix is in the current rollup version used by vite fixed... So if that was I thought was the problem this should be fixed now. |
Update:
More complicated exampleSo it seems to be a tangential problem. I created a stack blitz to demonstrate the issue. https://stackblitz.com/edit/vitejs-vite-yqbzxq?file=lib/assets/test.worker.81214889.js Seems that when reference Should check the stack blitz for real example but in short index.tsimport worker from './test.worker?worker';
console.log(new worker()); test.worker.tsimport './worker_import_script';
addEventListener(
'message',
function (e: any) {
console.log(e)
},
false
);
postMessage({ ready: true }); worker_import_script.ts// This right here adds document references when bundled into the worker.
var _scriptDir = import.meta.url; console.log(_scriptDir);
// This right here actually works fine
const wasmBinaryFile = new URL('test.wasm', import.meta.url).toString();
console.log(wasmBinaryFile); output(function(){"use strict";
var e=document.currentScript&&document.currentScript.src||new URL("test.worker.js",document.baseURI).href;
console.log(e);
// ...etc |
EDIT: Problem can be simplified further. All that is actually needed is this code in a worker. var _scriptDir = import.meta.url;
console.log(_scriptDir); As it gets converted to (function(){"use strict";var r=document.currentScript&&document.currentScript.src||new URL("test.worker.js",document.baseURI).href;console.log(r)})(); |
As long this is not fixed you can try https://github.com/mathe42/vite-plugin-worker |
Awesome thanks |
Still reproducible in Vite 2.9.0-beta.2 |
Describe the bug
use import.meta in worker, in dev mode is ok, but after build got the error
document doesn't exist in web worker
Reproduction
create worker in main.js
worker.js
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: