Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 1.87 KB

plugin.md

File metadata and controls

97 lines (72 loc) · 1.87 KB

Plugin

编写插件

使用命令行创建项目,按提示完成项目的创建

pnpm create vite

在项目更目录下创建plugin目录,然后创建名为test的js文件

在文件内默认导出一个js函数,该函数将作为插件函数使用

export default function myPlugin(options = {}) {}

在vite的配置文件中使用

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import test from './plugins/test.js'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), test()]
})

现在来在插件函数中编写具体的代码。该函数的返回值会作为具体的插件配置交给vite,有以下几个常见的属性

export default function myPlugin(options = {}) {
  return {
    name: 'my-plugin', // 插件名称

    // 在 Vite 构建开始时调用
    buildStart() {},

    // 在每个模块被加载时调用
    transform(code, id) {},

    // 在 Vite 开发服务器启动时调用
    configureServer(server) {}
  }
}

下面增加一个功能,将js文件中的console.log修改为console.debug,这主要在transform中进行操作

    transform(code, id) {
      if (id.endsWith(".js")) {
        return {
          code: code.replace("console.log", "console.debug"),
          map: null,
        };
      }
    },

随便编写一个测试js文件

const testA = () => {
  console.log(11)
}

export { testA }

在组件中使用中引入

<script setup lang="ts">
import { ref } from 'vue'
import { testA } from '../c.js'

defineProps<{ msg: string }>()

const count = ref(0)
</script>

<button
  type="button"
  @click="
    () => {
      testA()
      count++
    }
  "
>count is {{ count }}</button>

重新构建后再页面上点击按钮,在控制台中会看到该函数的输出,这样就完成了一次插件的开发。