首先,我们看到在 package.json 中有 scripts 的定义:

"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
}

我们看到 serve 和 build 其实多是依托的 vue-cli-service

之前我们提到过了通过 api.registerCommand 来注册命令的:

比如在 cli-service/lib/commands/serve.js


module.exports = (api, options) => {
api.registerCommand('serve', {
// ...
}, async function serve (args) {
})
}

我们看一下 cli-service/lib/PluginAPI.js 文件:


class PluginAPI {
constructor (id, service) {
this.id = id
this.service = service
}
}

函数 registerCommand 会设置 service.commands

接受 3 个参数:

  • name
  • opts
  • fn

registerCommand (name, opts, fn) {
if (typeof opts === 'function') {
fn = opts
opts = null
}
this.service.commands[name] = { fn, opts: opts || {}}
}

我们再看一下 cli-service/bin/vue-cli-service.js


service.run(command, args, rawArgv).catch(err => {
error(err)
process.exit(1)
})

cli-service/lib/Service.js 会调用 run 方法:


async run (name, args = {}, rawArgv = []) {
}

里面会从 commands 里面取:


let command = this.commands[name]

最终执行它里面的 fn:


const { fn } = command
return fn(args, rawArgv)

来源:https://segmentfault.com/a/1190000016253182

[Vue CLI 3] 插件开发之 registerCommand 到底做了什么的更多相关文章

  1. [Vue CLI 3] 插件开发中的 genCacheConfig 细节研究

    在 @vue/cli-plugin-babel/index.js 中: api.genCacheConfig('babel-loader', {}, []) 我们看一下 api.genCacheCon ...

  2. vue.js中,input和textarea上的v-model指令到底做了什么?

    v-model是 vue.js 中用于在表单表单元素上创建双向数据绑定,它的本质只是一个语法糖,在单向数据绑定的基础上,增加了监听用户输入事件并更新数据的功能: 对,它本质上只是一个语法糖,但到底是一 ...

  3. 从vue.js的源码分析,input和textarea上的v-model指令到底做了什么

    v-model是 vue.js 中用于在表单表单元素上创建双向数据绑定,它的本质只是一个语法糖,在单向数据绑定的基础上,增加了监听用户输入事件并更新数据的功能:对,它本质上只是一个语法糖,但到底是一个 ...

  4. vue cli 3

    介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-service-global 快 ...

  5. [Vue CLI 3] vue inspect 的源码设计实现

    首先,请记住: 它在新版本的脚手架项目里面非常重要 它有什么用呢? inspect internal webpack config 能快速地在控制台看到对应生成的 webpack 配置对象. 首先它是 ...

  6. vue cli 3.x 项目部署到 github pages

    github pages 是 github 免费为用户提供的服务,写博客,或者部署一些纯静态项目. 最近将 vue cli 3.x 初始化项目部署到 github pages,踩了一些坑,记录如下. ...

  7. Vue CLI 3.0脚手架如何在本地配置mock数据

    前后端分离的开发模式已经是目前前端的主流模式,至于为什么会前后端分离的开发我们就不做过多的阐述,既然是前后端分离的模式开发肯定是离不开前端的数据模拟阶段. 我们在开发的过程中,由于后台接口的没有完成或 ...

  8. vue cli 3.0创建项目

    .npm i -g @vue/cli .vue create my-project 此处有两个选择: 1.default (babel, eslint)默认套餐,提供babel和eslint支持 2. ...

  9. 如何使用@vue/cli 3.0在npm上创建,发布和使用你自己的Vue.js组件库

    译者按: 你可能npm人家的包过成千上万次,但你是否有创建,发布和使用过自己的npm包? 原文: How to create, publish and use your own VueJS Compo ...

随机推荐

  1. 提示microsoft incremental linker已停止工作解决方法

    解决方案一:项目->属性->链接器->常规 下面的“启用增量链接”,将“是(/INCREMENTAL)”改为“否(/INCREMENTAL:NO)”.不过这又引入了另外一个警 告:F ...

  2. C++面向对象高级编程(下)-Geekband

    11, 组合和继承 一, Composition 复合  has-a的关系 简单来讲, 就是: class A{     classB b1;   }; 这里讲到Adapter设计模式: templa ...

  3. Tomcat Add and Remove 项目时提示 Project facet Java version 1.8 is not supported 错误

    原因:项目的jdk和tomcat的jdk版本不同 将eclipse-preference-server-runtime environments 点击你要用的tomcat 点击 edit-jre选择和 ...

  4. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  5. HZOI20190803 A,C题

    题目链接:https://www.cnblogs.com/Juve/articles/11295333.html A: 考场上只有70分... 发现几个性质:特殊性质1:在两条链上,看它是fib第几项 ...

  6. Ajax 导出Excel 方式

    1.使用iframe 加载 使用get方式 <iframe id="comdownshow" height="0" width="0" ...

  7. 你真的了解cookies吗?

    互联网隐私安全,直接放链接吧,这一篇非常好的文章,详细,全面,专业. http://www.freebuf.com/articles/web/127266.html 浅谈Web客户端追踪 一.   W ...

  8. PAT甲级——A1049 Counting Ones

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  9. python 搜索匹配文件目录下所有的jpg或者png图片

    import glob PATH_TO_TEST_IMAGES_DIR = 'D:\TrainChangeFifteenData\Picture\Test' for pidImage in glob. ...

  10. 入门servlet:request获取请求参数通用方式

    一.获取请求参数通用方式 1. String getParameter(String name):根据参数名称获取参数值 例:username=flypig&password=2343 2. ...