1.4.2 • Published 1 year ago

vite-plugin-plain-text v1.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

vite-plugin-plain-text

NPM

publish workflow npm version

A Vite plugin that transforms matched files into plain text.

Installation

pnpm i -D vite-plugin-plain-text (or npm/yarn)

Usage Example

Assume we are going to transform these files:

  1. The project's LICENSE file
  2. Textbox
  3. .glsl file

into plain text.

// vite.config.(t|j)s

import { defineConfig } from 'vite';

import plainText from 'vite-plugin-plain-text';

export default defineConfig({
  plugins: [
    // passing regular expression or glob matcher
    plainText([/\/LICENSE$/, '**/*.text', /\.glsl$/]),
  ],
});
// 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)

Advanced Usage

Options Reference

type PlainTextOptions = {
  namedExport?: string | false,
  dtsAutoGen?: boolean,
  distAutoClean?: boolean,
}

Enable Default Export

Use the plainTextOptions.namedExport option to configure the named exported variable. To enable the default export, pass false, '', or undefined.

// vite.config.(t|j)s

import { defineConfig } from 'vite';
import plainText from 'vite-plugin-plain-text';

export default defineConfig({
  plugins: [
    plainText(
      [/\/LICENSE$/, '**/*.text', /\.glsl$/],
      { namedExport: false },
    ),
  ],
});
// component.js

import LICENSE from '../LICENSE'
import Lorem from '../lorem-ipsum.text'
import Siren from '../siren.glsl'

console.log(LICENSE)
console.log(Lorem)
console.log(Siren)

Type Safety

Adding Module Declarations Manually

// vite-env.d.ts
declare module '*/LICENSE' {
    export const plainText: string
}
declare module '*.text' {
    export const plainText: string
}
declare module '*.glsl' {
    export const plainText: string
}

Generate the declaration automatically

  1. plainTextOptions.dtsAutoGen generates .dts files for matched files.
  2. plainTextOptions.dtsAutoClean cleans up these .dts files after the vite plugin starts up each time.
import { defineConfig } from 'vite';
import plainText from 'vite-plugin-plain-text';

export default defineConfig({
  plugins: [
    plainText(
      [/\/LICENSE$/, '**/*.text', /\.glsl$/],
      { namedExport: false, dtsAutoGen: true, distAutoClean: true },
    ),
  ],
});

License

MIT

Alternative

Virtual asset Loader: vite-plugin-virtual-plain-text

1.4.2

1 year ago

1.4.1

1 year ago

1.3.2

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago