Logo

dev-resources.site

for different kinds of informations.

Get rid of Copy/Paste with Plop Js!

Published at
2/15/2024
Categories
plopjs
javascript
nextjs
codegeneration
Author
melihs
Author
6 person written this
melihs
open
Get rid of Copy/Paste with Plop Js!

Hello,

Almost all of us, while working on a project, are copy/pasting from the previous one while creating similar structures. While thinking if there is a fast way to do this, I came across plop js.

Plop js actually allows us to create the structures that we have previously created templates on cli via command. It does this in a very simple way. I can give hygen and yeoman as an alternative to plop js. I plan to write content about these libraries in the future.

What Are the Conveniences It Provides Us?

  • Saves time.
  • Saves from writing repetitive code blocks and folder structures every time.
  • Helps people working in the project to create structures in the same standard. So it provides code integrity.

Let’s reinforce them by writing a sample project. In our scenario, we will create a component whose name we will determine, a file where we determine the types contained in this component, and a style file with the help of a command.

Let’s create a next js project from scratch. project from scratch.



npx create-next-app@latest


Enter fullscreen mode Exit fullscreen mode

First, let’s include plop js in our project with yarn.



yarn add plop -D


Enter fullscreen mode Exit fullscreen mode

Those who use npm can use this.



npm install --save-dev plop


Enter fullscreen mode Exit fullscreen mode

To run our Plop script from the terminal, let’s add it to our “package.json” file.



{  
 ...
  "scripts": {    
    ...,
    "plop": "plop"
  },
  ...
}


Enter fullscreen mode Exit fullscreen mode

It’s time to create our code template. To do this, we use handlebars js, which allows us to create templates at a basic level. We create a folder called templates in the project home directory and add our template files inside.

template/{{name}}.tsx.hbs



import { {{pascalCase name}}Props } from "./{{name}}";
import  "./{{name}}.css";

const {{name}} = ({ className="" }:{{pascalCase name}}Props) => {

  return (
    <div className={`{{name}} ${className}`}>
      {{name}} component
    </div>
  );
};

export default {{name}};


Enter fullscreen mode Exit fullscreen mode

template/{{name}}.ts.hbs



export interface {{pascalCase name}}Props {
 className?: string;
}


Enter fullscreen mode Exit fullscreen mode

template/{{name}}.css.hbs



.{{name}} {
  background-color: "#FF5733";
}


Enter fullscreen mode Exit fullscreen mode

When we have completed our template files, we can create a file called “plopfile.js” in the main directory of our project.

plopfile.js



module.exports = function (plop) {
  plop.setGenerator("component", {
    description: "this is a skeleton plopfile",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "Enter component name:"
      },
    ],
    actions: [
      {
        type: "addMany",
        destination: "app/components/{{camelCase name}}",
        templateFiles: "templates/*.hbs",
        base: "templates",
      },
    ]
  });
};


Enter fullscreen mode Exit fullscreen mode
  • description: Describes what the command does.
  • prompts: The part where we define the questions asked to the user.
  • actions: The part that defines the operation that the command will execute.
  • actions types: is used to define the type of the function. For example, in our example, we create 3 files with a single command. We do this with the action of type addMany. The add type is used to add a single file. If we want to modify the file we use modify.

Finally, let’s run our command:



yarn plop


Enter fullscreen mode Exit fullscreen mode

Image description

Image description

You can access the source codes of the sample project from this link. 🔗

Continue with content 🚀

You can access my other content by clicking this link. I would be very happy if you like and leave a comment 😇

codegeneration Article's
30 articles in total
Favicon
Anvil: An attempt of saving time
Favicon
Spending Less Time on Boilerplate with Blackbird
Favicon
Boost Your Coding: Easy AI Code Generation Tricks
Favicon
How to Use AI Code Generation to Enhance Developer Productivity
Favicon
Understanding Abstract Syntax Trees
Favicon
Component Generation with Figma API: Bridging the Gap Between Development and Design
Favicon
How to Perform Code Generation with LLM Models
Favicon
Get rid of Copy/Paste with Plop Js!
Favicon
ABP Suite: Best CRUD Page Generation Tool for .NET
Favicon
Generating TypeScript Code for a Dynamic Country Flag React Component
Favicon
Top Free Code Generation tools, APIs, and Open Source models
Favicon
Introduction to Code Generation in Rust
Favicon
Best Code Generation APIs in 2023
Favicon
Crafting Prompt Templates for Code Generation
Favicon
NEW: Code Generation APIs available on Eden AI
Favicon
Declarative code generation in Unity
Favicon
Build a WebAssembly Language for Fun and Profit: Code Generation
Favicon
Using EDMX file and T4 in .NET Core to Generate Code (Entities, DTO, API, Services etc.)
Favicon
Freezed Kullanarak Flutter'da JSON Nasıl Ayrıştırılır? 💫 🌌 ✨
Favicon
Dotnet code generation overview by example
Favicon
Sparky's Tool Tips: NimbleText
Favicon
Coding the code versus coding the code that codes
Favicon
Build an entire React application in one command
Favicon
Ensure auto-generated code is always up-to-date with compile-time assertions in Go
Favicon
Sitecore Code Generation with Unicorn in 2020
Favicon
gosli: a little attempt to bring a bit of LINQ to Golang
Favicon
How to make a code generator in 5 minutes (or less)
Favicon
Adding Contexts via Go AST (Code Instrumentation)
Favicon
How to Add Generated HttpClient to ASP.NET Core Dependency Injection (Right Way)
Favicon
Using code generation to survive without generics in Go

Featured ones: