[Vue CLI 3] vue inspect 的源码设计实现
首先,请记住:
它在新版本的脚手架项目里面非常重要
它有什么用呢?
inspect internal webpack config
能快速地在控制台看到对应生成的 webpack 配置对象。
首先它是 vue 的一个扩展命令,在文件 @vue/cli/bin/vue.js 中定义了 command
还是依赖了工具包 commander
const program = require('commander')
代码配置如下:
program
.command('inspect [paths...]')
.description('inspect the webpack config in a project with vue-cli-service')
.option('--mode <mode>')
.option('--rule <ruleName>', 'inspect a specific module rule')
.option('--plugin <pluginName>', 'inspect a specific plugin')
.option('--rules', 'list all module rule names')
.option('--plugins', 'list all plugin names')
.option('-v --verbose', 'Show full function definitions in output')
.action((paths, cmd) => {
require('../lib/inspect')(paths, cleanArgs(cmd))
})
这里的 option 比较多:
- mode
- rule
- plugin
- rules
- plugins
- verbose
在前面的文章中,我们比较常用的有 rule 相关的
再接着看一下 lib/inspect.js 文件:接受 2 个参数:
- paths
- args
module.exports = function inspect (paths, args) {
}
核心还是找到 @vue/cli-service,先获取当前执行命令的目录:
const cwd = process.cwd()
let servicePath = resolve.sync('@vue/cli-service', { basedir: cwd })
最终执行了 node ***/node_modules/@vue/cli-service/bin/vue-cli-service.js inspect 再带上参数:
调用了工具包 execa:
const execa = require('execa')
execa('node', [
binPath,
'inspect',
...(args.mode ? ['--mode', args.mode] : []),
...(args.rule ? ['--rule', args.rule] : []),
...(args.plugin ? ['--plugin', args.plugin] : []),
...(args.rules ? ['--rules'] : []),
...(args.plugins ? ['--plugins'] : []),
...(args.verbose ? ['--verbose'] : []),
...paths
], { cwd, stdio: 'inherit' })
那我们接着往下看,后面就是去 cli-service 目录了:@vue/cli-service/lib/commands/inspect.js
通过 api.registerCommand 来注册一个:
module.exports = (api, options) => {
api.registerCommand('inspect', {
}, args => {
})
}
在回调函数里面会处理之前的 option 传递的参数:
1、处理 rule
if (args.rule) {
res = config.module.rules.find(r => r.__ruleNames[0] === args.rule)
}
2、处理 plugin
if (args.plugin) {
res = config.plugins.find(p => p.__pluginName === args.plugin)
}
3、处理 rules
if (args.rules) {
res = config.module.rules.map(r => r.__ruleNames[0])
}
4、处理 plugins
if (args.plugins) {
res = config.plugins.map(p => p.__pluginName || p.constructor.name)
}
其他分支情况比较少用,暂时不做展开。
最后多是通过 webpack-chain 的 toString 函数来生成,最终在控制台打印:
You can inspect the generated webpack config using
config.toString(). This will generate a stringified version of the config with comment hints for named rules, uses and plugins.
const { toString } = require('webpack-chain')
const output = toString(res, { verbose })
来源:https://segmentfault.com/a/1190000016260585
[Vue CLI 3] vue inspect 的源码设计实现的更多相关文章
- (原创)通用查询实现方案(可用于DDD)[附源码] -- 设计思路
[声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html). [系列文章] 通用查询实现方案(可用于DDD)[附源码] -- ...
- 【 js 基础 】【 源码学习 】源码设计 (持续更新)
学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析第二部分:undersc ...
- 【 js 基础 】【 源码学习 】源码设计 (更新了backbone分析)
学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析 第二部分:unders ...
- Netflix Ribbon源码设计错误的证据(附正确示例)
我在之前一篇博客里https://www.cnblogs.com/yangfeiORfeiyang/p/9644254.html 里对Netflix Ribbon的Loadbalancer类源码设计的 ...
- 通用查询实现方案(可用于DDD)[附源码] -- 设计思路
原文:通用查询实现方案(可用于DDD)[附源码] -- 设计思路 [声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html). ...
- 从SpringBoot启动,阅读源码设计
目录 一.背景说明 二.SpringBoot工程 三.应用上下文 四.资源加载 五.应用环境 六.Bean对象 七.Tomcat服务 八.事件模型 九.配置加载 十.数据库集成 十一.参考源码 服务启 ...
- 百度ueditor vue项目应用 -- 图片上传源码修改
本文目的有两个,一.废掉单图上传,二.改造多图上传 大家都知道百度ueditor不是针对vue项目开发的,官方文档提供的源码包里有需要后端配置的接口,but到vue项目就不太好办了,网上有些文章也介绍 ...
- vue系列---snabbdom.js使用及源码分析(九)
一:什么是snabbdom? 在学习Vue或React中,我们了解最多的就是虚拟DOM,虚拟DOM可以看作是一颗模拟了DOM的Javascript树,主要是通过vnode实现一个无状态的组件,当组件状 ...
- vue v-cloak 指令 处理页面显示源码
有时候页面会先出现源码,再显示解析的内容.可以通过v-cloak解决 v-cloak 这个指令会作为元素的一个属性一直保持到vue实例编译结束,即解析后移除此指令. /* 含有v-cloak的html ...
随机推荐
- 04-6-queryselect
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- windows API 第 18篇 FindFirstVolume FindNextVolume
函数定义:Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the vol ...
- 廖雪峰Java10加密与安全-2加密算法-1URL编码
1.URL编码 URL编码是浏览器发送数据给服务器时使用的编码. 如通过百度搜索美女: 编码前:https://www.baidu.com/s?wd=美女 编码后:https://www.baidu. ...
- 模板:数位DP
第一次听说dp还有模板的... 当然你要是记忆化搜索的话,就可以有一些套路 这是一个伪代码: LL Dfs(LL now,限制,LL top){ if(!now) return 判断条件; if(!t ...
- SQL Server作业的备份
作业备份,不是备份数据库,是备份作业.我的方法是把作业导出成文件备份起来,因为当你服务器维护的多了的时候很多你的作业 就很成问题,很麻烦.最好能够作业实现同步,这个也是第一步,保存成文件,之后个人设想 ...
- Java内功修炼系列一责任链模式
在上一节的拦截器中提到,程序的设计者一般会用拦截器替替代动态代理,将动态代理的逻辑隐藏起来,而把拦截器接口提供给开发者,使开发者不需要关系动态代理的具体实现过程,但是有时候需要多个拦截器,而且拦截器之 ...
- PAT甲级——A1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- 原型模式(Prototype)(对象、克隆广告邮件)
有些对象创建过程较为复杂,而且有些时候需要频繁的创建,原型模式通过给出一个原型对象来指明所要创建的对象的类型,然后复制这个原型对象的方法创建更多同类型的对象.这就是原型模式的动机. 原型模式的主要思想 ...
- TCP、UDP区别
个人认为,用户层Socket的TCP和UDP编程,区别并不大. 最大的区别,或者说,唯一的区别,似乎就是TCP客户端中断连接之后,TCP会得到一个应答. 但是如果用UDP来实现TCP,似乎也不 ...
- tensorflow object detection faster r-cnn 中keep_aspect_ratio_resizer是什么意思
如果小伙伴的英语能力强可以直接阅读这里:https://stackoverflow.com/questions/45137835/what-the-impact-of-different-dimens ...