A comprehensive template for creating Vue 3 plugins with TypeScript, testing, and development tools pre-configured.
- β Vue 3 support with TypeScript
- β Local playground for testing your plugin during development
- β Unit testing with Vitest and Vue Test Utils
- β Linting with ESLint and Prettier
- β GitHub Actions CI/CD pipeline
- β Build configuration for npm package publishing
- β Example components and composables
git clone https://github.com/monterail/vue-plugin-template.git
cd vue-plugin-templatenpm installUpdate the following files:
package.json- Change name, description, author, etc.src/index.ts- Rename your plugin and customize functionalitysrc/components/- Add or modify componentssrc/composables/- Add or modify composablesREADME.md- Update documentation for your plugin
# Run the playground for local development
npm run dev
# Or run tests in watch mode
npm run test:watch// src/index.ts
import type { App, Plugin } from 'vue'
export interface MyPluginOptions {
// Your options here
}
const MyPlugin: Plugin = {
install(app: App, options: MyPluginOptions = {}) {
// Register components
app.component('MyComponent', MyComponent)
// Add global properties
app.config.globalProperties.$myPlugin = {
// Your global methods
}
// Provide data
app.provide('myData', someData)
}
}
export default MyPlugin// Register globally
app.component('MyComponent', MyComponent)
// Register with prefix
const prefix = options.prefix || 'My'
app.component(`${prefix}Component`, MyComponent)app.config.globalProperties.$myPlugin = {
greet: (name: string) => `Hello, ${name}!`
}
// TypeScript types
declare module '@vue/runtime-core' {
export interface ComponentCustomProperties {
$myPlugin: {
greet: (name: string) => string
}
}
}// src/composables/useMyPlugin.ts
import { ref, computed } from 'vue'
export function useMyPlugin() {
const count = ref(0)
const double = computed(() => count.value * 2)
const increment = () => count.value++
return { count, double, increment }
}Once published, users can install and use your plugin:
npm install your-plugin-name// main.ts
import { createApp } from 'vue'
import App from './App.vue'
import MyPlugin from 'your-plugin-name'
const app = createApp(App)
app.use(MyPlugin, {
// Plugin options
})
app.mount('#app')<!-- Using in components -->
<template>
<MyComponent />
</template>
<script setup>
import { useMyPlugin } from 'your-plugin-name'
const { count, increment } = useMyPlugin()
</script>Before publishing your plugin to npm, make sure you've:
-
Updated
package.jsonwith your plugin details:- Change
nameto your package name - Update
version,description,author, andkeywords - Set
repositoryURL if applicable
- Change
-
Built your plugin:
npm run build
-
Tested thoroughly:
npm test -
Log in to npm (if not already logged in):
npm login
-
Publish to npm:
npm publish
Note: The
prepublishOnlyscript will automatically run the build before publishing.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
Built with:
Happy plugin building! π
If you find this template helpful, please give it a βοΈ on GitHub!