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

Vite fails to parse worker options when they are stored in a variable #18651

Closed
7 tasks done
mcmy opened this issue Nov 13, 2024 · 5 comments
Closed
7 tasks done

Vite fails to parse worker options when they are stored in a variable #18651

mcmy opened this issue Nov 13, 2024 · 5 comments

Comments

@mcmy
Copy link

mcmy commented Nov 13, 2024

Describe the bug

Title: Vite fails to parse worker options when they are stored in a variable

Description:

When using Vite with the following code, an error occurs during the bundling process:

var worker;
var workerOptions = {
  'type': 'module',
  'name': 'em-pthread',
};
worker = new Worker(new URL('wasm.js', import.meta.url), workerOptions);
PThread.unusedWorkers.push(worker);

The error message is:

[plugin:vite:worker-import-meta-url] Vite is unable to parse the worker options as the value is not static. To ignore this error, please use /* @vite-ignore */ in the worker options.
D:/ProGram/IClion/wasm/web/src/assets/wasm.js:1061:65
1059|          // the first case in their bundling step. The latter ends up producing an invalid
1060|          // URL to import from the server (e.g., for webpack the file:// path).
1061|          worker = new Worker(new URL('wasm.js', import.meta.url), workerOptions);
   |                                                                  ^
1062|        PThread.unusedWorkers.push(worker);
1063|        },

However, the following code does not throw an error:

var worker;
worker = new Worker(new URL('wasm.js', import.meta.url), {
  'type': 'module',
  'name': 'em-pthread',
});
PThread.unusedWorkers.push(worker);

The Emscripten community has also mentioned this bug but cannot fix it on their end, as it is a Vite issue. As a workaround, I have to manually add the /* @vite-ignore */ comment to the worker options like this:

worker = new Worker(new URL('wasm.js', import.meta.url), /* @vite-ignore */ workerOptions);

Reproduction

emscripten-core/emscripten#20452

Steps to reproduce

No response

System Info

Need to install the following packages:
[email protected]
Ok to proceed? (y)


  System:
    OS: Windows 11 10.0.26100
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
    Memory: 35.39 GB / 63.74 GB
  Binaries:
    Node: 20.9.0 - E:\APP\ScoopGlobal\apps\nodejs-lts\current\node.EXE
    Yarn: 1.22.22 - E:\APP\ScoopGlobal\apps\nodejs-lts\current\bin\yarn.CMD
    npm: 10.8.1 - E:\APP\ScoopGlobal\apps\nodejs-lts\current\bin\npm.CMD
    pnpm: 9.12.3 - E:\APP\ScoopGlobal\apps\nodejs-lts\current\bin\pnpm.CMD
  Browsers:
    Edge: Chromium (130.0.2849.56)
  npmPackages:
    @vitejs/plugin-react: ^4.3.3 => 4.3.3
    vite: ^5.4.10 => 5.4.11

Used Package Manager

npm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Nov 13, 2024

It looks like the fix is on going here emscripten-core/emscripten#22834

Also remembered we already had a discussion on Vite side in #18396

@mcmy
Copy link
Author

mcmy commented Nov 13, 2024

It looks like the fix is on going here emscripten-core/emscripten#22834

But I think vite should be able to parse this static variable, and it doesn't,And I didn't find a way to modify the configuration to ignore this error

@mcmy
Copy link
Author

mcmy commented Nov 13, 2024

This js is automatically generated, and I don't want to modify the source code every time it is generated

@hi-ogawa
Copy link
Collaborator

FYI, workaround is suggested in emscripten-core/emscripten#22394 (comment)

@mcmy
Copy link
Author

mcmy commented Nov 13, 2024

FYI, workaround is suggested in emscripten-core/emscripten#22394 (comment)

Thanks for your solution, I solved this problem with

https://gist.github.com/jamsinclair/6ad148d0590291077a4ce389c2b274ea

I initially wanted to write a plug-in replacement, but not familiar with the vite plug-in, the GPT code can not be used

@mcmy mcmy closed this as completed Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants