New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@zippybee/plugin-cdn-import

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zippybee/plugin-cdn-import

Import packages from CDN for the vite plugin

latest
Source
npmnpm
Version
2.0.2
Version published
Maintainers
1
Created
Source

从 CDN 加载 modules 的 vite 插件

GitHub tag License

允许指定 modules 在生产环境中使用 CDN 引入。

这可以减少构建时间,并且提高生产环境中页面加载速度。

安装

下载 npm 插件

npm install vite-plugin-cdn-import --save-dev

or yarn

yarn add vite-plugin-cdn-import -D

基本用法

// vite.config.js
import reactRefresh from '@vitejs/plugin-react-refresh';
import importToCDN from 'vite-plugin-cdn-import';

export default {
  plugins: [
    importToCDN({
      modules: [
        {
          name: 'react',
          var: 'React',
          path: `umd/react.production.min.js`,
        },
        {
          name: 'react-dom',
          var: 'ReactDOM',
          path: `umd/react-dom.production.min.js`,
        },
      ],
    }),
  ],
};

使用 autoComplete

// vite.config.js
import reactRefresh from '@vitejs/plugin-react-refresh';
import importToCDN, { autoComplete } from 'vite-plugin-cdn-import';

export default {
  plugins: [
    importToCDN({
      modules: [autoComplete('react'), autoComplete('react-dom')],
    }),
    reactRefresh(),
  ],
};

自动完成支持的 module

"react" | "react-dom" | "react-router-dom" |
"antd" | "ahooks" | "@ant-design/charts" |
"vue" | "vue2" | "@vueuse/shared" |
"@vueuse/core" | "moment" |
"eventemitter3" | "file-saver" |
"browser-md5-file" | "xlsx | "crypto-js" |
"axios" | "lodash" | "localforage"

VueUse demo

import vue from '@vitejs/plugin-vue';
import { importToCDN, autoComplete } from '@zippybee/plugin-cdn-import';

export default {
  plugins: [
    vue(),
    importToCDN({
      modules: [
        autoComplete('vue'), // vue2 使用 autoComplete('vue2')
        autoComplete('@vueuse/shared'),
        autoComplete('@vueuse/core'),
      ],
    }),
  ],
};

参数

NameDescriptionTypeDefault
prodUrl覆盖全局 prodUrl 属性,允许为特定的模块指定 CDN 的位置stringhttps://cdn.jsdelivr.net/npm/{name}@{version}/{path}
modules模块配置Array<Module> / Array<(prodUrl:string) => Module>-

Module 配置

NameDescriptionType
name需要 CDN 加速的包名称string
var全局分配给模块的变量,Rollup 需要这个变量名称string
path指定 CDN 上的加载路径string / string[]
css可以指定从 CDN 地址上加载多个样式表string / string[]

其他的 CDN pordUrl 地址

NamepordUrl
unpkg//unpkg.com/{name}@{version}/{path}
cdnjs//cdnjs.cloudflare.com/ajax/libs/{name}/{version}/{path}

资源

Keywords

vite cdn plugin

FAQs

Package last updated on 05 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts