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

rollup-plugin-base64

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-base64

Converts files to base64

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
306
-19.69%
Maintainers
1
Weekly downloads
 
Created
Source

rollup-plugin-base64

A Rollup plugin to load any file as a base64 string. This is especially useful for embedding WebAssembly programs into your code so no additional .wasm file needs to be loaded.

Note that with base64 encoding the size of the input file will be increased by around 33%.

Usage

A minimum example of code using a WASM function that adds two numbers together.

The input files

// rollup.json
const { base64 } = require("rollup-plugin-base64");

export default {
    input: "index.js",
    output: {
        file: "build.js", format: 'iife'
    },
    plugins: [
        base64({ include: "**/*.wasm" })
    ]
};
// index.js
import wasmB64 from "./add.wasm";

// This works in Node, in the browser you will need to convert from base64 with a different method.
const wasmBytes = Buffer.from(wasmB64, "base64");

// Note: synchronous loading is only recommended for very small binaries, please replace
// this with your favorite (asynchronous) WASM module loader code).
const mod = new WebAssembly.Module(wasmBytes);
const instance = new WebAssembly.Instance(mod, {});

const addResult = instance.exports.add(12, 34);
console.log(addResult); // 46

The output

// build.js
(function () {
	'use strict';

	var wasmB64 = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=";

	// This works in Node, in the browser you will need to convert from base64 with a different method.
	const wasmBytes = Buffer.from(wasmB64, "base64");

	// Note: synchronous loading is only recommended for very small binaries, please replace
	// this with your favorite (asynchronous) WASM module loader code).
	const mod = new WebAssembly.Module(wasmBytes);
	const instance = new WebAssembly.Instance(mod, {});

	const addResult = instance.exports.add(12, 34);
	console.log(addResult); // 46

}());

Simples. Let's try it:

$ node build.js
46

This example can be found in the example folder.

Configuration

You can specify which files to apply base64ing to by specifying the include and exclude field in the config object. include is required, if you leave it out an error will be thrown.

Keywords

rollup-plugin

FAQs

Package last updated on 15 Jun 2020

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