npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

babel-plugin-transform-assets-import-to-string

v2.0.0

Published

Babel plugin that transforms image assets import and requires to urls / cdn

Readme

babel-plugin-transform-assets-import-to-string

Babel plugin that transforms image assets import and requires to urls / cdn

npm Build Status

Table of Contents

About

This babel plugin allows you to transform asset files into a string uri, allowing you to point your assets to CDN or other hosts, without needing to run your code through module bundlers.

This helps when doing isomorphic / server-rendered applications.

import image from '../path/assets/icon.svg';
const image1 = require('../path/assets/icon1.svg');

// to

const image = 'https://cdn.example.com/assets/icon.a1b2c3d4.svg';
const image1 = 'https://cdn.example.com/assets/icon1.e5f6g7h8.svg';

// Somewhere further down in your code:
//
// eg: JSX
// <img src={image} alt='' />
//
// eg: Other cases
// ajaxAsyncRequest(image)

See the spec for more examples.

Features

  • Transforms asset imports to CDN URLs with content hashing
  • Supports both ES6 import and CommonJS require()
  • Optional file copying to output directory during build
  • Content-based hashing for cache busting (same content = same hash)
  • Configurable file extensions and hash length
  • Path structure preservation option
  • TypeScript support

Requirements

  • Node.js 20 or higher
  • Babel 7.20 or higher

Installation

$> npm install babel-plugin-transform-assets-import-to-string --save-dev

This plugin requires @babel/core as a peer dependency. If you don't already have it installed:

$> npm install @babel/core --save-dev

Usage

via .babelrc

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com/assets"
      }
    ]
  ]
}

via Node API

require('@babel/core').transform('code', {
  plugins: [
    [
      'transform-assets-import-to-string',
      {
        baseUri: 'https://cdn.example.com/assets'
      }
    ]
  ]
});

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | baseUri | string | "" | URL prefix for transformed paths (e.g., "https://cdn.example.com/assets") | | outputDir | string | undefined | Directory to copy assets to during build. If not set, no files are copied. | | extensions | string[] | [".gif", ".jpeg", ".jpg", ".png", ".svg"] | File extensions to transform. Leading . (dot) is required. | | hashLength | number | 8 | Length of content hash in filename. Set to 0 to disable hashing. | | preservePaths | string | undefined | Base path to strip while preserving directory structure. If not set, filenames are flattened. |

Examples

Basic URI Transformation

Transform imports to CDN URLs with content hashing:

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com/assets"
      }
    ]
  ]
}
// Input
import logo from './images/logo.svg';

// Output
const logo = 'https://cdn.example.com/assets/logo.a1b2c3d4.svg';

File Copying with outputDir

Copy assets to a build directory during transformation:

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com/assets",
        "outputDir": "./dist/assets"
      }
    ]
  ]
}
// Input
import logo from './images/logo.svg';

// Output
const logo = 'https://cdn.example.com/assets/logo.a1b2c3d4.svg';
// File copied to: ./dist/assets/logo.a1b2c3d4.svg

Path Preservation with preservePaths

Keep directory structure by specifying a base path to strip:

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com",
        "outputDir": "./dist/static",
        "preservePaths": "src"
      }
    ]
  ]
}
// Input (file at src/components/icons/logo.svg)
import logo from './icons/logo.svg';

// Output
const logo = 'https://cdn.example.com/components/icons/logo.a1b2c3d4.svg';
// File copied to: ./dist/static/components/icons/logo.a1b2c3d4.svg

Without preservePaths, all files are flattened to the root of outputDir.

Disabling Hash

Use hashLength: 0 to disable content hashing:

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com/assets",
        "hashLength": 0
      }
    ]
  ]
}
// Input
import logo from './images/logo.svg';

// Output
const logo = 'https://cdn.example.com/assets/logo.svg';

Custom Extensions

Transform only specific file types:

{
  "plugins": [
    [
      "transform-assets-import-to-string",
      {
        "baseUri": "https://cdn.example.com/assets",
        "extensions": [".svg", ".png"]
      }
    ]
  ]
}

License

babel-plugin-transform-assets-import-to-string is MIT licensed