Skip to content

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 ​

OptionDefaultDescription
modulesRequiredOne module, an array, or a function returning modules for the Vite environment
urlTemplatejsDelivr{name}, {version}, and {path} template
devfalseUse CDN in development too
resolveVersiontrueRead missing versions from node_modules/{name}/package.json
ssrfalseUse 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 ​

FieldDescription
namenpm/import module name
globalBrowser global; nested paths such as ReactDOM.client are supported
versionPinned version; automatically read when omitted
jsRelative JS path, full URL, or array
cssRelative CSS path, full URL, or array
aliasesAdditional module names mapped to the same global
urlTemplatePer-module URL template
scriptAttributes / styleAttributesAdditional 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 ​

InputTypeRequired / defaultDescription
optionsCdnImportPluginOptionsRequiredCDN modules, version resolution, development, SSR, and tag settings.

Returns ​

ValueTypeDescription
pluginPluginCDN import plugin for Vite plugins.