1.2.5 • Published 10 months ago

rollup-plugin-string-import v1.2.5

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
10 months ago

npm size libera manifesto

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

1.2.5

10 months ago

1.2.4

12 months ago

1.2.3

12 months ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago