Registry / web-framework / vite-string-plugin

vite-string-plugin

JSON →
library2.0.3jsnpmunverified

A zero-dependency Vite plugin that allows importing files (SVG, MD, XML, TXT, or custom extensions) as raw strings at build time. Current stable version is 2.0.3 as of April 2025, requiring Vite >= 6.3.0 (or compatible rolldown-vite) due to hook filter API usage introduced in v2.0.0. It is more lightweight than generic file-loading plugins like rollup-plugin-string or vite-plugin-raw, but limited to string output. TypeScript type declarations are provided via a separate types entry point for automatic module augmentation of default file extensions.

npm install vite-string-plugin
INSTALL
IMPORT
SIG · VITE-STRING-PLUGIN
V
vite-string-plugin
web-frameworkjavascriptv2.0.3
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stringPlugin
✓ import { stringPlugin } from 'vite-string-plugin'
✗ const { stringPlugin } = require('vite-string-plugin')
ESM-only package, no CommonJS support. Named export, not default.
types
✓ /// <reference types="vite-string-plugin/types" /> or in tsconfig.json: "types": ["vite-string-plugin/types"]
✗ import './vite-string-plugin/types'
Type augmentation is done via types entry; do not import it as a module.
default import of file
✓ import foo from './foo.svg'
✗ import { foo } from './foo.svg'; const data = require('./foo.svg')
Files are exported as default string export; named or require will fail in ESM context.

Sets up the plugin to load .svg files as strings and then imports one in a TypeScript module.

// vite.config.js import { defineConfig } from 'vite'; import { stringPlugin } from 'vite-string-plugin'; export default defineConfig({ plugins: [ stringPlugin({ match: /\.(svg|md|xml|txt)$/i }), ], }); // src/example.ts import svgContent from './icon.svg?raw'; // works only if match includes .svg console.log(svgContent); // string of the file content
Debug
Known issues
breakingvite-string-plugin@2.0.0 requires Vite >= 6.3.0 (or rolldown-vite equivalent) due to hook filter API usage. Older Vite versions will fail to load the plugin.
fix
Upgrade Vite to 6.3.0 or later, or use vite-string-plugin@1.x (v1.0.5) with Vite < 6.3.0.
affects: >=2.0.0
gotchaFiles imported via stringPlugin are not hot-reloaded in dev mode? (check if HMR works)
fix
Ensure the plugin is added to the Vite plugins array; HMR should work automatically for files matching the pattern. If not, try appending a query parameter like ?raw.
affects: *
deprecatedThe 'match' option default covers only .svg, .md, .xml, .txt. Customizing match may be needed for other extensions like .graphql or .html. The plugin does not handle query parameters like ?raw (but can be combined with Vite's ?raw).
fix
Set match to a broader regex or use Vite's built-in ?raw query for specific files.
affects: *
gotchaTypeScript type augmentation only applies to default extensions (.svg, .md, .xml, .txt). For custom extensions, you must add your own declare module statements.
fix
Add ambient type declarations for each custom extension as shown in the README.
affects: *
Errors
Common errors & fixes
TypeError: vite-string-plugin is not a function
Attempting to call require('vite-string-plugin') which returns an ESM module; it does not have a default export.
fix
Use `import { stringPlugin } from 'vite-string-plugin'` instead of `const stringPlugin = require('vite-string-plugin')`.
Plugin vite-string-plugin: filter is not a function
Vite version < 6.3.0 does not support the hook filter API used by plugin v2.
fix
Upgrade Vite to >=6.3.0 or use vite-string-plugin@1.0.5.
TypeScript error: Cannot find module './foo.svg' or its corresponding type declarations.
Missing type augmentation for the imported file extension.
fix
Add `"types": ["vite-string-plugin/types"]` to tsconfig.json or create a .d.ts file with declare module statements.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
viterequiredpeer dependency: plugin requires Vite >= 6.3.0 (or rolldown-vite equivalent) for hook filter API
Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources
vite-string-plugin — npm install vite-string-plugin · libregistry