其他分享
首页 > 其他分享> > scaffoldingTools

scaffoldingTools

作者:互联网

脚手架工具

脚手架工具概要(前端工程化的发起者)

常用的脚手架工具

  1. React项目:create-react-app
  2. vue.js项目:vue-cli
  3. angular项目:angular-cli

Yeoman(The web's scaffolding tool for modern webapps)

Yeoman 是一种高效、开源的 Web 应用脚手架搭建系统,意在精简开发过程。Yeoman 因其专注于提供脚手架功能而声誉鹊起,它支持使用各种不同的工具和接口协同优化项目的生成。

  1. Yeoman 提供了一种灵活创建、开发、编译和调试 Web 应用的脚手架(scaffolding)软件。
  2. 虽然 Yeoman 本身是 JavaScript 编写的,但适用于任何语言编写的应用。
  3. Yeoman 支持与 Webpack、Babel、TypeScript、React 和 Angular 等多种第三方软件库的无缝集成。
  4. Yeoman 内建立有一个基于 Node.js 的 HTTP 开发服务器,简化了开发环境的设置和开发过程的迭代。
    5.Yeoman 实现构建过程由开发环境到优化后生产环境间的无缝转移。 Yeoman以上简介出自此博客

Yeoman 基本使用

  1. node安装:
    • node版本:v14.15.4
    • npm版本:v6.14.10
    • node版本控制工具:nvm
  2. Yeoman安装:yarn global add yo
  3. 安装对应的generator:yarn global add generator-node
  4. 使用:
    1. 创建目录: mkdir my-module
    2. 进入目录: cd my-module
    3. 运行:yo node

Sub Generator

Yeoman 使用步骤总结

自定义Generator

基于Yeoman搭建自己的脚手架

创建Generator模块

Generator本质上就是一个NPM模块

// 此文件作为Generator的核心入口
// 需要导入一个继承自Yeoman Generator的类型
// Yeoman Generator 在工作时会自动调用我们在此类型中定义的一些生命周期方法
// 我们在这些方法中可以通过调用父类提供的一些工具方法实现一些功能,例如文件写入
// fs.write('绝对路径','文件的内容')


const Generator = require("yeoman-generator");

module.exports = class extends Generator {
    wrting() {
        // Yeoman 自动在生成文件阶段调用此方法
        // 我们这里尝试往项目目录中写入文件
        this.fs.write(
            this.destinationPath('temp.txt'),
            Math.random().toString()
        )
    }
}

根据模版创建文件

  1. 创建一个templates文件夹, 将需要的模板文件放入此目录下
  2. 运行:yo + 模块名
// 此文件作为Generator的核心入口
// 需要导入一个继承自Yeoman Generator的类型
// Yeoman Generator 在工作时会自动调用我们在此类型中定义的一些生命周期方法
// 我们在这些方法中可以通过调用父类提供的一些工具方法实现一些功能,例如文件写入
// fs.write('绝对路径','文件的内容')


const Generator = require("yeoman-generator");

module.exports = class extends Generator {
    wrting() {
        // Yeoman 自动在生成文件阶段调用此方法
        // 我们这里尝试往项目目录中写入文件
        // this.fs.write(
        //     this.destinationPath('temp.txt'),
        //     Math.random().toString()
        // )

        // 通过模版方式写入文件到目标目录
        // fs.copyTpl(模版文件的路径, 输出文件的路径,  模版文件的上下文)
        // 模版文件的路径
        const tmpl = this.templatePath('foo.txt');
        // 输出目标的路径
        const output = this.destinationPath('fll.txt');
        // 模版数据上下文
        const context = { title: 'hell yeoman', success: false };

        this.fs.copyTpl(tmpl, output, context);
    }
}

总结:相对于手动创建每一个文件, 模版的方式大大的提高了效率;

接收用户输入

// 此文件作为 Generator 的核心入口
// 需要导出一个继承自 Yeoman Generator 的类型
// Yeoman Generator 在工作时会自动调用我们在此类型中定义的一些生命周期方法
// 我们在这些方法中可以通过调用父类提供的一些工具方法实现一些功能,例如文件写入

const Generator = require('yeoman-generator')

module.exports = class extends Generator {
  prompting () {
    // Yeoman 在询问用户环节会自动调用此方法
    // 在此方法中可以调用父类的 prompt() 方法发出对用户的命令行询问
    return this.prompt([
      {
        type: 'input',
        name: 'name',
        message: 'Your project name',
        default: this.appname // appname 为项目生成目录名称
      }
    ])
    .then(answers => {
      // answers => { name: 'user input value' }
      this.answers = answers
    })
  }
  writing () {
    // Yeoman 自动在生成文件阶段调用此方法

    // // 我们这里尝试往项目目录中写入文件
    // this.fs.write(
    //   this.destinationPath('temp.txt'),
    //   Math.random().toString()
    // )

    // -------------------------------------------------------

    // // 通过模板方式写入文件到目标目录

    // // 模板文件路径
    // const tmpl = this.templatePath('foo.txt')
    // // 输出目标路径
    // const output = this.destinationPath('foo.txt')
    // // 模板数据上下文
    // const context = { title: 'Hello zce~', success: false }

    // this.fs.copyTpl(tmpl, output, context)

    // -------------------------------------------------------

    // 模板文件路径
    const tmpl = this.templatePath('bar.html')
    // 输出目标路径
    const output = this.destinationPath('bar.html')
    // 模板数据上下文
    const context = this.answers

    this.fs.copyTpl(tmpl, output, context)
  }
}

Vue Generator案列

const Generator = require('yeoman-generator');

module.exports = class extends Generator {
    prompting() {
        return this.prompt([{
            type: "name",
            name: "name",
            message: "Your project name",
            default: this.appname
        }]).then(answers => {
            this.answers = answers;
        })
    }

    writing() {
        // 把每一个文件通过模版转化到目标路径
        const templates = [
            '.browserslistrc',
            '.editorconfig',
            '.env.development',
            '.env.production',
            '.eslintrc.js',
            '.gitignore',
            'babel.config.js',
            'package.json',
            'postcss.config.js',
            'README.md',
            'public/favicon.ico',
            'public/index.html',
            'src/App.vue',
            'src/main.js',
            'src/router.js',
            'src/assets/logo.png',
            'src/components/HelloWorld.vue',
            'src/store/actions.js',
            'src/store/getters.js',
            'src/store/index.js',
            'src/store/mutations.js',
            'src/store/state.js',
            'src/utils/request.js',
            'src/views/About.vue',
            'src/views/Home.vue'
        ]
        templates.forEach(item => {
            // item => 每个文件路径
            this.fs.copyTpl(
                this.templatePath(item),
                this.destinationPath(item),
                this.answers
            )
        })
    }
}

标签:文件,const,Generator,generator,js,scaffoldingTools,Yeoman
来源: https://www.cnblogs.com/lwenl/p/scaffoldingtools.html