【vue-cli】vue-cli@2源码学习

vue-cli 2 源码

@vue/cli: 3.11.0创建项目

vue create 项目名称

@vue/cli: 2.x.x 创建项目

vue init webpack yhh-project

脚手架初始化项目流程:
脚手架初始化项目流程

下载vue/cli@2 源码
下载完成后初始化

npm i

创建项目

vue init webpack yhh-project

vue-init:

bin/vue-init

#!/usr/bin/env node

// 下载远程仓库
const download = require('download-git-repo')
// 命令行处理工具
const program = require('commander')
// 路径检查
const exists = require('fs').existsSync
// path
const path = require('path')
// 作为等待图标使用
const ora = require('ora')
// 用户根目录
const home = require('user-home')
// 波浪符路径转换
const tildify = require('tildify')
// 高亮打印
const chalk = require('chalk')
// 命令行与开发者交互工具
const inquirer = require('inquirer')
// 
const rm = require('rimraf').sync
// 日志打印
const logger = require('../lib/logger')

// 内部自定义方法
const generate = require('../lib/generate')
const checkVersion = require('../lib/check-version')
const warnings = require('../lib/warnings')
const localPath = require('../lib/local-path')

// 是否为本地路径
const isLocalPath = localPath.isLocalPath
// 本地模版存放路径
const getTemplatePath = localPath.getTemplatePath

/**
 * Usage. 配置commander 使用方法 
 */
program
  .usage('<template-name> [project-name]')
  .option('-c, --clone', 'use git clone')
  .option('--offline', 'use cached template')

/**
 * Help.
 */

program.on('--help', () => {
   
  console.log('  Examples:')
  console.log()
  console.log(chalk.gray('    # create a new project with an official template'))
  console.log('    $ vue init webpack my-project')
  console.log()
  console.log(chalk.gray('    # create a new project straight from a github template'))
  console.log('    $ vue init username/repo my-project')
  console.log()
})

/**
 * Help.
 */

function help () {
   
  program.parse(process.argv)
  if (program.args.length < 1) return program.help()
}
help()

/**
 * Settings. 配置项变量的设置
 */
// 模板名称
let template = program.args[0] 
// 是否包含/
const hasSlash = template.indexOf('/') > -1
// 项目构建目录名称
const rawName = program.args[1]
// 是否存在当前路径名称
const inPlace = !rawName || rawName === '.'
const name = inPlace ? path.relative('../', process.cwd()) : rawName
const to = path.resolve(rawName || '.')
const clone = program.clone || false

// 本地模板地址
const tmp = path.join(home, '.vue-templates', template.replace(/[\/:]/g, '-'))
if (program.offline) {
   
  console.log(`> Use cached template at ${
     chalk.yellow(tildify(tmp))}`)
  template = tmp
}

/**
 * Padding.
 */
// 结束行
console.log()
process.on('exit', () => {
   
  console.log()
})
// 命令: vue init webpack yhh-project
// const rawName = program.args[1] // yhh-project
// const inPlace = !rawName || rawName === '.' // false
// to : 项目名称文件路径 // User/../../yhh-project/
// 判断:当前项目名称不存在相同名称的文件夹时询问:在当前目录中生成项目?
//        否则询问:目标目录已存在,是否继续?
// inPlace : 是否为当前路径下构建项目
// exists(to) : 存在当前路径
if (inPlace || exists(to)) {
   
  inquirer.prompt([{
   
    type: 'confirm',
    message: inPlace
      ? 'Generate project in current directory?'
      : 'Target directory exists. Continue?',
    name: 'ok'
  }]).then(answers => {
   
    if (answers.ok) {
   
      run()
    }
  }).catch(logger.fatal)
} else {
   
  run()
}

/**
 * Check, download and generate the project.
 */
// init 主函数
function run () {
   
  // check if template is local
  // 检查是否为本地模版
  // let template = program.args[0] // webpack
  if (isLocalPath(template)) {
   
    // 获取模版文件路径
    const templatePath = getTemplatePath(template)
    // 判断模版文件是否存在
    if (exists(templatePath)) {
   
      // 开始生成框架
      generate(name, templatePath,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CCC1115

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值