vite-plugin-plain-text
raw JSON → 1.4.2 verified Mon Apr 27 auth: no javascript
A Vite plugin that transforms matched files (e.g., LICENSE, .text, .glsl) into plain text strings, enabling direct import of file contents. Current stable version is 1.4.2, with regular releases. Key features include support for both named and default exports, automatic type declaration generation (.d.ts), sourcemaps, and ESM compatibility. Compared to alternatives like vite-plugin-virtual-plain-text, this plugin operates on real file paths rather than virtual modules.
Common errors
error Cannot find module './file.text' or its corresponding type declarations. ↓
cause TypeScript cannot resolve module types for matched files without declaration files.
fix
Add manual module declarations in vite-env.d.ts or enable dtsAutoGen option.
error Uncaught SyntaxError: The requested module './LICENSE' does not provide an export named 'default' (at ...) ↓
cause After v1.2.0, matched files export a named export 'plainText' instead of default export.
fix
Change import to use named import: import { plainText } from './LICENSE' or set namedExport: false in plugin options.
error Error: 'plainText' is not exported by node_modules/vite-plugin-plain-text/dist/index.mjs, imported by ... ↓
cause Named import from the plugin package itself is incorrect; it exports a default function.
fix
Use default import: import plainText from 'vite-plugin-plain-text'
Warnings
breaking In v1.2.0, the default export behavior changed: previously matched files used default export; now they use named export 'plainText' by default. ↓
fix Update imports to use named export 'plainText' or set namedExport: false to get default export.
deprecated Option 'namedExport' accepts false to enable default export; passing undefined or '' is deprecated and will be removed in future. ↓
fix Use namedExport: false explicitly instead of undefined or empty string.
gotcha Glob patterns without leading slash may not match as expected; use absolute-like patterns starting with / or **/ to avoid issues. ↓
fix Use patterns like '**/*.text' or /\/LICENSE$/ for reliability.
gotcha Automatic type declaration generation (dtsAutoGen) creates .d.ts files in the source directory; these may be committed by accident. ↓
fix Add generated .d.ts files to .gitignore or use distAutoClean to remove them on startup.
Install
npm install vite-plugin-plain-text yarn add vite-plugin-plain-text pnpm add vite-plugin-plain-text Imports
- default (plugin function) wrong
const plainText = require('vite-plugin-plain-text')correctimport plainText from 'vite-plugin-plain-text' - plainText as named import from matched files wrong
import fileText from './file.text'correctimport { plainText } from './file.text' - Type declarations (manual)
declare module '*.text' { export const plainText: string }
Quickstart
// vite.config.ts
import { defineConfig } from 'vite';
import plainText from 'vite-plugin-plain-text';
export default defineConfig({
plugins: [
plainText([/\/LICENSE$/, '**/*.text', /\.glsl$/])
]
});
// src/component.js
import { plainText as license } from '../LICENSE';
import { plainText as lorem } from '../lorem-ipsum.text';
import { plainText as siren } from '../siren.glsl';
console.log(license);
console.log(lorem);
console.log(siren);