You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm making a tool that allows reading NBT files, and NBT files are directly binary data. In order to provide a clear demonstration, I'm grabbing all files from my local /resources folder with a glob import, but whenever I use ?raw or { as: "raw" }, it corrupts the data when it turns it into a string.
Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Reacted by Kasper, Guilherme Bernal, Mirko Akov, Saharath Kleips, Yosuke Torii, Michael Franzl, gtmnayan, mor-o, Albert Marashi, dominictobias and 27 more
Seems like a nice addition, but we probably can't add this until the next major since it could collide with other Vite plugins that uses ?buffer for something else. A vite plugin would be good to implement this at the meantime though.
Alright, it seems that the stackoverflow answer offers a pretty good substitute and works for both glob imports { as: "raw-hex" } and regular/dynamic imports in the meantime.
@bluwy given that the plugin is straight forward, maybe we should close this one? I also think it is an interesting feature but for completness we may later need to add other types too? ArrayBuffer, SharedArrayBuffer, etc.
I'm not sure about your point with ArrayBuffer and SharedArrayBuffer since we're reading the file as a buffer itself. But I could see the scope grow with needing Int32Array etc instead, which maybe we can support with ?buffer=Int32Array.
I think it's a nice addition for Vite since it's small, generic, and (kinda) useful when you need it, unlike other feature requests that are specific to their preference and/or non-standard.
Alright, it seems that the stackoverflow answer offers a pretty good substitute and works for both glob imports { as: "raw-hex" } and regular/dynamic imports in the meantime.
Any idea why this plugin works during build, but not during dev? In dev, my import.default is just a string (the path to the file) so I'm a bit stuck on this one 😅
God damn this is annoying. There needs to be an arraybuffer import
I used this plugin to get it to work
import{sveltekit}from"@sveltejs/kit/vite"import{defineConfig}from"vite"importarraybufferfrom"vite-plugin-arraybuffer"exportdefaultdefineConfig({plugins: [sveltekit(),// used for importing fonts in satori /api/og/+server.tsarraybuffer()],})
and declared a fonts.d.ts in the folder where I was importing some fonts
Reacted by dominictobias, Ryan Conceicao, Joe Bell, Michael, Perry Rylance, Anton Mikhailov, Albert Marashi, Willian Pinheiro, Zicklag and Nathan Rajlich
Reacted by Joe Bell, Michael and Nathan Rajlich
Reacted by Pat Sissons, Haywirez, Stefan Krüger, Arttu Pennanen, Joe Bell, Michael, Raphael Zenhäusern, Perry Rylance, Martin Kase, Jake Hamilton and 2 more
Seems like a nice addition, but we probably can't add this until the next major since it could collide with other Vite plugins that uses ?buffer for something else.
@bluwy Any chance of supporting ?buffer in Vite 6?
Reacted by Skyf0l, maro, Peter Batory-Bernardin and Kieran Pilkington
For those who are interested in having this a built-in support, it would be helpful if you described your expected usage of it. We are not sure how the API should be designed.
For example,
whether you need an ArrayBuffer or a TypedArray
whether you can convert ArrayBuffer to what you want on your own
why fetching via URL is not an option
if you are using it on the server side, why fs.readFile is not an option
My usage was about inlining a svg in astro in a static website.
The easiest way would be to have a simple (single) line to import it as binary
I've now others solutions
Note that I think your function is almost correct but it should use decodeURIComponent().
If we use decodeURI(), hashtag are not replaced and are still %23
For those who are interested in having this a built-in support, it would be helpful if you described your expected usage of it. We are not sure how the API should be designed. For example,
whether you need an ArrayBuffer or a TypedArray
whether you can convert ArrayBuffer to what you want on your own
why fetching via URL is not an option
if you are using it on the server side, why fs.readFile is not an option
A typedarray would be nice, but I can't think of a nice API design for it, and converting from ArrayBuffer to any TypedArray worked perfectly fine for my use case. Fetching via URL was an option, but this meant that I would have to deal with lifecycle hooks when I can very much just avoid it: essentially, fetching via URL would be a minor performance penalty on my side. This fetching, regardless, is happening client-side.
Activity
bluwy commentedon Mar 11, 2023
Seems like a nice addition, but we probably can't add this until the next major since it could collide with other Vite plugins that uses
?bufferfor something else. A vite plugin would be good to implement this at the meantime though.tristan-f-r commentedon Mar 11, 2023
Alright, it seems that the stackoverflow answer offers a pretty good substitute and works for both glob imports
{ as: "raw-hex" }and regular/dynamic imports in the meantime.[-]Import files as direct array[/-][+]Import files as direct bytearray[/+]patak-cat commentedon May 15, 2023
@bluwy given that the plugin is straight forward, maybe we should close this one? I also think it is an interesting feature but for completness we may later need to add other types too? ArrayBuffer, SharedArrayBuffer, etc.
bluwy commentedon May 16, 2023
I'm not sure about your point with
ArrayBufferandSharedArrayBuffersince we're reading the file as a buffer itself. But I could see the scope grow with needing Int32Array etc instead, which maybe we can support with?buffer=Int32Array.I think it's a nice addition for Vite since it's small, generic, and (kinda) useful when you need it, unlike other feature requests that are specific to their preference and/or non-standard.
braebo commentedon May 29, 2023
Any idea why this plugin works during build, but not during dev? In dev, my
import.defaultis just a string (the path to the file) so I'm a bit stuck on this one 😅ryuujo1573 commentedon Sep 15, 2023
@LeoDog896
Hi there 👋
I was wondering if
import raw from 'some.wasm?raw'would help!AlbertMarashi commentedon Jan 19, 2024
God damn this is annoying. There needs to be an arraybuffer import
I used this plugin to get it to work
and declared a
fonts.d.tsin the folder where I was importing some fontsSerator commentedon May 23, 2024
@bluwy Any chance of supporting
?bufferin Vite 6?pepefeliblu commentedon Nov 27, 2024
I am not sure if
?bufferwill be added, but a neat workaround that might work for some is to use fetch in order to obtain the arrayBuffer:The only downside with this method is that it relies on a Promise, which in some use cases might be inconvenient. Anyways I hope it helps someone!
sapphi-red commentedon Feb 21, 2025
Since we now have
?url&inline, you can convert it into ArrayBuffers with a function like this:https://stackblitz.com/edit/vitejs-vite-jgmtwvec?file=src%2Fmain.js&terminal=dev
sapphi-red commentedon Feb 21, 2025
For those who are interested in having this a built-in support, it would be helpful if you described your expected usage of it. We are not sure how the API should be designed.
For example,
fs.readFileis not an optionIts-Just-Nans commentedon Feb 22, 2025
Hi
My usage was about inlining a svg in astro in a static website.
The easiest way would be to have a simple (single) line to import it as binary
I've now others solutions
Note that I think your function is almost correct but it should use
decodeURIComponent().If we use
decodeURI(), hashtag are not replaced and are still%23carlos-dubon commentedon Feb 22, 2025
I want to import an image and then convert it to an ArrayBuffer to get a blurhash from it in my Astro app.
tristan-f-r commentedon Feb 23, 2025
A typedarray would be nice, but I can't think of a nice API design for it, and converting from
ArrayBufferto anyTypedArrayworked perfectly fine for my use case. Fetching via URL was an option, but this meant that I would have to deal with lifecycle hooks when I can very much just avoid it: essentially, fetching via URL would be a minor performance penalty on my side. This fetching, regardless, is happening client-side.Its-Just-Nans commentedon Feb 23, 2025
Another API design idea #19346 (comment)
disjukr commentedon Jul 17, 2025
A feature was added in Deno 2.4 that allows importing bytes, and I think it would be nice if Vite also followed Deno’s syntax.
arcanis commentedon Sep 28, 2025
This is now a stage 2.7 feature, and webpack is working to support it.