New
Apps 6,283Makers 6,262This week 13
Promote your app−30%
Vite Import Attributes screenshot 1
4views in
the last 30 days

Vite Import Attributes

Vite plugin that rewrites TC39 import attributes into Vite's query-string syntax

Built with Claude Code

by chances · Unclaimed · from USUnited States · added Oct 29, 2026
no ratings0 comments

A Vite plugin that transforms import attributes into Vite's native query-string syntax, so you can write modern with { ... } imports today.

What it does

Import attributes (formerly "import assertions") let you attach metadata to module imports:

import text from "./notes.txt" with { type: "text" };
import styles from "./theme.scss" with { type: "css" };
import data from "./schema.json" with { type: "json" };
import { Counter } from "./counter" with { island: "client-only" };
const mod = await import("./lazy", { with: { island: "client-only" } });

Vite doesn't understand those attributes yet. This plugin rewrites them into Vite's existing query-string mechanism so they "just work":

  • { type: "text" } becomes ?raw, matching Vite's built-in handling for raw text imports.
  • { type: "css" } becomes ?inline, so Vite runs the CSS pipeline (preprocessors, PostCSS) and hands back the processed stylesheet as a string default export — closer to a native with { type: "css" } import than ?raw.
  • Other attributes are serialized into a ?__attributes=<json> query string that other plugins can read via parseImportAttributes(id).
  • Dynamic import(..., { with: { ... } }) calls are rewritten the same way.

Install

This is a Deno project; install it as a JSR or npm dependency in your own project, or import directly:

// deno.json
{
  "imports": {
    "vite-import-attributes": "jsr:...",
    "vite": "npm:vite"
  }
}

Usage

// vite.config.ts
import { defineConfig } from "vite";
import vitePluginImportAttributes from "vite-import-attributes";

export default defineConfig({
  plugins: [vitePluginImportAttributes()],
});

Options

vitePluginImportAttributes({
  include: ["src/**/*"], // default: all files
  exclude: /\/node_modules\//, // default
});

Reading attributes in your own plugin

The serialized attributes are available to other Vite plugins via the exported helper:

import { parseImportAttributes } from "vite-import-attributes";

transform(_code, id) {
  const { rawId, attributes } = parseImportAttributes(id);
  if (attributes.island === "client-only") {
    // ...
  }
}

Why a "pre" resolveId hook?

If you use Vite with a custom resolver (e.g. @deno/vite-plugin) that resolves bare or aliased specifiers, the resolver may drop the ?raw / ?inline / ?__attributes= query that this plugin appends. To prevent that, this plugin runs in enforce: "pre" and resolves the base specifier itself first, then re-appends the query. The bundled tests cover this case.

Development

deno test      # run unit + e2e tests
deno check .   # typecheck
deno fmt .     # format

License

MIT

Description taken from the repository README.

Ratings

–
0 ratings
5 0
4 0
3 0
2 0
1 0

Rate this app

Sign in to rate

Comments 0

Sign in to comment

No comments yet.

Related apps

#vite · #plugin · #javascript