rollup-plugin-string-import
v1.2.5
Published
Import any file as a string
Downloads
1,304
Maintainers
Readme
rollup-plugin-string-import
🍣 A Rollup plugin to import any file as a string with proper TypeScript support
Requirements
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Install
Using npm
:
npm install -D rollup-plugin-string-import
or yarn
yarn add -D rollup-plugin-string-import
Usage
Create a rollup.config.js
configuration file and import the plugin:
import { importAsString } from 'rollup-plugin-string-import';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs',
},
plugins: [
importAsString({
include: ['**/*.txt', '**/*.frag', '**/*.vert'],
exclude: ['**/*.test.*'],
}),
],
};
Then call rollup
either via the CLI or the API.
In runtime, all matching files will be imported as strings, same as if they were defined in TypeScript files like this:
export default `This is a
text file
content!`;
Optionally, you can create a .d.ts
file to let TypeScript know that such imports should be treated as strings:
// string-import.d.ts
declare module '*.txt' {
const file: string;
export default file;
}
declare module '*.vert' {
const file: string;
export default file;
}
declare module '*.frag' {
const file: string;
export default file;
}
Options
include
Type: String
| Array[...String]
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on.
exclude
Type: String
| Array[...String]
Default: undefined
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
transform
Type: (content: String, file: String) => String
Default: content => content
A transformer function that will be applied to each matched file. In this example, we append "Hello World" to each .txt
file:
...
importAsString({
include: ['**/*.txt', '**/*.frag', '**/*.vert'],
exclude: ['**/*.test.*'],
transform:
(content, file) => file.endsWith('.txt') ? `${content}\nHello World` : content,
}),
...
Meta
Licensed under the GPL version 3.0 or higher