Generate a Vue component using Plop

Hi, my name is Tina! I am a Full Stack Software Developer and Tech Lead from Canada. 🙋🏻♀️
Search for a command to run...

Hi, my name is Tina! I am a Full Stack Software Developer and Tech Lead from Canada. 🙋🏻♀️
Have you ever installed a package by using go install <some-package> and wondered how to uninstall it? Go is really great in that respect. Go packages are a single binary and they’re installed based on how your Go tooling is set up. Provided we have ...

Overview This is the 3rd article in a series of posts about working with Open API to create generated API clients and documentation websites. In the 1st post we went over what we can do with Open API spec files, e.g. generate API clients and documen...

Overview This article is the 2nd in a series of blog posts that discuss using Open API to help with API documentation and API client code generation. In the previous article, we went over how we can generate documentation and API clients using existi...

Overview This article starts a series of Open API-related blog posts. OpenAPI, previously known as Swagger (a name still in use today for many of its tools), is a suite of tools that generate documentation and networking clients for various programmi...

You've probably already seen the scary alert (that's possibly why you're here), but in case you haven't, it looks something like this: NewAppIJustDownloaded pasted from your clipboard Sometimes it h

Plop is a handy tool for code generation. It uses Handlebars under the hood alongside some provided Handlebars helper functions.
By using a code generation tool like plop, you can avoid writing tedious boilerplate code and be productive right away. It's also great for maintaining consistency across the project and company.
Updated 4 Nov 2020: Added template for Vue 3 syntax.
To make sure you're set up to generate components using Plop, you'll need to have a Vue.js project. You can generate one with Vue CLI or with Nuxt.
Run the following to add Plop as a dependency:
npm install --save-dev plop
Add a command to call plop from an NPM script, e.g.
{
"name": "example-project",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "nuxt-ts start",
"plop": "plop"
}
}
Create a file named plopfile.js in the root of your project. Put the following code in it:
// plopfile.js
module.exports = function (plop) {
plop.setGenerator('component', {
description: 'Generates a Vue component',
prompts: [
{
type: 'input',
name: 'component',
message: 'Component name, e.g. NatureBanner'
}
],
actions: [
{
type: 'add',
path: 'components/{{pascalCase component}}/{{pascalCase component}}.vue',
templateFile: 'plop-templates/component.hbs'
}
]
})
}
You'll notice the above plopfile references a template file plop-templates/component.hbs. You will need to create this file.
<template>
<div class="{{kebabCase component}}">{{pascalCase component}}</div>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: '{{pascalCase component}}',
props: {}
})
</script>
<style lang="scss">
.{{kebabCase component}} {}
</style>
For Vue 3, we don't need to import or extend Vue:
<template>
<div class="{{kebabCase component}}">{{pascalCase component}}</div>
</template>
<script lang="ts">
export default {
name: '{{pascalCase component}}',
props: {}
};
</script>
<style lang="scss">
.{{kebabCase component}} {}
</style>
These are the differences:


Above diffs generated with TextDiffer.
Note: If you're using Nuxt (or any project set up to use Prettier), you will likely need to disable the "helpful" Prettier JS formatting to avoid destroying your Handlebars file.

Once you've added all your files, your project should look something like this:
plop-vue-example/
├── package.json
├── plop-templates
│ └── component.hbs
└── plopfile.js
1 directory, 3 files
Now all you need to do is run your script to generate a component:
npm run plop
You will be prompted for the component name. Enter it.
You should now have generated a component in the project's component directory:
components/
├── Logo.vue
├── NatureBanner
│ └── NatureBanner.vue
└── README.md
1 directory, 3 files
If you open the file, you should see a Vue component that uses Sass and TypeScript was generated.
<template>
<div class="nature-banner">NatureBanner</div>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'NatureBanner',
props: {}
})
</script>
<style lang="scss">
.nature-banner {}
</style>
You can see an example without Sass or TypeScript here.