Babel plugin that transforms asset imports and requires (e.g., images, SVGs) into CDN URL strings with content-based hashing. Version 2.0.0 is the current stable release, requiring Node.js 20+ and @babel/core ^7.20.0. It supports both ESM import and CommonJS require, configurable file extensions, hash length, output directory for copying assets, and path preservation. Differentiators include isomorphic/SSR focus, TypeScript typings, and simple setup without a module bundler. Release cadence is low; the plugin has had only a few updates since its initial 1.0.0 release.
npm install babel-plugin-transform-assets-import-to-stringVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage with .babelrc configuration, transforming both ES import and CommonJS require to CDN URLs with content-hashed filenames.
Upgrade Node.js to 20 or later, or pin to v1.0.1 if Node <20 is needed.
Update @babel/core to ^7.20.0 or later.
Explicitly specify `extensions` option if relying on defaults.
Always provide `baseUri` unless you intentionally want local relative paths.
Ensure `outputDir` is set and Babel runs as part of build process if asset copying is needed.
Run `npm install babel-plugin-transform-assets-import-to-string --save-dev`
Ensure @babel/core is ^7.20.0 and plugin is imported correctly (as default).
Add `"baseUri": "https://mycdn.com/assets"` to plugin options.
Create the output directory or ensure write permissions. Use absolute paths or adjust relative path.