Writing a plopfile: Define Your First Generator

Learn the structure of plopfile.mjs, define a generator with setGenerator, and run it from the command line.

🎯 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:

plopfile.mjs
export default function (plop) {
    // Register generators, helpers, and other extensions here
}

This function typically handles three tasks:

  1. Register generators: Define runnable tasks with plop.setGenerator().
  2. Register helpers: Extend the template syntax with plop.setHelper().
  3. 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.

plopfile.mjs
plop.setGenerator('component', {
    description: 'Create a Vue component',
    prompts: [
        // Interactive questions
    ],
    actions: [
        // Actions to execute
    ]
});

The configuration object has three core fields:

FieldPurpose
descriptionA description shown when you run npx plop
promptsAn array of question objects
actionsAn array of action objects

🛠 A More Complete Generator

Let's define a generator that creates an API file:

plopfile.mjs
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:

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:

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:

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
Arguments after -- 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:

  1. Add a generator named util to plopfile.mjs.
  2. Have it ask for a utility function name and generate a file at src/utils/{{camelCase name}}.js.
  3. Complete the path and variables in the configuration below:
plopfile.mjs
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.