Continuing our Plop.js journey from the last blog. Be sure to go back and read the previous installment in this series.
In our previous discussion, we explored an introduction to Plop.js and its installation in a Next.js project. Additionally, we looked at a basic skeleton of plopfile.js.
As we saw earlier, the plopfile.js consists of key elements that define the generation. Let’s break them down again for clarity:
Before creating a template, understand the concept of actions inside “setGenerator.” After all, this is where the real magic happens. Let’s write a generator to create a new component.
plop.setGenerator("component", { description: "Create a new React component", prompts: [ { type: "input", name: "name", message: "What is this component’s name?", }, ], actions: [ { type: "add", path: "src/components/{{pascalCase name}}/{{pascalCase name}}.tsx", templateFile: "plop-template/component.hbs", }, ], });
Apart from “add”, there are several other built-in actions like:
You can explore the complete list here: Plop.js Built-in Actions.
Now that we understand actions, let’s organize our template files.
In Handlebars, variables are enclosed within double curly braces {{}}. Moreover, built-in helpers like “pascalCase” allow the formatting of variables.
const {{pascalCase name}} = () => { return <div>{{pascalCase name}} Component</div>; }; export default {{pascalCase name}};
In addition to “pascalCase,” you can also use:
Check out the complete list here: Plop.js Built-in Helpers.
After setting everything up, we are now ready to run our generator! There are two ways to do this:
Alternatively, you can open the package.json file, hover over “generate script,” and click “Run Script” in your editor.
Next, let’s create our first real component, “Button,” using the plop command npm run generate (with either of the two options mentioned above). After you run the command, the terminal will show prompts as mentioned in the plopfile.js
This will prompt you with questions as per plopfile.js, such as:
Once you provide the inputs (refer to the above screenshot to understand better), the component gets created at the specified location, and you will see a success message in the terminal.
As you can see, Plop.js simplifies component creation by automating file generation and reducing repetitive tasks. By setting up structured templates, we ensure consistency and boost productivity across the project.
In the upcoming blog, we will explore:
So, stay tuned!
]]>We may all have encountered 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.
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. It saves time, reduces errors, and standardizes code structures. Moreover, it ensures uniformity across files, making life easier for the entire team.
Plop.js can be installed in any of your projects. To illustrate this, let’s take an example of a Next.js project.
To begin with, create a Next.js project using the following command:
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):
Once your Next.js project is set up, navigate to the project folder and install Plop.js using the command below:
In addition to this, installing Plop globally is optional but recommended:
Next, create a plopfile.js at the root of your project. Below is a very basic example of plopfile.js
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", }, ], }); };
Before running Plop, add the following script (highlighted in the screenshot below) to package.json.
Lastly, run plop through the CLI command “npm run generate.”
Now, Plop will execute and guide you through the component creation process!
So far, we’ve covered the introduction and installation of Plop.js and a basic skeleton for plopfile.js.
In the next part, Plop.js Template Creation, we will explore plopfile.js more thoroughly, replace the skeleton code with working code, and create our first real template. Stay tuned!