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

v1.5.1

Published

## 介绍

Downloads

11

Readme

VitePluginExternals

介绍

添加外部依赖,注入远程脚本。

使用方式

一、安装插件

yarn add vite-plugin-externals-new -D

二、导入插件

import { defineConfig } from 'vite';
import { VitePluginExternals } from 'vite-plugin-externals-new';
export default defineConfig({
  ...
  plugins: [VitePluginExternals()],
});

三、创建配置

  1. 方法一(创建 external.config.js)
export default {
  vue: {
    src: 'https://unpkg.com/vue@3/dist/vue.global.js',
    varName: 'Vue',
    inject: 'head',
    defer: true,
  },
  'vue-router': {
    src: 'https://unpkg.com/vue-router@4',
    varName: 'VueRouter',
    defer: true,
    async: false,
  },
};

inject默认为headdefer默认为true

  1. 方法二(添加 options 配置)
VitePluginExternals({
  vue: {
    src: 'https://unpkg.com/vue@3/dist/vue.global.js',
    varName: 'Vue',
  },
  'vue-router': {
    src: 'https://unpkg.com/vue-router@4',
    varName: 'VueRouter',
  },
});

插件配置

export interface External {
  [key: string]: {
    /** 远程地址 */
    src?: string;
    /** 变量名称 */
    varName: string;
    /** 脚本注入位置 */
    inject?: 'head' | 'body';
    /** 是否为defer */
    defer?: boolean;
    /** 是否为async */
    async?: boolean;
  };
}

key为外部依赖模块的名称,比如:要把vue设置为外部依赖,则keyvue

模块配置

| 配置 | 类型 | 说明 | | :------ | :------ | :---------------------------------------------------------------------------- | | src | string | 脚本 CDN 地址。 | | varName | string | 模块对应的全局变量名称 。 | | inject | string | 脚本注入位置,可选:head/body ,默认为 head,会注入到第一个 script 标签前面。 | | defer | boolean | vite 默认构建包为 type=module 类型,因此注入脚本默认为 defer。 | | async | boolean | 是否设置异步加载,默认:false 。 |

示例效果

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
    <script
      type="text/javascript"
      src="https://unpkg.com/vue@3/dist/vue.global.js"
      defer="defer"
    ></script>
    <script
      type="text/javascript"
      src="https://unpkg.com/vue-router@4"
      defer="defer"
    ></script>
    <script
      type="module"
      crossorigin=""
      src="/assets/index-25c071f5.js"
    ></script>
    <link rel="stylesheet" href="/assets/index-eb1c0fa2.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>