当前位置:网站首页>每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,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边栏推荐
- 炼金术(6): 可迭代的模型和用例
- MATLB|基于复杂网络的配电系统微电网优化配置
- Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
- 零基础自学SQL课程 | CASE函数
- How many securities companies can a person open an account? Is it safe to open an account
- Sell notes | brief introduction to video text pre training
- [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
- Build an open source and beautiful database monitoring system -lepus
- C语言malloc函数的功能及用法
- 【无标题】
猜你喜欢

零基础自学SQL课程 | SQL基本函数大全

表单form 和 表单元素(input、select、textarea等)

免费、好用、强大的开源笔记软件综合评测

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

Flutter series: Transformers in flutter
Does the subscription of Siyuan notes stop deleting cloud data directly?

吴恩达《机器学习》课程总结(11)_支持向量机

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

ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code

Sentinel
随机推荐
[PCL self study: segmentation4] point cloud segmentation based on Min cut
golang使用mongo-driver操作——查(进阶)
[idea] idea formatting code skills
内网IP和公网IP的区别及作用
搭建开源美观的数据库监控系统-Lepus
golang使用mongo-driver操作——查(基础)
MySQL分表查询之Merge存储引擎实现
Grab those duplicate genes
Is it safe to open a stock account through the account opening QR code of CICC securities manager? Or is it safe to open an account in a securities company?
本地可视化工具连接阿里云centOS服务器的redis
How to quote Chinese documents when writing a foreign language?
[PCL self study: pclplotter] pclplotter draws data analysis chart
吴恩达《机器学习》课程总结(13)_聚类
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
Count prime [enumeration - > space for time]
How to use the apipost script - global variables
Pytorch Foundation (1)
翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
A summer party
Instructions for vivado FFT IP