npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-dynamic-import-cdn

v0.2.2

Published

在生产环境中使用 CDN 引入依赖,同时支持动态引入

Downloads

179

Readme

vite-plugin-dynamic-import-cdn

English | 简体中文

GitHub tag License

  • In production environments, dependencies can be loaded from a CDN to reduce build time and bundle size.
  • For dynamically imported dependencies, the CDN resources can also be dynamically loaded to reduce initial request time.

References

  • This plugin is based on vite-plugin-cdn-import.
  • Fixes issues with some preset modules not working correctly with certain CDN paths.
  • Fixes some dependencies failing to be external during the build process.
  • Adds support for dynamically importing CDN resources.

Installation

# npm
npm i vite-plugin-dynamic-import-cdn -D

# yarn
yarn add vite-plugin-dynamic-import-cdn -D

# pnpm
pnpm add vite-plugin-dynamic-import-cdn -D

Basic Usage

// vite.config.js
import { defineConfig } from 'vite'
import { dynamicImportCdn } from 'vite-plugin-dynamic-import-cdn'

export default defineConfig({
  plugins: [
    // ...other plugins
    dynamicImportCdn({
      cdnUrlPreset: 'unpkg',
      modules: [
        // Preset modules
        'react',
        'react-dom',
        'dayjs',
        'antd@5',
        // Custom modules
        {
          dynamic: 'global', // Enable dynamic loading of CDN resources
          name: 'echarts',
          var: 'echarts',
          path: 'dist/echarts.min.js'
        },
      ]
    })
  ]
})

Preset npm Packages

  • react
  • react-dom
  • react-router-dom
  • antd@5
  • antd@4
  • vue@3
  • vue@2
  • vue-router@4
  • vue-router@3
  • moment
  • dayjs
  • axios
  • lodash

Parameters

Plugin Configuration

| Name | Description | Type | | ----------------- | ------------------------------------------ | --------------------------------------------- | | cndUrl | Template URL for generating CDN file paths | string | 'jsdelivr' | 'cdnjs' | 'unpkg' | | modules | Configuration for CDN modules | (string | Module)[] | | generateScriptTag | Customizes the generated script tag | Omit<HtmlTagDescriptor, 'tag' | 'children'> | | generateCssTag | Customizes the generated CSS tag | Omit<HtmlTagDescriptor, 'tag' | 'children'> |

Module Configuration

| Name | Description | Type | | --------------- | ------------------------------------------------------------------------ | -------------------------------- | | name | Name of the package to be accelerated by the CDN | string | | alias | Alias for the name, e.g., react-dom/client is an alias for react-dom | string[] | | var | Global variable assigned to the module | string | | path | Specific CDN path for loading | string | string[] | | css | Stylesheets to load from the CDN | string | string[] | | version | Manually specify the dependency version if it can't be resolved | string | | dynamic | Enable dynamic CDN import for dynamically imported modules | 'esm' | 'global' | | externalGlobals | Customizes global variables; see [rollup-plugin-external-globals] | (id: string) => (string | void) | | descriptor | Additional tags to insert, e.g., for modifying the global name of lodash | HtmlTagDescriptor |

Conversion Process

Standard Import

// index.js
import * as echarts from 'echarts'
echarts.init(document.getElementById('main'))

// ----------After Conversion-------------

// index.html
<script src="https://unpkg.com/[email protected]/dist/echarts.min.js"></script>

// index.js
echarts.init(document.getElementById('main'))

Dynamic Import

// index.js
import('echarts').then(echarts => {
  echarts.init(document.getElementById('main'))
})

// ----------After Conversion-------------

// index.html
window.echartsThenable = {
  then(resolve, reject) {
    if (window.echarts) return resolve(window.echarts)
    // __loadScript__: A function to create a script tag for loading JS resources
    window.__loadScript__("https://unpkg.com/[email protected]/dist/echarts.min.js")
      .then(() => {
        resolve(window.echarts)
      })
      .catch((err) => {
        reject(err)
      })
  }
}

// index.js
Promise.resolve(echartsThenable).then(echarts => {
  echarts.init(document.getElementById('main'))
})

Resources