vue CLI 用起来的确很舒服,方便省事,但他经过层层封装很难明白,执行完那个npm run serve/build 后他都干了些什么,甚至不知道整个项目是怎么跑起来的,今天自己抽时间就去瞅瞅,为加深记录特此记录记录

【声明】纯属个人学习推敲,有不对的地方欢迎指正,我们一起讨论共同学习一起进步

一、探寻npm run 背后的真实操作

1、看看 npm run serve

首选从npm run serve 开始,整个应该都很熟悉了,执行这命令后就是执行,package.json 的script 中key为serve后面的值

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

其实真实的执行命令是这一个 npm run vue-cli-service serve 命令,那这个是个啥意思我们做个测试,添加个test 进行测试

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

再来执行下命令 run , 看如下打印

    D:\YLKJPro\fgzs>npm run test

    > sdz@0.1.0 test D:\YLKJPro\fgzs
> echo hello vue hello vue

其实就是执行了test 后面的echo , 那么 npm run vue-cli-service serve 后面的serve 是干啥的呢?再来看看

    D:\YLKJPro\fgzs>npm run test serve

    > sdz@0.1.0 test D:\YLKJPro\fgzs
> echo hello vue "serve" hello vue "serve"

其实就是将后面的当成了参数

2、仿造一个serve

如果不信,我们再来做一个测试看看(仿造一个 serve)

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

执行npm run test 输出如下

D:\YLKJPro\fgzs>npm run test

> sdz@0.1.0 test D:\YLKJPro\fgzs
> my-npm-test serve serve

咦,奇怪了 , serve 怎么打印出来的呢,我并没有使用echo ?其实我是模仿了原来的脚本,


2-1. 创建测试文件夹

先在node_modules下创建一个mytest/bin目录,同时在该bin目录下创建一个测试的js,如下



这个测试的js 也很简单就是把那个接收的参数打印出来,如下:

#!/usr/bin/env node

const rawArgv = process.argv.slice(2)

console.log(rawArgv[0])

2-2. 在 node_modules/.bin下创建测试脚本



添加了一个 linux 和 windows 的shell 脚本(my-npm-test和my-npm-test.cmd)

其实里面就一些目标js的路径


2-3. 添加my-npm-test

my-npm-test

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../mytest/bin/my-npm-test.js" "$@"
ret=$?
else
node "$basedir/../mytest/bin/my-npm-test.js" "$@"
ret=$?
fi
exit $ret

2-4. 添加my-npm-test.cmd

my-npm-test.cmd 用于windows 端

@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\mytest\bin\my-npm-test.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\mytest\bin\my-npm-test.js" %*
)

到这里总算对npm run 有些了解了;

其实 执行 npm help run 官方也有想对应的解释 如


2-5. 执行原理

使用npm run script执行脚本的时候都会创建一个shell,然后在shell中执行指定的脚本。

这个shell会将当前项目的可执行依赖目录(即node_modules/.bin)添加到环境变量path中,当执行之后之后再恢复原样。就是说脚本命令中的依赖名会直接找到node_modules/.bin下面的对应脚本,而不需要加上路径。


2-6. 举一反三探寻npm run serve

好吧到这了总算知道npm run 并不是那么神秘了,咦 好像搞了半天还没说到,npm run serve 相关的东西,其实这已经讲完了,仔细一想,npm run serve === npm run vue-cli-service serve ,那么node_modules/.bin下面一定有两个vue-cli-service的文件,找找。。。



果不其然,再打开看看,他最终执行的js 是什么。打开文件



根据路径可以找到node_modules/@vue下对应的 js,

如下:



OK, 总算找到了真正的执行者,那这个文件又干了些什么呢,项目就这么启动了?

二、项目编译详解

我们打开这个vue-cli-service.js (代码就不行行详细讲解了,直接借助大佬博客https://segmentfault.com/a/1190000017876208)

1、关于vue-cli-service.js
    const semver = require('semver')
const { error } = require('@vue/cli-shared-utils')
const requiredVersion = require('../package.json').engines.node // 检测node版本是否符合vue-cli运行的需求。不符合则打印错误并退出。
if (!semver.satisfies(process.version, requiredVersion)) {
error(
`You are using Node ${process.version}, but vue-cli-service ` +
`requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
)
process.exit(1)
} // cli-service的核心类。
const Service = require('../lib/Service')
// 新建一个service的实例。并将项目路径传入。一般我们在项目根路径下运行该cli命令。所以process.cwd()的结果一般是项目根路径
const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd()) // 参数处理。
const rawArgv = process.argv.slice(2)
const args = require('minimist')(rawArgv, {
boolean: [
// build
'modern',
'report',
'report-json',
'watch',
// serve
'open',
'copy',
'https',
// inspect
'verbose'
]
})
const command = args._[0] // 将我们执行npm run serve 的serve参数传入service这个实例并启动后续工作。(如果我们运行的是npm run build。那么接收的参数即为build)。
service.run(command, args, rawArgv).catch(err => {
error(err)
process.exit(1)
})

上面js 最后调用了../lib/Service 中的run来进行项目的构建 ,那再去看看 Service.js 又做了些什么

2、关于Service.js
 // ...省略import

module.exports = class Service {
constructor (context, { plugins, pkg, inlineOptions, useBuiltIn } = {}) {
process.VUE_CLI_SERVICE = this
this.initialized = false
// 一般是项目根目录路径。
this.context = context
this.inlineOptions = inlineOptions
// webpack相关收集。不是本文重点。所以未列出该方法实现
this.webpackChainFns = []
this.webpackRawConfigFns = []
this.devServerConfigFns = []
//存储的命令。
this.commands = {}
// Folder containing the target package.json for plugins
this.pkgContext = context
// 键值对存储的pakcage.json对象,不是本文重点。所以未列出该方法实现
this.pkg = this.resolvePkg(pkg)
// **这个方法下方需要重点阅读。**
this.plugins = this.resolvePlugins(plugins, useBuiltIn) // 结果为{build: production, serve: development, ... }。大意是收集插件中的默认配置信息
// 标注build命令主要用于生产环境。
this.modes = this.plugins.reduce((modes, { apply: { defaultModes }}) => {
return Object.assign(modes, defaultModes)
}, {})
} init (mode = process.env.VUE_CLI_MODE) {
if (this.initialized) {
return
}
this.initialized = true
this.mode = mode // 加载.env文件中的配置
if (mode) {
this.loadEnv(mode)
}
// load base .env
this.loadEnv() // 读取用户的配置信息.一般为vue.config.js
const userOptions = this.loadUserOptions()
// 读取项目的配置信息并与用户的配置合并(用户的优先级高)
this.projectOptions = defaultsDeep(userOptions, defaults()) debug('vue:project-config')(this.projectOptions) // 注册插件。
this.plugins.forEach(({ id, apply }) => {
apply(new PluginAPI(id, this), this.projectOptions)
}) // wepback相关配置收集
if (this.projectOptions.chainWebpack) {
this.webpackChainFns.push(this.projectOptions.chainWebpack)
}
if (this.projectOptions.configureWebpack) {
this.webpackRawConfigFns.push(this.projectOptions.configureWebpack)
}
} resolvePlugins (inlinePlugins, useBuiltIn) {
const idToPlugin = id => ({
id: id.replace(/^.\//, 'built-in:'),
apply: require(id)
}) let plugins // 主要是这里。map得到的每个插件都是一个{id, apply的形式}
// 其中require(id)将直接import每个插件的默认导出。
// 每个插件的导出api为
// module.exports = (PluginAPIInstance,projectOptions) => {
// PluginAPIInstance.registerCommand('cmdName(例如npm run serve中的serve)', args => {
// // 根据命令行收到的参数,执行该插件的业务逻辑
// })
// // 业务逻辑需要的其他函数
//}
// 注意着里是先在构造函数中resolve了插件。然后再run->init->方法中将命令,通过这里的的apply方法,
// 将插件对应的命令注册到了service实例。
const builtInPlugins = [
'./commands/serve',
'./commands/build',
'./commands/inspect',
'./commands/help',
// config plugins are order sensitive
'./config/base',
'./config/css',
'./config/dev',
'./config/prod',
'./config/app'
].map(idToPlugin) // inlinePlugins与非inline得处理。默认生成的项目直接运行时候,除了上述数组的插件['./commands/serve'...]外,还会有
// ['@vue/cli-plugin-babel','@vue/cli-plugin-eslint','@vue/cli-service']。
// 处理结果是两者的合并,细节省略。
if (inlinePlugins) {
//...
} else {
//...默认走这条路线
plugins = builtInPlugins.concat(projectPlugins)
} // Local plugins 处理package.json中引入插件的形式,具体代码省略。 return plugins
} async run (name, args = {}, rawArgv = []) {
// mode是dev还是prod?
const mode = args.mode || (name === 'build' && args.watch ? 'development' : this.modes[name]) // 收集环境变量、插件、用户配置
this.init(mode) args._ = args._ || []
let command = this.commands[name]
if (!command && name) {
error(`command "${name}" does not exist.`)
process.exit(1)
}
if (!command || args.help) {
command = this.commands.help
} else {
args._.shift() // remove command itself
rawArgv.shift()
}
// 执行命令。例如vue-cli-service serve 则,执行serve命令。
const { fn } = command
return fn(args, rawArgv)
} // 收集vue.config.js中的用户配置。并以对象形式返回。
loadUserOptions () {
// 此处代码省略,可以简单理解为
// require(vue.config.js)
return resolved
}
}
2-1. command 中的fn

看到上面说的

   // 执行命令。例如vue-cli-service serve 则,执行serve命令。
const { fn } = command
return fn(args, rawArgv)

其实还是不明吧,command中他究竟执行了个什么操作,那不妨来个console



我们再运行下 run build 来看究竟,一执行屏幕就打印了一异步函数

咦这是哪里的,不要忘记了,上面说的在运行npm run build 时我们给他传入了一个build的参数

而在代码的解析中我们知道,在constructor构造时就将其所需外部plugin编译到了command中

所以根据builtInPlugins这里的操作,我们就能找到这个异步函数是在commands/build/index.js中, 到该文件一看就都明白了

接下来还有一个是 PluginAPI 进行插件编译的js

3、关于PluginAPI
class PluginAPI {

  constructor (id, service) {
this.id = id
this.service = service
}
// 在service的init方法中
// 该函数会被调用,调用处如下。
// // apply plugins.
// 这里的apply就是插件暴露出来的函数。该函数将PluginAPI实例和项目配置信息(例如vue.config.js)作为参数传入
// 通过PluginAPIInstance.registerCommand方法,将命令注册到service实例。
// this.plugins.forEach(({ id, apply }) => {
// apply(new PluginAPI(id, this), this.projectOptions)
// })
registerCommand (name, opts, fn) {
if (typeof opts === 'function') {
fn = opts
opts = null
}
this.service.commands[name] = { fn, opts: opts || {}}
} } module.exports = PluginAPI

这些文件所有的操作加起来就完成了我们vue项目的构建,直接浏览器输入地址就可以看见效果了(一步步操作看完,是否感觉还是蛮复杂的呢- -哪有什么岁月静好,不过是有人替你负重前行罢了),欢迎加群一起交流

npm run serve/build 背后的真实操作的更多相关文章

  1. npm run dev/build/serve

    1.ERR引发的思考 npm run dev npm ERR! missing script: dev npm ERR! A complete log of this run can be found ...

  2. npm run dev 和 npm run serve

    1.ERR引发的思考 创建好的 vue 项目直接执行 vue run dev 报错?运行 vue run serve 就可以启动...如下 npm run dev npm ERR! missing s ...

  3. 在终端输入npm run serve时出现npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test_vue_0613@1.0.0 dev: 错误的解决方法

    在vscode终端使用命令 npm run serve 的时候报错 错误原因在于由于文件 node_modules 太大,在项目上传时有些人会删掉 导致我们下载的项目中缺少这个文件 在尝试把自己项目的 ...

  4. npm run serve 报错问题 (npm ERR! code ELIFECYCLE)

    运行 npm cache clean --force删除 node_modules删除 package-lock.json运行 npm install最后 npm run serve

  5. vue工程npm run serve/start/dev启动时,node_modules文件报:Cannot read property 'range' of null 错误

    改问题是"babel-eslint"版本更新问题导致的: 给大家一个最简单粗暴的解决方案: 在项目里找到对应的工程:直接删除里面的node_modules文件夹,然后重新npm i ...

  6. npm run serve修改为npm run dev

    找到package.json文件,打开文件找到  "serve": "vue-cli-service serve"  这一行,把前面的 serve 修改 dev ...

  7. npm run build 时的 warning

    entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit ...

  8. React项目配置npm run build命令分环境打包

    使用create-react-app脚手架创建的项目默认隐藏了webpack等配置文件信息,使用npm run eject命令暴露这些隐藏的配置文件信息 项目默认有两个环境:开发环境(npm star ...

  9. npm run dev 报错:missing script:dev

    一.问题: 今天在运行vue项目时,在mac终端输入npm run dev,结果报错: 翻译是: npm错误:缺少script:dev npm错误:完整路径见:users/mymac/ .npm/_l ...

  10. 三面面试官:运行 npm run xxx 的时候发生了什么?

    事情是这样的,直接开讲 面试官:npm run xxx的时候,发生了什么?讲的越详细越好. 我(心想,简单啊): 首先,DNS 解析,将域名解析成 IP 地址,然后 TCP 连接,TCP 三次握手.. ...

随机推荐

  1. 【读书笔记】组合计数-Tilings-正文 学一半的笔记

    Tilings-正文部分 目录 9.2 转移函数方法 例子 补充 9.3 其余的方法 9.3.1 the path method 9.3.2 The permanent-determinant and ...

  2. vue2升级vue3:vue-i18n国际化异步按需加载

    vue2异步加载之前说过,vue3还是之前的方法,只是把 i18n.setLocaleMessage改为i18n.global.setLocaleMessage 但是本文还是详细说一遍: 为什么需要异 ...

  3. return、break与continue的区别

    前言 在上一篇文章中,壹哥给大家介绍了while.do-while两种循环结构,并且给大家总结了两种循环的区别.实际上,我们在利用循环执行重复操作的过程中,还存在着另一个需求:如何中止,或者说提前结束 ...

  4. 手机号码归属地 API 实现防止骚扰电话,看这一篇就够了(内附设计思路和代码)

     在当今时代,骚扰电话已经成为了很多人日常生活中的一个常见问题,严重影响了人们的工作和生活. 为了避免这种情况的发生,企业和机构可以采用手机号码归属地 API,以提供更好的电话服务,减少骚扰电话的出现 ...

  5. 传输层和网络层的checksum区别,TCP cksum为何包含伪首部

    一直搞不清传输层和网络层的校验和为什么校验内容不一样,最近问了一些前辈,找寻了一些答案,总结一下自己的思考. 先说一下传输层(TCP)和网络层(IP)的校验和: TCP校验和有伪首部.TCP herd ...

  6. wireshark抓包教程详解

    https://blog.csdn.net/lixinkuan328/article/details/122985439 Wireshark 的抓包和分析,看这篇就够了!:https://blog.c ...

  7. 一次因生产事故与chatGpt的对话

    一次因生产事故与chatGpt的对话 前言:生产出现了一个内存溢出的事故,记录错误信息.错误日志如下 org.springframework.web.util.NestedServletExcepti ...

  8. 字符串常见API(charCodeAt\fromCharCode)

    1.myStr.charCodeAt(num) 返回指定位置的字符的Unicode(是字符编码的一种模式)编码. 2.String.fromCharCode() String的意思就是不能用自己定义的 ...

  9. 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(13)-Charles如何进行Mock和接口测试

    1.简介 Charles最大的优势在于抓包分析,而且我们大部分使用的功能也在抓包的功能上,但是不要忘记了,Charles也可以做接口测试.至于Mock,其实在修改请求和响应数据哪里就已经介绍了,宏哥就 ...

  10. golang pprof 监控系列(4) —— goroutine thread 统计原理

    golang pprof 监控系列(4) -- goroutine thread 统计原理 大家好,我是蓝胖子. 在之前 golang pprof监控 系列文章里我分别介绍了go trace以及go ...