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-inject-externals

v1.1.1

Published

When vite is packaged, the specified package is changed to be imported from CDN.

Downloads

384

Readme

简体中文 | English

When vite is packaged, the specified module is imported from CDN instead.

Script tag and link tag can be injected into the specified location.

Reduce build time and increase page load speed in production environments.

Installation

Install the plugin with npm:

npm install --save-dev vite-plugin-inject-externals

Basic Usage

// vite.config.js
import injectExternals from 'vite-plugin-inject-externals'

export default {
  plugins: [
    injectExternals({
      // Default value: 'head-prepend'
      // The custom injection location will replace the corresponding text in index.html
      injectTo: '<!-- Custom placeholder for vite plugin inject externals -->',
      modules: [
        {
          name: 'vue',
          global: 'Vue',
          path: 'https://unpkg.com/[email protected]/dist/vue.global.prod.js'
        },
        {
          name: 'axios',
          global: 'axios',
          // If there are name and global, but there are no path and htmltag, the global variables will be replaced directly, but the script tag will not be injected
          // path: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js'
        },
        {
          name: 'md-editor-v3',
          global: 'MdEditorV3',
          path: 'https://cdn.jsdelivr.net/npm/[email protected]/lib/md-editor-v3.umd.js',
          injectTo: '<!-- example2 -->'
        },
        // If there is path but no global, the link tag will be injected
        {
          name: 'md-editor-v3/lib/style.css',
          // If there is a name but no global, the import of name will be deleted, which is only applicable to bare imports(import 'md-editor-v3/lib/style.css')
          path: 'https://cdn.jsdelivr.net/npm/[email protected]/lib/style.css',
        }
      ]
    })
  ],
}

Result

// dev
import { createApp } from 'vue'
import axios from 'axios'
import MdEditorV3 from 'md-editor-v3'
import 'md-editor-v3/lib/style.css'

createApp()
axios()
console.log(MdEditorV3)

// build
Vue.createApp()
axios()
console.log(MdEditorV3)
<!-- Inject CDN links -->
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/vue.global.prod.js"></script>

Extended usage

// vite.config.js
import injectExternals from 'vite-plugin-inject-externals'

export default {
  plugins: [
    injectExternals({
      // Default value: 'head-prepend'
      // The custom injection location will replace the corresponding text in index.html
      injectTo: '<!-- Custom placeholder for vite plugin inject externals -->',
      modules: [
        {
          name: 'vue',
          // When the import method is bare imports(import 'md-editor-v3/lib/style.css'), and there is a name('md-editor-v3/lib/style.css') but no global, the import will be deleted.
          // When the import method is not bare imports, and there are name and global, the global variables will be replaced.
          global: 'Vue',
          // If there is a path, the script tag will be injected if there are name and global.
          // If there is a path, the link tag will be injected if there is no global.
          path: 'https://unpkg.com/[email protected]/dist/vue.global.prod.js',
          // Custom HTML tags with higher priority than path
          htmlTag: {
            tag: 'script',
            attrs: {
              type: 'text/javascript',
              crossorigin: '',
              src: 'https://unpkg.com/[email protected]/dist/vue.global.prod.js'
            }
          },
          // Module has no injectto. The injectto of the previous layer is the default
          injectTo: '<!-- Custom1 -->'
        }
      ]
    })
  ],
}

InjectExternalsConfig

| Name | Required | Desc | Type | Default | |:--------:|:---------:|:--------------------------------------------------------------------------------------------------------------------------------------------:|:------------------------------------------------------------:|:----------------:| | command | false | Inject HTML tag when running which command, A value of true indicates that HTML tags are injected when both build and serve commands are run | 'build' / true | 'build' | | injectTo | false | Location of HTML tags injection | 'head' / 'body' / 'head-prepend' / 'body-prepend' / string | 'head-prepend' | | modules | true | Modules configuration | InjectExternalsModule[] | [] |

InjectExternalsModule

| Name | Required | Desc | Type | Default | |:--------:|:--------:|:-----------------------------------------------------------------------------------------------:|:---------:|:--------------------------------:| | name | false | Module name | string | | global | false | Global variables corresponding to the module | string | | path | true | CDN link of JS or CSS resources. If there is no global, it indicates that it is a CSS resource. | string | | htmlTag | true | Custom HTML tags, priority is higher than path. | HtmlTag | | injectTo | false | Location of HTML tags injection | string | InjectExternalsConfig.injectTo |

HtmlTag

| Name | Required | Desc | Type | Default | |:-----:|:--------:|:-----------------------------------------------------:|:--------:|:-------:| | tag | true | Tag name | string | | attrs | false | Attributes({ 'Attribute name': 'Attribute value' }) | object |

License

MIT