前端自动化构建工具——Plop的基本使用

一款小而美的脚手架工具,一般集成在项目中使用,用于自动化创建重复类型文件

一、安装

# 将plop模块作为项目开发依赖安装
yarn add plop --dev

二、使用

  1. 在项目根目录下创建 plopfile.js
module.exports = plop => {
    // component ---> 为运行命令
    plop.setGenerator('component', {
        // 描述
        description: 'create a component',
        // 询问,用户交互 (api参照:Inquirer.js)
        prompts: [
            {
                type: 'input',
                name: 'name',
                message: 'component name',
                default: 'MyComponent'
            }
        ],
        // 执行的动作
        actions: [
            {
                type: 'add', // 代表添加文件
                path: 'src/components/{{name}}/{{name}}.vue',
                templateFile: 'plop-templates/xxx.hbs'
            }
        ]
    })
}
  1. 创建模版
    一般是在项目根目录下创建 plop-templates 文件夹
    内部再创建 hbs模板(Handlebars模板语法) 中文版
<template>
    <div class="{{ name }}">{{ name }}</div>
</template>

<script>
export default {
    name: '{{ name }}',
}
</script>

<style lang="less" scoped>
.{{name}} {

}
</style>
  1. 运行cli命令生成对应模版文件
yarn plop component

到此结束,自动在src/components/目录下生成了对应模版的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hh_self

您的鼓励将是我原创最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值