/rollup-plugin-data-files

A rollup plugin to include binary/string data files directly in your code with custom or default transformations.

Primary LanguageJavaScript

Install

npm i -D rollup-plugin-data-files

Requirements

  • rollup v2.29.0 or later to allow for the addition of a file in a globbed directory to rebuild in this plugin

Usage

include rollup-plugin-data-files in your rollup plugins array for a build:

const dataFiles = require('rollup-plugin-data-files');

...
plugins: [
  dataFiles({
    something: {
      // files to include when data-files!something is imported
      include: 'test/fixtures/**',
      // these are binary files, give me a uint8array
      transform: 'binary'
    }
  })
]
...

When you want to include the data file use data-files!<configured-key> so for our example that would be:

import something from "data-files!something";

// get and console.log the uint8 array for some-file-name.jpg
console.log(something['some-file-name.jpg']());

Also know that you can find your file in the output bundle by searching for rollup-plugin-data-files and finding your key.

Options

The main Object that is passed to rollup-plugin-data-files takes keys that will refer to the reference id of your data file. The value for that key is the configuration for it's build. Those options are defined below.

.transform

Default: 'binary'

As a string

transform can be set 4 possible string values:

  1. 'binary' exports an object with functions that return a Uint8Array of file contents.
  2. 'string' exports an object with functions that will return a string of the file contents.
  3. 'json' exports an object with function that will return a JSON.parse of the file contents.
  4. 'js' exports an object with function that will return a the result of requiring the files specified.
As a function

Using transform as a function

transform: function(key, files) {
  let output = 'module.exports = {\n'

  files.forEach(function({filepath, contents}) {
    // filepath is a string, contents is a buffer.
    output += `'${filepath}': () => '${contents.toString()}',\n`
  });

  output += '};\n';
  return output;
}

.include

Note: if you want the addition of files to trigger a rebuild, make sure you are using a glob or directly pass in a directory here. A file, glob or an array of files/globs to include in your data-file.

.exclude

A file, glob or an array of files/globs to exclude from your data-file.

.extensions

Default: true

Should file extensions be included in the resulting object generated by string transform.

example for foo.txt with extensions true:

{
  'foo.txt': () => 'hello world'
}

example for foo.txt with extensions false:

{
  'foo': () => 'hello world'
}