首页 > 技术文章 > Plop的基本使用

phantomyy 2021-02-22 17:49 原文

1、plop的具体使用

(1)将plop作为一个npm模块安装到开发依赖当中

yarn add plop --dev

(2、)项目根目录下新建一个plopfile.js文件

// Plop入口文件,需要导出一个函数
// 此函数接收一个Plop对象,用于创建生成器任务
// setGenerator需要两个参数,第一个参数是项目名字,第二个参数是项目配置
module.exports = plop => {
    plop.setGenerator('component',{
        description:'create a component',
        prompots:[
            {
                type:'input',
                name:'name',
                message:'component name',
                default:'MyCompontent',
            }
        ],
     // 添加多个模板,就在actions中添加多个数组
        actions:[
            {
                type:'add',
                path:'src/components/{{name}}/{{name}}.js',
                templateFile:'plop-templates/component.hbs'
            },
            {
                type:'add',
                path:'src/components/{{name}}/{{name}}.js',
                templateFile:'plop-templates/component.test.hbs'
            }
        ]
    })
}

(3)新建plop-templates文件夹,在此文件夹下新建components.hbs文件,写入模板

import React from 'React';
export default() => (
<div className="{{name}}">
    <h1>{{name}}Component</h1>
</div>
)

(4)通过yarn启动plop程序

yarn plop component

根据模板创建成功

2、plop使用总结

(1)将plop模块作为项目开发依赖安装

(2)在项目根目录下创建一个plopfile.js文件

(2)在plopfile.js文件中定义脚手架任务

(3)编写用于生产特定类型文件的模板

(4)通过plop提供的CLI运行脚手架任务

推荐阅读