Practice: Generate Vue Components with Plop
🎯 Introduction
In this article, you will bring together what you have learned about Plop in a complete practical example. We will build a Vue component generator that creates component, style, and test files based on your choices.
🧱 Goals
Our generator will:
- Ask for the component name.
- Ask whether to use
<script setup>. - Ask whether to include a style file.
- Ask whether to include a test file.
- Generate the corresponding files based on the answers.
✨ Project Structure
The completed project has this structure:
plop-demo/
├── plopfile.mjs
├── plop-templates/
│ ├── component.hbs
│ ├── component.scss.hbs
│ └── component.test.hbs
└── src/
├── components/
└── components/__tests__/
🚀 Step 1: Write plopfile.mjs
export default function (plop) {
plop.setHelper('now', function () {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
});
plop.setGenerator('vue-component', {
description: 'Create a complete Vue component',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the component name (PascalCase)',
validate: function (value) {
if (value.trim() === '') {
return 'Component name cannot be empty';
}
if (!/^[A-Z][a-zA-Z0-9]*$/.test(value)) {
return 'Component name must start with an uppercase English letter';
}
return true;
}
},
{
type: 'confirm',
name: 'useSetup',
message: 'Use <script setup> syntax?',
default: true
},
{
type: 'confirm',
name: 'hasStyle',
message: 'Generate a style file?',
default: true
},
{
type: 'confirm',
name: 'hasTest',
message: 'Generate a test file?',
default: false
}
],
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
},
{
type: 'add',
path: 'src/components/__tests__/{{pascalCase name}}.test.js',
templateFile: 'plop-templates/component.test.hbs',
skip: function (answers) {
if (!answers.hasTest) {
return 'Skipping test file generation';
}
}
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}.scss',
templateFile: 'plop-templates/component.scss.hbs',
skip: function (answers) {
if (!answers.hasStyle) {
return 'Skipping style file generation';
}
}
}
]
});
}
🛠 Step 2: Write the Templates
Component Template
<!--
Component: {{pascalCase name}}
Created on: {{now}}
-->
<template>
<div class="{{kebabCase name}}">
{{pascalCase name}} component
</div>
</template>
{{#if useSetup}}
<script setup>
// {{pascalCase name}} component logic
</script>
{{else}}
<script>
export default {
name: '{{pascalCase name}}'
};
</script>
{{/if}}
{{#if hasStyle}}
<style scoped>
@import './{{pascalCase name}}.scss';
</style>
{{/if}}
Style Template
.{{kebabCase name}} {
/* {{pascalCase name}} component styles */
}
Test Template
import { describe, it, expect } from 'vitest';
import { mount } from '@vue/test-utils';
import {{pascalCase name}} from '../{{pascalCase name}}.vue';
describe('{{pascalCase name}}', () => {
it('renders successfully', () => {
const wrapper = mount({{pascalCase name}});
expect(wrapper.exists()).toBe(true);
});
});
💡 Step 3: Run the Generator
Run the command:
npx plop vue-component
Answer the prompts:
? Enter the component name (PascalCase) UserInfo
? Use <script setup> syntax? Yes
? Generate a style file? Yes
? Generate a test file? Yes
The generator creates the following files:
src/components/
├── UserInfo.vue
├── UserInfo.scss
└── __tests__/
└── UserInfo.test.js
⚡ Step 4: Inspect the Generated Component
<!--
Component: UserInfo
Created on: 2026-07-16
-->
<template>
<div class="user-info">
UserInfo component
</div>
</template>
<script setup>
// UserInfo component logic
</script>
<style scoped>
@import './UserInfo.scss';
</style>
<style> block uses @import './UserInfo.scss', allowing the style and component files to be maintained separately. If you prefer inline styles, put the SCSS content directly in the component template.❓ Questions and Answers
Q1: Why put test files in __tests__?
This is a common convention in frontend projects and helps test tools discover and collect test cases.
Q2: Why does the component template still need {{#if hasStyle}} if the user chooses not to include styles?
skip only controls whether the .scss file is generated. In the component template, hasStyle controls whether the <style> block appears. Both are needed to keep the generated output consistent.
Q3: How does <script setup> differ from a traditional <script> block?
<script setup> is syntactic sugar in Vue 3 that makes code more concise. For Vue 2 projects, choose the traditional syntax.
Q4: Can I use this generator in a real project?
Yes, as a starting point. You may need to adapt it to your team's directory conventions, test framework, and styling approach.
🧪 Exercise
Extend the practical example with one more feature:
When the component name is UserCard, automatically generate a props definition containing the title and desc props.
Hint: Add a confirm question to prompts and use {{#if}} in the template.
{{#if hasProps}}
<script setup>
const props = defineProps({
title: String,
desc: String
});
</script>
{{/if}}
🎉 You have completed the Plop course! You can now use Plop in your own projects to generate common files automatically and speed up development.
