New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rollup-plugin-string-import

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-string-import

Import any file as a string

latest
Source
npmnpm
Version
1.2.6
Version published
Maintainers
1
Created
Source

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

Keywords

rollup

FAQs

Package last updated on 15 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts