Skip to main content

Front-End Development

Plop.js – A micro-generator framework: Introduction and Installation (Part-1)

Istock 2012746941

We all may have come across this situation countless times in our projects—copying and pasting files just to create a new component, page, or service. Unfortunately, this slows us down and hampers our productivity by introducing errors into the workflow. However, there’s a solution! Plop.js is the answer to this problem, as it automates these tasks and allows us to focus on writing great code.

What is Plop.js?

Plop.js is a simple yet powerful scaffolding tool—in other words, a micro-generator framework—that helps us automate repetitive coding tasks for projects. As a result, it saves time, reduces errors, and standardizes code structures. Moreover, it ensures uniformity across files, making life easier for the entire team.

Highlight Features:

  • First and foremost, template-based file generation.
  • Additionally, an interactive CLI enabling a smoother user experience.
  • Lastly, achieving extensibility through custom actions.

Installation of Plop.js

Plop.js can be installed in any of your projects. To illustrate this, let’s take an example of a Next.js project.

Step 1: Create a Next.js Project

To begin with, create a Next.js project using the following command:

Nextjs Installation Cli

As a result, the above CLI command will prompt you with further questions for setting up your Next.js project.
(Select answers as per your requirement):

  • What is your project named? my-app
  • Would you like to use TypeScript? No / Yes
  • Would you like to use ESLint? No / Yes
  • Would you like to use Tailwind CSS? No / Yes
  • Would you like your code inside a `src/` directory? No / Yes
  • Would you like to use App Router? (recommended) No / Yes
  • Would you like to use Turbopack for `next dev`? No / Yes
  • Would you like to customize the import alias (`@/*` by default)? No / Yes

Step 2: Install Plop.js

Once your Next.js project is set up, navigate to the project folder and install Plop.js using the command below:

Install Plop

This installation generates three key files:

  1. A node_modules folder for all your libraries, packages, and third-party code.
  2. A package.json file which will give you a starting point for your scripts.
  3. A package-lock.json file, which will lock down the versions for each package.
    Plop Project Scaffolding

In addition to this, installing Plop globally is optional but recommended:

Install Plop Globally

Step 3: Create plopfile.js

Next, create a plopfile.js at the root of your project. Below is a very basic example of plopfile.js

Plopfile Js Config

module.exports = function (plop) {
  plop.setGenerator("basics", {
    description: "My first Plop generator",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "What is the name of your component?",
      },
    ],
    actions: [
      {
        type: "add",
        path: "./components/{{pascalCase name}}.js",
        templateFile: "templates/component.hbs",
      },
    ],
  });
};

Breaking Down the Code:

  • The “setGenerator” creates a generator of the plop. Here, this plopfile.js has a single generator called “basics”.
  • The “description” as the name suggests, describes the purpose of the generator.
  • The “prompts” is an array of prompts. This could be added for your created generator.
  • The “actions” takes the information that the user provided to each prompt. It is an array of where each action is an object. This is an important step and requires creating some templates.

Step 4: Add a Script to package.json

Before running Plop, add the following script (highlighted in the screenshot below) to package.json

Generate Plop Script

Step 5: Run plop

And lastly, run plop through CLI command “npm run generate

Run Generate Script

At this point, Plop will execute and guide you through the component creation process!


What’s Next?

So far, we’ve covered the introduction and installation of Plop.js along with a basic skeleton for plopfile.js.
In the next part Plop.js Template Creation, we will dive deeper into plopfile.js, replace the skeleton code with working code, and create our first real template. Stay tuned!

 

Thoughts on “Plop.js – A micro-generator framework: Introduction and Installation (Part-1)”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Rasika Senad

Rasika Senad is a lead technical consultant at Perficient with over 12 years of experience in front-end development. She has been with Perficient for the past 10 years, actively contributing to various organizational and practice-level initiatives. Beyond work, Rasika is a wife and mother to twin daughters. She enjoys music, dancing, cooking, celebrating festivals, and cherishing time with family and friends.

More from this Author

Follow Us