Home

Awesome

Rollup Smart Asset Plugin

Overview

Rollup plugin to rebase, inline or copy assets referenced from the JavaScript code.

Usage

import smartAsset from "rollup-plugin-smart-asset"

const smartAssetOpts = { ... }

export default {
  input: "src/index.tsx",
  output: {
    file: "dist/index.js",
    format: "iife"
  },
  plugins: [
    ...
    smartAsset(smartAssetOpts)
    ...
  ]
}

Configuration

For libraries it is recommended to use inline or copy mode with keepImport option to delegate bundling to consumer's package bundler. Asset hashing is not needed for this case and it is safe to set useHash: false and keepName: true.

For applications it is also recommended to use inline or copy mode with enabled by default hashing.

Default settings are set to be the same as in postcss-smart-asset to have one config for both of them.

Main options:

For more details about include / exclude syntax please refer to: https://github.com/micromatch/micromatch

Mode: rebase

Rebase asset references to be relative to specific directory.

Output:

// without keepImport (inside wrapper)
export default "public_path_to_asset"

// with keepImport and cjs module format
const myAsset = require("relative_path_to_asset_from_bundle")

// with keepImport and esm module format
import myAsset from "relative_path_to_asset_from_bundle"

Options:

Mode: inline

Inline assets as base64 urls directly in source code.

Keep in mind, all options for copy mode will be used if falled back to copy mode.

Output:

export default "data:{mimeType};base64,{data}"

Options:

Mode: copy

Copy asset to target directory and rebase original references to point to target path depending on provided configuration.

Output:

// without keepImport (inside wrapper)
export default "public_path_to_asset"

// with keepImport and cjs module format
const myAsset = require("relative_path_to_asset_from_bundle")
// + file is copied to target directory

// with keepImport and esm module format
import myAsset from "relative_path_to_asset_from_bundle"
// + file is copied to target directory

Options:

Preserve Modules

Rollup preserveModules: true is supported but additional context is required for the plugin to properly detect rebased path to the asset.

Additional options needed:

Migration

Migration from v1.x to v2.x

Changes:

The default configuration is changed in favor of default hash functions that are available in NodeJS without requirement to build any native extensions during npm install.

If you would like to use ultra fast metrohash64 or metrohash128 hashes then do npm install metrohash and set hashOptions.hash to metrohash64 or metrohash128.

If you would like to use ultra fast xxhash32 or xxhash64 hashes then do npm install xxhash and set hashOptions.hash to xxhash32 or xxhash64.

Alternatives

@rollup/plugin-url (ex rollup-plugin-url)

https://github.com/rollup/rollup-plugin-url or https://github.com/rollup/plugins/tree/master/packages/url

This Rollup plugin has fewer options, doesn't work if asset is already loaded by another plugin (by sourcemaps, for example) and doesn't have keepImport like option (designed for applications).

postcss-smart-asset

https://github.com/sebastian-software/postcss-smart-asset

This PostCSS plugin works for assets referenced from CSS, but doesn't work for assets imported from JavaScript.

rollup-plugin-rebase

https://github.com/sebastian-software/rollup-plugin-rebase

This Rollup plugin is designed for libraries, has keepImport like option enabled by default so can't be used for applications.

Contribution

PRs are very welcome!

License

MIT