1. rollup-plugin-data-files
Bundle web workers that work in nodejs and the browser, without a separate build target.
rollup-plugin-data-files
Package: rollup-plugin-data-files
Created by: brandonocasey
Last modified: Mon, 16 May 2022 04:19:50 GMT
Version: 0.1.0
License: Apache-2.0
Downloads: 802
Repository: https://github.com/brandonocasey/rollup-plugin-data-files

Install

npm install rollup-plugin-data-files
yarn add rollup-plugin-data-files

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'
}

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code