-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Export fileToUrl
function for plugins
#7162
Comments
@Menci Can you show an example where this would be useful for external plugins? It would be helpful to justify exposing another API. |
yep, I want to see you example too. When you encounter picture resources in for this example maybe we also can use |
I'm working on a plugin bringing WebAssembly ESM integration to Vite, which is equivalent to Webpack's asyncWebAssembly. I need to emit the WASM file just like the internal |
Thanks for the explanation @Menci (and thanks for updating some old issues about your plugin!). I think it makes sense to export it looking at the plugin code. I'll bring the PR to the next team meeting for discussion. PS: I wonder if the ESM integration could be first-class in Vite one day, that would be cool. |
+1, having a similar case trying to add WASI support (via @wasmer/wasi) to the wasm loader. It needs to do some stuff before returning exports, so hacking around with the base loader is a no-go, and I'm only left with copy-pasting code from |
@bluwy 👀👀👀 |
This seems to still be in the team discussion list. We had briefly talked about it but need Evan's input on how to expose this. Will give an update again when we circle back again! |
Related discussion from @sapphi-red #7763 about the use case |
If it's in JS, I think @Menci For example for your plugin, I guess this part `
import __wasmUrl from "${id}?url"
import __vite__initWasm from "${wasmHelper.id}"
${imports
.map(
({ from, names }, i) =>
`import { ${names.map((name, j) => `${name} as __vite__wasmImport_${i}_${j}`).join(", ")} } from ${JSON.stringify(
from
)};`
)
.join("\n")}
const __vite__wasmModule = await __vite__initWasm({ ${imports
.map(
({ from, names }, i) =>
`${JSON.stringify(from)}: { ${names.map((name, j) => `${name}: __vite__wasmImport_${i}_${j}`).join(", ")} }`
)
.join(", ")} }, __wasmUrl});
${exports
.map(name => `export ${name === "default" ? "default" : `const ${name} =`} __vite__wasmModule.${name};`)
.join("\n")}
`; or `
import __vite__initWasm from "${id}?init"
${imports
.map(
({ from, names }, i) =>
`import { ${names.map((name, j) => `${name} as __vite__wasmImport_${i}_${j}`).join(", ")} } from ${JSON.stringify(
from
)};`
)
.join("\n")}
const __vite__wasmModule = await __vite__initWasm({ ${imports
.map(
({ from, names }, i) =>
`${JSON.stringify(from)}: { ${names.map((name, j) => `${name}: __vite__wasmImport_${i}_${j}`).join(", ")} }`
)
.join(", ")} });
${exports
.map(name => `export ${name === "default" ? "default" : `const ${name} =`} __vite__wasmModule.${name};`)
.join("\n")}
`; |
Let's move the discussion to: |
Clear and concise description of the problem
Vite's some internal plugins uses the internal helper function
fileToUrl
, which emits asset file to dev-server/bundle and return a URL for using in generated module code. It's also very useful for external plugins to emit asset files.Suggested solution
Export
fileToUrl
function from Vite and document it in the docs.Alternative
Plugin authors could copy the code from
fileToUrl
from their plugins. This is very inelegant.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: