Features
- CDN Imports - Import npm packages directly from CDN URLs
- Tree-shaking - Full tree-shaking support for CDN modules
- Multiple CDNs - Support for Skypack with fallback chains
- Version Pinning - Pin specific package versions
- Custom Resolvers - Use any CDN with custom URL resolvers
Installation
Quick Start
Configuration
Version Pinning
Pin specific versions for reproducible builds:Custom CDN Resolvers
Use any CDN by providing custom resolver functions:Custom Fetch Implementation
For environments without global fetch or for adding custom headers:How It Works
- Resolution: When Rollup encounters an import, the plugin checks if it’s a CDN-resolvable module
- Fetching: The module is fetched from the configured CDN(s)
- Transformation: Relative imports within the fetched module are resolved to absolute CDN URLs
- Bundling: Rollup processes the module like any local file, enabling tree-shaking
Import Resolution
| Import Type | Example | Behavior |
|---|---|---|
| Package name | import "lodash" | Resolved via CDN |
| Scoped package | import "@tanstack/react-query" | Resolved via CDN |
| Direct URL | import "https://esm.sh/react" | Fetched directly |
| Local relative | import "./local" | Ignored (handled by Rollup) |
Popular CDN Examples
API Reference
importCdn(options?)
Creates the Rollup plugin.
Options
| Option | Type | Default | Description |
|---|---|---|---|
fetchImpl | FetchImpl | globalThis.fetch | Fetch implementation for HTTP requests |
priority | Array<AvailableCDNs | (key: string) => string> | ["skypack"] | CDN resolution priority |
versions | Record<string, string> | {} | Package version pinning |
Examples
Vite Configuration
Multi-CDN Fallback
Types
Compatibility
- Rollup: 3.x, 4.x
- Node.js: 18+ (requires
fetchor customfetchImpl)