1 Star 3 Fork 0

李涵 / vite-plugin-inject-externals

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

vite-plugin-inject-externals

简体中文 | English

在vite打包时将指定的包改为从CDN引入。

script标签和link标签可以注入到指定位置。

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

npm package npm downloads/month npm minified size github release License

安装

通过npm下载

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

基本用法

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

export default {
  plugins: [
    injectExternals({
      // 默认 'head-prepend'
      // 自定义注入位置将会替换index.html中的对应文本
      injectTo: '<!-- Custom placeholder for vite plugin inject externals -->',
      modules: [
        {
          name: 'vue',
          global: 'Vue',
          path: 'https://unpkg.com/vue@3.2.19/dist/vue.global.prod.js'
        },
        {
          name: 'axios',
          global: 'axios',
          // 如果有name和global,但是没有path和htmltag,会直接替换全局变量,但是不注入script标签
          // path: 'https://cdn.jsdelivr.net/npm/axios@0.22.0/dist/axios.min.js'
        },
        {
          name: 'md-editor-v3',
          global: 'MdEditorV3',
          path: 'https://cdn.jsdelivr.net/npm/md-editor-v3@1.5.0/lib/md-editor-v3.umd.js',
          injectTo: '<!-- example2 -->'
        },
        // 如果有path, 但是没有global, 会注入link标签
        {
          name: 'md-editor-v3/lib/style.css',
          // 如果有name,但是没有global,会删除掉name的导入,仅适用于裸导入(import 'md-editor-v3/lib/style.css')
          path: 'https://cdn.jsdelivr.net/npm/md-editor-v3@1.5.0/lib/style.css',
        }
      ]
    })
  ],
}

效果

// 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)
<!-- 注入CDN链接 -->
<script type="text/javascript" src="https://unpkg.com/vue@3.2.19/dist/vue.global.prod.js"></script>

延伸用法

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

export default {
  plugins: [
    injectExternals({
      // 默认 'head-prepend'
      // 自定义注入位置将会替换index.html中的对应文本
      injectTo: '<!-- Custom placeholder for vite plugin inject externals -->',
      modules: [
        {
          name: 'vue',
          // 当导入方式是裸导入时(import 'md-editor-v3/lib/style.css'),并且有name('md-editor-v3/lib/style.css')但是没有global,会删除导入
          // 当导入方式不是裸导入时,并且有name和global,会替换全局变量
          global: 'Vue',
          // 有path时,如果name和global, 会注入script标签
          // 有path时, 如果没有global, 会注入link标签
          path: 'https://unpkg.com/vue@3.2.19/dist/vue.global.prod.js',
          // 自定义html标签,优先级比path高
          htmlTag: {
            tag: 'script',
            attrs: {
              type: 'text/javascript',
              crossorigin: '',
              src: 'https://unpkg.com/vue@3.2.19/dist/vue.global.prod.js'
            }
          },
          // 如果module没有injectTo, 默认上层的injectTo
          injectTo: '<!-- Custom1 -->'
        }
      ]
    })
  ],
}

InjectExternalsConfig

Name Required Desc Type Default
command false 在运行哪个命令时注入html标签,build表示在打包时注入,true表示build和serve命令都注入 'build' / true 'build'
injectTo false 生成的html标签注入到什么位置 'head' / 'body' / 'head-prepend' / 'body-prepend' / string 'head-prepend'
modules true 模块配置 InjectExternalsModule[] []

InjectExternalsModule

Name Required Desc Type Default
name false 模块名 string
global false 全局变量 string
path false js或者css资源的cdn链接,如果没有global,表示是css资源 string
htmlTag false 自定义html标签,优先级比path高 HtmlTag
injectTo false 生成的html标签注入到什么位置 string InjectExternalsConfig.injectTo

HtmlTag

Name Required Desc Type Default
tag true 标签名 string
attrs false 属性({ 属性名: 属性值 }) object

开源许可证

MIT

MIT License Copyright (c) 2021 李涵 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

vite打包时,指定的模块将从CDN导入。script和link标签可以插入到指定位置。 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/lihanspace/vite-plugin-inject-externals.git
git@gitee.com:lihanspace/vite-plugin-inject-externals.git
lihanspace
vite-plugin-inject-externals
vite-plugin-inject-externals
main

搜索帮助