Plop 【一个小而美的脚手架工具】

Plop 简介

是一个前端脚手架工具,最大的好处是通过输入命令创建相同模板的css,js,提高重复操作的效率

 Plop 的基本使用

基于一个react 项目 

  • yarn add plop --dev  将 plop 安装在项目中
yarn add plop --dev
  • 在项目根目录新建一个 plopfile.js 文件
     
    // Plop 入口文件,需要导出一个函数
    // 此函数接收一个 plop 对象,用于创建生成器任务
    
    module.exports = plop => {
      plop.setGenerator('component', {
        description: 'create a component',
        prompts: [
          {
            type: 'input',
            name: 'name',
            message: 'component name',
            default: 'MyComponent'
          }
        ],
        actions: [
          {
            type: 'add', // 代表添加文件
            path: 'src/components/{{name}}/{{name}}.js',
            templateFile: 'plop-templates/component.hbs'
          },
          {
            type: 'add', // 代表添加文件
            path: 'src/components/{{name}}/{{name}}.css',
            templateFile: 'plop-templates/component.css.hbs'
          },
          {
            type: 'add', // 代表添加文件
            path: 'src/components/{{name}}/{{name}}.test.js',
            templateFile: 'plop-templates/component.test.hbs'
          }
        ]
      })
    }

     

  • 新建 plop-templates 文件夹,在文件夹新建 component.hbs , component.css.hbs,component.test.js.hbs
  • 在这里插入图片描述 
  • component.hbs:
  • //component.hbs
    
    import React from 'react';
    
    export default () => (
      <div className="{{name}}">
        <h1>{{name}} Component</h1>
      </div>
    )

    文件内容举例:
    component.css.hbs 

    //component.css.hbs
    
    .{{name}} {
    
    }
    

    component.test.hbs 

    //component.test.js.hbs
    
    import React from 'react';
    import ReactDOM from 'react-dom';
    import {{name}} from './{{name}}';
    
    it('renders without crashing', () => {
      const div = document.createElement('div');
      ReactDOM.render(<{{name}} />, div);
      ReactDOM.unmountComponentAtNode(div);
    });
    

     

  • yarn plop component 创建了一个footer模板

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值