Writing a plopfile: Define Your First Generator
🎯 Introduction
In this article, you will learn the structure of plopfile.mjs, Plop's main configuration file, define a complete generator, and run it from the command line.
🧱 What plopfile.mjs Does
plopfile.mjs is Plop's entry-point configuration file. It exports a default function that Plop calls at startup, passing in the plop instance.
Here is its simplest form:
export default function (plop) {
// Register generators, helpers, and other extensions here
}
This function typically handles three tasks:
- Register generators: Define runnable tasks with
plop.setGenerator(). - Register helpers: Extend the template syntax with
plop.setHelper(). - Register partials: Define reusable template fragments with
plop.setPartial().
This lesson focuses on the most common task: registering generators.
✨ Defining a Generator with setGenerator
setGenerator takes two arguments: the generator name and a configuration object.
plop.setGenerator('component', {
description: 'Create a Vue component',
prompts: [
// Interactive questions
],
actions: [
// Actions to execute
]
});
The configuration object has three core fields:
| Field | Purpose |
|---|---|
description | A description shown when you run npx plop |
prompts | An array of question objects |
actions | An array of action objects |
🛠 A More Complete Generator
Let's define a generator that creates an API file:
export default function (plop) {
plop.setGenerator('api', {
description: 'Create an API file',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the API function name (e.g. getUser)'
}
],
actions: [
{
type: 'add',
path: 'src/api/{{camelCase name}}.js',
templateFile: 'plop-templates/api.hbs'
}
]
});
}
Here is the corresponding template, plop-templates/api.hbs:
import request from '@/utils/request';
export function {{camelCase name}}(params) {
return request({
url: '/{{kebabCase name}}',
method: 'get',
params
});
}
Run:
npx plop api
Enter getUser to generate src/api/getUser.js:
import request from '@/utils/request';
export function getUser(params) {
return request({
url: '/get-user',
method: 'get',
params
});
}
camelCase converts input to camelCase, while kebabCase converts it to kebab-case. This lets a single input adapt automatically to different naming conventions.⚡ Defining Multiple Generators
Projects usually need to generate more than one kind of file. You can register multiple generators in plopfile.mjs:
export default function (plop) {
plop.setGenerator('component', {
description: 'Create a Vue component',
prompts: [{ type: 'input', name: 'name', message: 'Component name' }],
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}.vue',
templateFile: 'plop-templates/component.hbs'
}
]
});
plop.setGenerator('api', {
description: 'Create an API file',
prompts: [{ type: 'input', name: 'name', message: 'API function name' }],
actions: [
{
type: 'add',
path: 'src/api/{{camelCase name}}.js',
templateFile: 'plop-templates/api.hbs'
}
]
});
plop.setGenerator('page', {
description: 'Create a page file',
prompts: [{ type: 'input', name: 'name', message: 'Page name' }],
actions: [
{
type: 'add',
path: 'src/views/{{pascalCase name}}Page.vue',
templateFile: 'plop-templates/page.hbs'
}
]
});
}
When you run npx plop without arguments, Plop lists all available generators for you to choose from:
$ npx plop
? [PLOP] Please choose a generator (Use arrow keys)
❯ component - Create a Vue component
api - Create an API file
page - Create a page file
🧰 Command-Line Tips
Alongside interactive selection, you can specify the generator and arguments directly on the command line:
# Specify a generator
npx plop component
# Pass arguments directly to skip prompts
npx plop component -- --name UserInfo
-- are passed to Plop. --name corresponds to the value of a prompt's name field.❓ Questions and Answers
Q1: Can I rename plopfile.mjs to plopfile.js?
Yes, but you need to set "type": "module" in package.json. Otherwise, Node.js parses it as CommonJS and export default causes an error.
Q2: Can generators share the same name?
Avoid duplicate names. If you register a generator with an existing name, the later registration overwrites the earlier one.
Q3: Is description required?
No, but it is recommended. When you run npx plop, the description helps you and your teammates quickly identify what each generator does.
Q4: Can one plopfile.mjs contain multiple setGenerator calls?
Yes. Keeping commonly used generators for a project in one configuration file makes them easier to maintain.
🧪 Exercise
Complete these tasks in the plop-demo project you created earlier:
- Add a generator named
utiltoplopfile.mjs. - Have it ask for a utility function name and generate a file at
src/utils/{{camelCase name}}.js. - Complete the path and variables in the configuration below:
plop.setGenerator('util', {
description: 'Create a utility function',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the utility function name'
}
],
actions: [
{
type: 'add',
// Complete the path here
templateFile: 'plop-templates/util.hbs'
}
]
});
🎉 You now know the basics of configuring Plop generators! Next, we will explore prompts to make your generators ask more flexible questions.
