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

Export fileToUrl function for plugins #7162

Closed
4 tasks done
Menci opened this issue Mar 3, 2022 · 10 comments
Closed
4 tasks done

Export fileToUrl function for plugins #7162

Menci opened this issue Mar 3, 2022 · 10 comments
Labels
enhancement: pending triage p2-to-be-discussed Enhancement under consideration (priority)

Comments

@Menci
Copy link
Contributor

Menci commented Mar 3, 2022

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

@bluwy
Copy link
Member

bluwy commented Mar 4, 2022

@Menci Can you show an example where this would be useful for external plugins? It would be helpful to justify exposing another API.

@poyoho
Copy link
Member

poyoho commented Mar 4, 2022

yep, I want to see you example too.
I'd like to share what I've met before

When you encounter picture resources in .markdown, you also can use the asset inside vite to judge whether the picture resources should become files.

for this example maybe we also can use import '1.img' to import assets. 😅
but we don't want to use script tag, it will need to fileToUrl api.

@Menci
Copy link
Contributor Author

Menci commented Mar 4, 2022

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 wasm plugin but I have no way to do it. My workaround is calling the original plugin's load function, which is inelegant and won't work for other types of files (for those without a internal plugin in Vite that emits the file directly).

https://github.com/Menci/vite-plugin-wasm/blob/e501eb234ad22d81d9bc94cb20086b759b4b07df/src/index.ts#L50-L52

@bluwy
Copy link
Member

bluwy commented Mar 5, 2022

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.
PSS: I think you forgot to rename your plugin name

@bluwy bluwy added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 5, 2022
@Niputi Niputi added this to Team Board Mar 5, 2022
@Niputi Niputi moved this to Discussing in Team Board Mar 5, 2022
@Niputi Niputi removed this from Team Board Mar 5, 2022
@reyafyi
Copy link

reyafyi commented Mar 8, 2022

I'm working on a plugin bringing WebAssembly ESM integration to Vite, which is equivalent to Webpack's asyncWebAssembly.

+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 assets.js

@patak-dev patak-dev moved this to Discussing in Team Board Mar 8, 2022
@poyoho
Copy link
Member

poyoho commented Apr 16, 2022

@bluwy 👀👀👀

@bluwy
Copy link
Member

bluwy commented Apr 18, 2022

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!

@bluwy bluwy added p2-to-be-discussed Enhancement under consideration (priority) and removed p2-nice-to-have Not breaking anything but nice to have (priority) labels Apr 18, 2022
@patak-dev
Copy link
Member

Related discussion from @sapphi-red #7763 about the use case

@sapphi-red
Copy link
Member

If it's in JS, I think import url from './something.js?url' could be used for now.

@Menci For example for your plugin, I guess this part
https://github.com/Menci/vite-plugin-wasm/blob/d98e0384950b6aea3156031a17ae65d28bbc7abd/src/index.ts#L37-L57
could be like

`
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")}
`;

@patak-dev
Copy link
Member

@patak-dev patak-dev closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2024
@patak-dev patak-dev removed this from Team Board Jan 9, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: pending triage p2-to-be-discussed Enhancement under consideration (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants