简体   繁体   English

如何使用 webpack 从 npm build 生成的文件中注册 Vue 插件

[英]How to register Vue plugin from files generated by npm build with webpack

I setup a new Vue project with basic scaffolding using the Vue CLI and Vuetify with tree shaking to eliminate any components I don't need.我使用 Vue CLI 和 Vuetify 设置了一个带有基本脚手架的新 Vue 项目,并使用 tree shaking 来消除任何我不需要的组件。 I'm really only interested in the vendor files that have the Vuetify components (presumably the chunk-vender... files under dist/js and dist/css).我真的只对具有 Vuetify 组件的供应商文件感兴趣(大概是 dist/js 和 dist/css 下的块供应商...文件)。

The HTML in my project is including those vendor files, but I don't understand how to register the plugin with Vue.我的项目中的 HTML 包含那些供应商文件,但我不明白如何使用 Vue 注册插件。 For example, in my app's JS file:例如,在我的应用程序的 JS 文件中:

Vue.use(vuetify); // where do I get the vuetify plugin object from?
new Vue({...})

Just to be clear, I'm not interested in the app JS/CSS files generated by the npm build.为了清楚起见,我对 npm 构建生成的app JS/CSS 文件不感兴趣。 I'm only using that whole setup so that I can manually cherry pick the Vuetify components I want and create the necessary JS/CSS.我只使用整个设置,以便我可以手动挑选我想要的 Vuetify 组件并创建必要的 JS/CSS。

If I include the full-fat Vuetify script/css in my HTML from a CDN eg如果我在来自 CDN 的 HTML 中包含完整的 Vuetify 脚本/css,例如

<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.3.17/vuetify.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.3.17/vuetify.min.css" rel="stylesheet" type="text/css"/>

...then I can initialize Vuetify in my apps JS like: ...然后我可以在我的应用程序 JS 中初始化 Vuetify,例如:

new Vue({
    el: '#my-app',
    vuetify: new Vuetify(),
})

...so what I'm hoping to find out is if there is a way to initialize Vuetify using the vendor JS file produced from the Vue CLI build instead of the full CDN version. ...所以我希望找出是否有一种方法可以使用从 Vue CLI 构建生成的供应商 JS 文件而不是完整的 CDN 版本来初始化 Vuetify。 If I try to use the same initialzation method above when using the vendor build JS file, I get the error Vuetify is not defined .如果我在使用供应商构建 JS 文件时尝试使用上述相同的初始化方法,则会收到错误Vuetify is not defined

There's actually a more idiomatic way to create a library with Vue CLI.实际上有一种更惯用的方式来使用 Vue CLI 创建库。 Your build NPM-script should specify the library build type and an entry point that exports a Vue plugin (eg, src/plugins/vuetify.js ), where only specific Vuetify components are installed.您的build NPM 脚本应指定构建类型和导出Vue 插件(例如src/plugins/vuetify.js )的入口点,其中仅安装特定的 Vuetify 组件。

For example, to create a Vue plugin that installs only VApp (required as root component in Vuetify 2.x), VContainer , VBtn , and VTextField components:例如,要创建一个仅安装VApp (在 Vuetify 2.x 中作为根组件需要)、 VContainerVBtnVTextField组件的 Vue 插件:

  1. Run these commands to generate a Vue CLI project with the default options:运行以下命令以使用默认选项生成 Vue CLI 项目:

     npx @vue/cli create --default my-vuetify cd my-vuetify
  2. Run this command from the project's root directory to add Vuetify to the project (and choose Default preset at prompt):从项目的根目录运行此命令以将 Vuetify 添加到项目中(并在提示时选择默认预设):

     npx @vue/cli add vuetify
  3. Edit the project's build NPM-script in package.json to create a library called MyVuetify (this will be the global variable set when the library is imported in your <script> tag):package.json中编辑项目的build NPM-script 以创建一个名为MyVuetify的库(这将是在您的<script>标签中导入库时设置的全局变量):

     // package.json { "scripts": { // BEFORE: "build": "vue-cli-service build", // AFTER: "build": "vue-cli-service build --target lib --name MyVuetify src/plugins/vuetify.js", } }
  4. In the plugin's entry file, export a vuetify function that constructs a new Vuetify instance:在插件的入口文件中,导出一个构造新Vuetify实例的vuetify函数:

     // src/plugins/vuetify.js import Vuetify from 'vuetify/lib' export const vuetify = options => new Vuetify({ ...options })

    Also export a plugin object, containing an install function that specifies only the Vuetify components of interest:还导出一个plugin对象,其中包含一个仅指定感兴趣的 Vuetify 组件的install函数:

     // src/plugins/vuetify.js import { VApp, VContainer, VBtn, VTextField } from 'vuetify/lib' export const plugin = { install(app, options) { app.use(Vuetify, { components: { // Vuetify 2.x requires `VApp` to be the root component, so `VApp` // needs to be exported unless you prefer the consumer app provided it VApp, VContainer, VBtn, VTextField, }, ...options }) } }
  5. Run this command to build the library:运行以下命令来构建库:

     npm run build

Usage用法

In dist/demo.html from the library build output, load the UMD script ( MyVuetify.umd.js ), which sets a global named MyVuetify , containing the exports from the build output (defined earlier in step 4).在来自库构建输出的dist/demo.html中,加载 UMD 脚本 ( MyVuetify.umd.js ),该脚本设置一个名为MyVuetify的全局变量,其中包含来自构建输出的导出(在前面的步骤 4 中定义)。 Also load the styles ( MyVuetify.css ), which contains the CSS for the used Vuetify components.还加载样式( MyVuetify.css ),其中包含所用 Vuetify 组件的 CSS。

<script src="./MyVuetify.umd.js"></script>
<link rel="stylesheet" href="./MyVuetify.css">

Finally, create a <script> block that installs the plugin from MyVuetify.plugin and initializes the Vue app's vuetify with MyVuetify.vuetify() :最后,创建一个<script>块,从MyVuetify.plugin安装插件并使用MyVuetify.vuetify()初始化 Vue 应用程序的vuetify

<script>
Vue.use(MyVuetify.plugin)

new Vue({
  vuetify: MyVuetify.vuetify(/* Vuetify options (e.g., `theme`) */),
})
</script>

Here's a complete dist/demo.html :这是一个完整的dist/demo.html

<script src="https://unpkg.com/vue@2.6.12/dist/vue.min.js"></script>
<script src="./MyVuetify.umd.js"></script>
<link rel="stylesheet" href="./MyVuetify.css">

<v-app id="app">
  <v-container>
    <form @submit.prevent="greet">
      <v-text-field v-model="name" label="Name"></v-text-field>
      <v-btn type="submit">Greet</v-btn>
    </form>
  </v-container>
</v-app>

<script>
Vue.use(MyVuetify.plugin)

new Vue({
  el: '#app',
  vuetify: MyVuetify.vuetify(),
  data: () => ({
    name: '',
  }),
  methods: {
    greet() {
      alert(`Hi ${this.name.trim() || 'there'}!`)
    }
  }
})
</script>

GitHub demoGitHub 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM