fix production usage with bundlers (fix #14, fix #15)#22
Conversation
50eee51 to
b8a70bf
Compare
|
also might be helpful for #10 |
|
for esm.sh, change // for esm.sh
new Worker(new URL("./worker.mjs", import.meta.url), { type: "module"}) |
|
does this work in nextjs with turbopack? |
|
Seems turbopack will, or already supports this. And I can't see any message indicating web workers not supported in turbopack docs so i guess it's already fixed |
This actully breaks bundler (mjs file doesn't exist) |
|
just check if the if (new URL(import.meta.url).hostname === "esm.sh") {
return new Worker(new URL("./worker.mjs", import.meta.url), { type: "module"})
}
return new Worker(new URL("./worker.js", import.meta.url), { type: "module"}) |
|
No it's a compile time error, not runtime |
|
that |
if (new URL(import.meta.url).hostname === "esm.sh") {
const workerUrl = new URL("./worker.mjs", import.meta.url).href
return new Worker(workerUrl, { type: "module"})
}
does dynamic worker url work? |
|
bundlers also handle src = new URL("./icon.txt", import.meta.url)
req = await fetch(src)
content = await req.text() |
|
Guess it will work now |
|
@undefined-moe i just released the changes, now neither dev nor prod build works in vite😭 |
|
modern-monaco/src/lsp/css/setup.ts Lines 48 to 55 in 6996826 No you can't do this, it must be |
|
anyway, i will fix it in #27 |
|
Shhhh! Any idea for fixing the cross-origin worker from CDN? |
|
Not very sure. What's blocking it from loading from another origin? |
|
i removed the cross-origin condition,
|
|
just need to add it to optimizeDeps.exclude in vite.config.ts and everything should work |
|
ideally we don't need the extra |
|
Vite doesn't bundle inputs when starting dev server. Instead, it transforms code once requested (pretty similar to what esm.sh does), using module support built into the browser (and that's why it's dev server is super fast compared to other ones), which works pretty well under most scenarios. And that inconsistency is the issue so they are planning to switch to full bundle mode in the future https://vite.dev/guide/rolldown.html#why-introducing-a-full-bundle-mode |




Using the original way, the resource was imported into whatever resource bundle, meaning that the url contains not only the requested worker, but lots of other modules (vite dev mode don't perform a bundle action so it works like magic in dev but broke in production).
Need to use a way to tell bundlers that it needs to be a seperate chunk and it runs in a worker environment (otherwise some bundlers might inject some dependency management stuff using non-exist
documentvariable)Luckily most bundlers understand
new Worker(new URL(src, import.meta.url))syntax:Integration on esm.sh might need to be tested as I don't know how it processes those code