当前位置:网站首页>每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,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边栏推荐
- N methods for obtaining effective length of genes
- One step forward is excellent, one step backward is ignorant
- NDSS 2022 received list
- golang使用mongo-driver操作——查(基础)
- 零基础自学SQL课程 | IF函数
- At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
- 互联网业衍生出来了新的技术,新的模式,新的产业类型
- MySQL enterprise parameter tuning practice sharing
- Zero foundation self-study SQL course | complete collection of SQL basic functions
- Cornernet understands from simple to profound
猜你喜欢

零基础自学SQL课程 | SQL基本函数大全
![[paper reading | deep reading] sdne:structural deep network embedding](/img/6a/b2edf326f6e7ded83deb77219654aa.png)
[paper reading | deep reading] sdne:structural deep network embedding

MongoDB-在windows电脑本地安装一个mongodb的数据库

Promise是什么

零基础自学SQL课程 | CASE函数

【无标题】

吴恩达《机器学习》课程总结(13)_聚类

Zero foundation self-study SQL course | complete collection of date functions in SQL

Webserver flow chart -- understand the calling relationship between webserver modules

Sécurité, économie de carburant et protection de l'environnement chameau
随机推荐
Pat class B 1013
Cornernet understands from simple to profound
Zero foundation self-study SQL course | if function
Golang uses Mongo driver operation - query (basic)
[idea] idea formatting code skills
线程池实现:信号量也可以理解成小等待队列
单片机之IIC通信协议「建议收藏」
代码整洁之道--格式
计数质数[枚举 -> 空间换时间]
吴恩达《机器学习》课程总结(11)_支持向量机
Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
SQL reported an unusual error, which confused the new interns
MYSQL的下载与配置安装
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
炼金术(3): 怎样做好1个业务流程的接口对接
Detailed explanation of MATLAB axis coordinate axis related settings
免费、好用、强大的开源笔记软件综合评测
技术的极限(11): 有趣的编程