当前位置:网站首页>每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
2022-06-27 21:51:00 【InfoQ】
1. 前言
jQueryunderscorelodashvuexsentryaxiosreduxkoavue-devtoolsvuex4koa-composevue 3.2 发布vue-thiscreate-vue玩具vite1. 电脑竟然乖乖的帮我打开了浏览器原理和源码实现
2. 学会使用 Node.js 强大的 child_process 模块
3. 学会调试学习源码
4. 等等
2. 使用
2.1 在 webpack 中使用
module.exports = {
//...
devServer: {
open: true,
},
};
npx webpack serve --open
npx webpack serve --no-open
webpack2.2 在 vue-cli 使用
npx @vue/cli create vue3-project
# 我的 open-analysis 项目中 vue3-project 文件夹
# npm i -g yarn
# yarn serve 不会自动打开浏览器
yarn serve
# --open 参数后会自动打开浏览器
yarn serve --open
2.3 在 create-react-app 使用
npx create-react-app react-project
# 我的 open-analysis 项目中 react-project 文件夹
# npm i -g yarn
# 默认自动打开了浏览器
yarn start

window terminalwebpackvue-clicreate-react-appopen- webpack-dev-server
- vue-cli
- create-react-app
open3. 原理
npmopen积极维护。
支持应用参数。
更安全,因为它使用 spawn 而不是 exec。
修复了大多数 node-open 的问题。
包括适用于 Linux 的最新 xdg-open 脚本。
支持 Windows 应用程序的 WSL 路径。
openNode.jschild_processspawn# mac
open https://lxchuan12.gitee.io
# win
start https://lxchuan12.gitee.io
# linux
xdg-open https://lxchuan12.gitee.io
opennpmopen- webpack-dev-server
- react-dev-utils
- @vue/cli-shared-utils
- patch-package
- lighthouse
- release-it
4. 阅读源码前的准备工作
# 推荐克隆我的项目,保证与文章同步,同时测试文件齐全
git clone https://github.com/lxchuan12/open-analysis.git
# npm i -g yarn
cd open && yarn
# 或者克隆官方项目
git clone https://github.com/sindresorhus/open.git
# npm i -g yarn
cd open && yarn
4.1 写个例子,便于调试源码
READMEopen-analysisexamplesindex.js// open-analysis/examples/index.js
(async () => {
const open = require('../open/index.js');
await open('https://lxchuan12.gitee.io');
})();
await open('https://lxchuan12.gitee.io');node examples/index.js
VSCodeopen

4.2 open 打开函数
// open/index.js
const open = (target, options) => {
if (typeof target !== 'string') {
throw new TypeError('Expected a `target`');
}
return baseOpen({
...options,
target
});
};
baseOpenconst subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);4.3 baseOpen 基础打开函数
// open/index.js
const childProcess = require('child_process');
const localXdgOpenPath = path.join(__dirname, 'xdg-open');
const {platform, arch} = process;
// 调试时我们可以自行调整修改平台,便于调试各个平台异同,比如 mac、win、linux
// const {arch} = process;
// mac
// const platform = 'darwin';
// win
// const platform = 'win32';
// const platform = '其他';
const baseOpen = async options => {
options = {
wait: false,
background: false,
newInstance: false,
allowNonzeroExitCode: false,
...options
};
// 省略部分代码
// 命令
let command;
// 命令行参数
const cliArguments = [];
// 子进程选项
const childProcessOptions = {};
if (platform === 'darwin') {
command = 'open';
// 省略 mac 部分代码
} else if (platform === 'win32' || (isWsl && !isDocker())) {
// 省略 window 或者 window 子系统代码
const encodedArguments = ['Start'];
} else {
const useSystemXdgOpen = process.versions.electron ||
platform === 'android' || isBundled || !exeLocalXdgOpen;
command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
// 省略 linux 代码
}
// 省略部分代码
const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
// 省略部分代码
subprocess.unref();
return subprocess;
}
openNode.jschild_processspawn# mac
open https://lxchuan12.gitee.io
# win
start https://lxchuan12.gitee.io
# linux
xdg-open https://lxchuan12.gitee.io
5. 总结
openNode.jschild_processspawnwebpackvue-clicreate-react-appVSCodeopen边栏推荐
- 内网IP和公网IP的区别及作用
- Zero foundation self-study SQL course | complete collection of SQL basic functions
- Golang uses Mongo driver operation - query (Advanced)
- Learning notes for qstringlist
- 数仓的字符截取三胞胎:substrb、substr、substring
- [microservices sentinel] sentinel data persistence
- 炼金术(6): 可迭代的模型和用例
- Chapter 2 integrated mp
- 互联网的发展为产业的变革和转型提供了新的解决方案
- Character interception triplets of data warehouse: substrb, substr, substring
猜你喜欢

现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)

Sécurité, économie de carburant et protection de l'environnement chameau

Webserver flow chart -- understand the calling relationship between webserver modules
![[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation](/img/9e/f08ce0729c89b0205c0ac47c523ad7.png)
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation

MATLB|改进的前推回代法求解低压配电网潮流
![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)
用两个栈实现队列[两次先进后出便是先进先出]

现代编程语言:zig

炼金术(1): 识别项目开发中的ProtoType、Demo、MVP

Flutter series: Transformers in flutter

Zero foundation self-study SQL course | case function
随机推荐
超纲练习题不超纲
Matlab基本函数 length函数
[AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb
Zero foundation self-study SQL course | complete collection of SQL basic functions
SQL报了一个不常见的错误,让新来的实习生懵了
安全省油环保 骆驼AGM启停电池魅力十足
Request object, response object, session object
一个人可以到几家证券公司开户?开户安全吗
MYSQL的下载与配置安装
Systematic learning + active exploration is the most comfortable way to get started!
MySQL enterprise parameter tuning practice sharing
Webserver flow chart -- understand the calling relationship between webserver modules
logging日志的使用
QStringList 的学习笔记
MATLAB basic function length function
HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)
Oracle数据库的启停
Is the securities registration account safe? Is there any risk?
夏日的晚会
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation