cdnImport(options)
Injects CDN CSS/JavaScript into HTML and replaces configured ESM references with globalThis access during transformation.
ts
cdnImport({
urlTemplate: "https://cdn.jsdelivr.net/npm/{name}@{version}/{path}",
modules: {
name: "react-dom",
global: "ReactDOM",
version: "19.0.0",
js: "umd/react-dom.production.min.js",
aliases: ["react-dom/client"],
},
crossorigin: "anonymous",
dev: false,
});CdnImportPluginOptions Options
| Option | Default | Description |
|---|---|---|
modules | Required | One module, an array, or a function returning modules for the Vite environment |
urlTemplate | jsDelivr | {name}, {version}, and {path} template |
dev | false | Use CDN in development too |
resolveVersion | true | Read missing versions from node_modules/{name}/package.json |
ssr | false | Use browser globals during SSR; normally leave disabled |
crossorigin | "anonymous" | Tag attributes; false omits the tag |
injectTo | "head-prepend" | Vite HTML injection position |
generateScriptTag / generateStyleTag | - | Custom descriptor for an individual resource tag |
CdnModule Module
| Field | Description |
|---|---|
name | npm/import module name |
global | Browser global; nested paths such as ReactDOM.client are supported |
version | Pinned version; automatically read when omitted |
js | Relative JS path, full URL, or array |
css | Relative CSS path, full URL, or array |
aliases | Additional module names mapped to the same global |
urlTemplate | Per-module URL template |
scriptAttributes / styleAttributes | Additional attributes merged into the tag |
Supports default/named imports, named reexports, export as name, and static-string dynamic imports. Plain export from throws because dynamic globals cannot safely become static ESM exports.
With dev: true, development still injects CDN tags and transforms JS module references. Inline HTML styles and other CSS requests do not undergo JavaScript AST transformation.
cdnImport CDN
Replaces specified dependencies with CDN globals and injects CSS/JavaScript in dependency order.
Signature
ts
export function cdnImport(options: CdnImportPluginOptions): Plugin;Example
ts
import { cdnImport } from "fast-vite-plugins";
const plugin = cdnImport({ modules: { name: "vue", global: "Vue", version: "3.5.11", js: "dist/vue.global.prod.js" } });
export default { plugins: [plugin] };Input
| Input | Type | Required / default | Description |
|---|---|---|---|
options | CdnImportPluginOptions | Required | CDN modules, version resolution, development, SSR, and tag settings. |
Returns
| Value | Type | Description |
|---|---|---|
plugin | Plugin | CDN import plugin for Vite plugins. |
