Skip to content

?raw after transformation #19808

Closed as duplicate
Closed as duplicate

Description

@huseeiin

Description

sometimes you want to import with ?raw but after vite's transformation like typescript, tailwindcss, etc.

Suggested solution

if you import script from "./file.ts?raw&transform" (the syntax doesn't matter) you'll get the raw string after the typescript has been transpiled to javascript

Alternative

No response

Additional context

No response

Validations

Activity

  1. added theissue type on Apr 7, 2025
  2. sapphi-red commented on Apr 8, 2025

    @sapphi-red
    Member

    I think this is possible if #6757 is fixed.

    import scriptUrl from './file.ts?url'
    const res = await fetch(scriptUrl)
    const script = await res.text()
  3. huseeiin commented on Apr 8, 2025

    @huseeiin
    Author

    I think this is possible if #6757 is fixed.

    import scriptUrl from './file.ts?url'
    const res = await fetch(scriptUrl)
    const script = await res.text()

    isn't this unnecessary fetch() call? also how will it work in ssr?

  4. sapphi-red commented on Apr 9, 2025

    @sapphi-red
    Member

    If you want to avoid the fetch, you should be able to use ?url&inline (#12366 (comment)).

  5. huseeiin commented on Apr 9, 2025

    @huseeiin
    Author

    If you want to avoid the fetch, you should be able to use ?url&inline (#12366 (comment)).

    i got a data: url 🤔 i can convert it to a string using Buffer or TextDecoder but sometimes it comes as data:application/octet-stream which makes it harder to convert to a string

  6. huseeiin commented on Apr 9, 2025

    @huseeiin
    Author

    nvm, its actually easy:

    import dataUrl from "../package.json?url&inline";
    
    // Remove the prefix
    const base64Data = dataUrl.split(",")[1];
    
    // Decode the base64 data
    const binaryData = atob(base64Data);
    
    // Convert the binary data to a string
    const result = binaryData.toString();

    but would be cool if there was a ?query to get it as a raw string

  7. huseeiin commented on Apr 9, 2025

    @huseeiin
    Author

    the typescript and jsx is not transformed as well :(

  8. sapphi-red commented on Apr 9, 2025

    @sapphi-red
    Member

    Well, that's the reason I mentioned #6757.

  9. locked and limited conversation to collaborators on Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions