Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

Import modules from CDN with vite plugin


English | 简体中文

Allows you to specify modules to be introduced in a production environment using a CDN.

This can reduce build time and improve page load speed in production environments.

Installation


Install the plugin with npm:

  1. ``` sh
  2. npm install vite-plugin-cdn-import --save-dev

  3. ```

or yarn

  1. ``` sh
  2. yarn add vite-plugin-cdn-import -D

  3. ```

Basic Usage


Add it to vite.config.js

  1. ``` js
  2. // vite.config.js
  3. import reactRefresh from '@vitejs/plugin-react-refresh'
  4. import importToCDN from 'vite-plugin-cdn-import'

  5. export default {
  6.     plugins: [
  7.         importToCDN({
  8.             modules: [
  9.                 {
  10.                     name: 'react',
  11.                     var: 'React',
  12.                     path: `umd/react.production.min.js`,
  13.                 },
  14.                 {
  15.                     name: 'react-dom',
  16.                     var: 'ReactDOM',
  17.                     path: `umd/react-dom.production.min.js`,
  18.                 },
  19.             ],
  20.         }),
  21.     ],
  22. }
  23. ```

Use autoComplete


  1. ``` js
  2. // vite.config.js
  3. import reactRefresh from '@vitejs/plugin-react-refresh'
  4. import importToCDN, { autoComplete } from 'vite-plugin-cdn-import'

  5. export default {
  6.     plugins: [
  7.         importToCDN({
  8.             modules: [
  9.                 autoComplete('react'),
  10.                 autoComplete('react-dom')
  11.             ],
  12.         }),
  13.         reactRefresh(),
  14.     ],
  15. }
  16. ```

Autocomplete supported modules


  1. ``` sh
  2. "react" | "react-dom" | "react-router-dom" |
  3. "antd" | "ahooks" | "@ant-design/charts" |
  4. "vue" | "vue2" | "@vueuse/shared" |
  5. "@vueuse/core" | "moment" |
  6. "eventemitter3" | "file-saver" |
  7. "browser-md5-file" | "xlsx | "crypto-js" |
  8. "axios" | "lodash" | "localforage"

  9. ```

VueUse demo


  1. ``` js
  2. import vue from '@vitejs/plugin-vue'
  3. import importToCDN, { autoComplete } from 'vite-plugin-cdn-import'

  4. export default {
  5.     plugins: [
  6.         vue(),
  7.         importToCDN({
  8.             modules: [
  9.                 autoComplete('vue'), // vue2 use autoComplete('vue2')
  10.                 autoComplete('@vueuse/shared'),
  11.                 autoComplete('@vueuse/core')
  12.             ],
  13.         }),
  14.     ],
  15. }
  16. ```

Options


Name Description Type Default
:--- :--- :--- :---
prodUrl Overrides the global prodUrl, allowing you to specify the CDN location for a specific module string https://cdn.jsdelivr.net/npm/{name}@{version}/{path}
modules Modules config Array<Module> / Array<(prodUrl:string) => Module> -

Module


Name Description Type
:--- :--- :---
name The name of the module you want to externalize string
var A variable that will be assigned to the module in global scope, Rollup requires this string
path Specify the load path on the CDN string / string[]
css You can alternatively specify multiple style sheets which will be loaded from the CDN string / string[]

Other CDN pordUrl


Name pordUrl
:--- :---
unpkg //unpkg.com/{name}@{version}/{path}
cdnjs //cdnjs.cloudflare.com/ajax/libs/{name}/{version}/{path}

Ressources


webpack-cdn-plugin
rollup-plugin-external-globals
Last Updated: 2023-05-23 11:11:51