[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 ...
随机推荐
- OSG实现利用菲波那契网格(Fibonacci lattice 或 Fibonacci grid)均分球面
#include<Windows.h> #include<osg/Node> #include<osg/Geode> #include<osg/Group&g ...
- warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
解决方法:是所有项目的这个"代码生成"属性设置保持一致. 项目——属性——配置属性——C/C++——代码生成:他有/MT,/MTd,/Md,/MDd四个选项,你必须让所有使用的库都 ...
- Odoo 新 API 概述
__all__ = [ 'Environment', 'Meta', 'guess', 'noguess', 'model', 'multi', 'one', 'cr', 'cr_context', ...
- webpack:Cannot find module 'extract-text-webpack-plugin'
问题: 在终端中使用此命令安装了extract-text-webpack-plugin,npm install -g extract-text-webpack-plugin并在webpack.conf ...
- Django WSGI响应过程之WSGIHandler
class WSGIHandler(base.BaseHandler): request_class = WSGIRequest def __init__(self, *args, **kwargs) ...
- light oj 1219 树上贪心
#include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...
- Maven实战错误笔记:使用mvn archetype:generate报错:Unable to add module to the current project as it is not of packaging type 'pom'
在使用mvn archetype:generate生成Maven实战03:HelloWorld中的HelloWorld的项目骨架时报了这个错,从字面上分析是可能与pom.xml文件有关,然后我看了一下 ...
- vscode, eslint, prettier, vetur冲突及解决
这3工具都必须安装. 但是安装之后, 规则冲突又让人头疼. 讲下解决方案吧.一 从0开始1. 禁止工作区插件, 如下图: 2. 清空用户设置(Code–>首选项–>设置–>[右上角 ...
- poj1961
poj1961主要是考察对next数组的理解,abaabaabaaba abaabaabaabaabaaba错开的部分便是循环节 7月29日更 如果n%(n-kmp[k])==0,那么n-kmp[k] ...
- 一句话介绍python线程、进程和协程
一.进程: Python的os模块封装了常见的系统调用,其中就包括fork.而fork是linux常用的产生子进程的方法,简言之是一个调用,两个返回. 在python中,以下的两个模块用于进程的使用. ...