Skip to content

Import files as direct bytearray #12366

In progress
In progress

Description

@tristan-f-r

Description

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.

This also seems to be a used use case for other situations (stackoverflow) by other developers as well.

Do note that (if this enhancement is accepted), I am fully willing to implement this myself.

Suggested solution

Provide a new query type, e.g. ?buffer that loads the data as a Uint8Array.

Alternative

I could make a new Vite plugin, but this seems useful enough to not mandate the use of a plugin.

Validations

Activity

  1. added
    enhancement
    and removed on Mar 11, 2023
  2. bluwy commented on Mar 11, 2023

    @bluwy
    Member

    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.

  3. tristan-f-r commented on Mar 11, 2023

    @tristan-f-r
    ContributorAuthor

    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.

  4. changed the title [-]Import files as direct array[/-] [+]Import files as direct bytearray[/+] on Mar 28, 2023
  5. patak-cat commented on May 15, 2023

    @patak-cat
    Member

    @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.

  6. bluwy commented on May 16, 2023

    @bluwy
    Member

    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.

  7. braebo commented on May 29, 2023

    @braebo
    Contributor

    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 😅

  8. ryuujo1573 commented on Sep 15, 2023

    @ryuujo1573

    @LeoDog896
    Hi there 👋
    I was wondering if import raw from 'some.wasm?raw' would help!

  9. added
    p2-to-be-discussed
    on Nov 29, 2023
  10. moved this from Discussing to P2 - 5 in Team Boardon Nov 29, 2023
  11. moved this from P2 - 5 to Later in Team Boardon Nov 29, 2023
  12. AlbertMarashi commented on Jan 19, 2024

    @AlbertMarashi

    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"
    import arraybuffer from "vite-plugin-arraybuffer"
    
    export default defineConfig({
        plugins: [
            sveltekit(),
            // used for importing fonts in satori /api/og/+server.ts
            arraybuffer()
        ],
    })

    and declared a fonts.d.ts in the folder where I was importing some fonts

    declare module "*.ttf?arraybuffer" {
        const content: ArrayBuffer
        export default content
    }
  13. Serator commented on May 23, 2024

    @Serator

    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?

  14. pepefeliblu commented on Nov 27, 2024

    @pepefeliblu

    I am not sure if ?buffer will be added, but a neat workaround that might work for some is to use fetch in order to obtain the arrayBuffer:

    import someResource from '/dir/resource.ext?url';
    const arrayBuffer = await fetch(someResource).then(b => b.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!

  15. sapphi-red commented on Feb 21, 2025

    @sapphi-red
    Member

    Since we now have ?url&inline, you can convert it into ArrayBuffers with a function like this:

    function dataUrlToArrayBuffer(dataUrl) {
      const base64StartIndex = dataUrl.indexOf('base64,');
      if (base64StartIndex >= 0) {
        const base64 = dataUrl.slice(base64StartIndex + 'base64,'.length);
        const binaryString = atob(base64);
        const bytes = new Uint8Array(binaryString.length);
        for (var i = 0; i < binaryString.length; i++) {
          bytes[i] = binaryString.charCodeAt(i);
        }
        return bytes.buffer;
      }
      const str = decodeURIComponent(dataUrl.slice(dataUrl.indexOf(',') + 1));
      const enc = new TextEncoder();
      const bytes = enc.encode(str);
      return bytes.buffer;
    }

    https://stackblitz.com/edit/vitejs-vite-jgmtwvec?file=src%2Fmain.js&terminal=dev

  16. sapphi-red commented on Feb 21, 2025

    @sapphi-red
    Member

    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
  17. Its-Just-Nans commented on Feb 22, 2025

    @Its-Just-Nans

    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 %23

  18. carlos-dubon commented on Feb 22, 2025

    @carlos-dubon

    I want to import an image and then convert it to an ArrayBuffer to get a blurhash from it in my Astro app.

    ---
    import arrayBuffer from "@assets/img/img.jpg?arraybuffer";
    import { encode } from "blurhash";
    import { getPixels } from "@unpic/pixels";
    
    const jpgData = await getPixels(
      arrayBuffer
    );
    
    const data = Uint8ClampedArray.from(jpgData.data);
    const blurhash = encode(data, jpgData.width, jpgData.height, 4, 4);
    const placeholder = blurhashToCssGradientString(blurhash);
    ---
  19. tristan-f-r commented on Feb 23, 2025

    @tristan-f-r
    ContributorAuthor

    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.

  20. Its-Just-Nans commented on Feb 23, 2025

    @Its-Just-Nans

    A typedarray would be nice, but I can't think of a nice API design for it

    Another API design idea #19346 (comment)

    import data from 'path/to/file' with { type: "arrayBuffer" }
  21. disjukr commented on Jul 17, 2025

    @disjukr

    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.

    import bytes from "./hello.txt" with { type: "bytes" };
    
    console.log("Bytes:", bytes);
    // Bytes: Uint8Array(12) [
    //    72, 101, 108, 108, 111,
    //    44,  32,  68, 101, 110,
    //   111,  33
    // ]
  22. arcanis commented on Sep 28, 2025

    @arcanis

    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.

    import bytes from "./hello.txt" with { type: "bytes" };

    This is now a stage 2.7 feature, and webpack is working to support it.

  23. self-assigned this
    on Nov 26, 2026
  24. added this to the 8.5 milestone on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementp2-to-be-discussed

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions