rollup-plugin-url-resolve

raw JSON →
0.2.0 verified Mon Apr 27 auth: no javascript

Rollup plugin that resolves import specifiers as URLs, allowing you to fetch dependencies directly from CDNs (e.g., unpkg, jsdelivr) or local files instead of using npm/yarn. Version 0.2.0 is the latest stable release. It supports https, http, file, and data URL protocols. Designed as an alternative to rollup-plugin-node-resolve for URL-based imports. No breaking changes or significant release cadence; the package is minimal and stable. Options are passed through to make-fetch-happen for HTTP caching, retries, and proxy support. Works alongside rollup-plugin-commonjs for CommonJS URLs. Essential for projects that want to avoid a full install step.

error Error: Cannot find module 'foo' from 'src/bar.js'
cause Import specifier 'foo' is not a URL and plugin cannot resolve it.
fix
Change import to a full URL, e.g., 'https://unpkg.com/foo?module'.
error Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
cause Imported URL returns CommonJS and rollup-plugin-commonjs is not configured to handle it.
fix
Add commonjs plugin with include pattern for the URL domain.
error fetch failed: getaddrinfo ENOTFOUND unpkg.com
cause Network error: cannot reach the remote URL.
fix
Check internet connection, or use a different CDN like cdn.jsdelivr.net.
gotcha When using CommonJS URLs, you must include rollup-plugin-commonjs and configure it to treat those URLs as CommonJS, otherwise imports may result in 'Unexpected token' errors.
fix Add commonjs include for the URL domain; see docs for example.
gotcha The plugin does not support node_modules resolution; it is not a drop-in replacement for @rollup/plugin-node-resolve. Only absolute URLs (https, http, file, data) are resolved.
fix Use @rollup/plugin-node-resolve if you need to resolve local node_modules packages.
gotcha Caching with cacheManager uses make-fetch-happen's cache; make sure the directory is writable and .gitignore it if needed.
fix Set cacheManager to a path like '.cache' and add to .gitignore.
npm install rollup-plugin-url-resolve
yarn add rollup-plugin-url-resolve
pnpm add rollup-plugin-url-resolve

Configures Rollup to use URL-based imports from unpkg with caching and CommonJS support for non-module URLs.

// rollup.config.js
import urlResolve from 'rollup-plugin-url-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'esm'
  },
  plugins: [
    urlResolve({
      cacheManager: '.cache'
    }),
    commonjs({
      include: /^https:\/\/unpkg\.com/,
      exclude: /^https:\/\/unpkg\.com.*?\?.*?\bmodule\b/
    })
  ]
};