当前位置:网站首页>每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,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边栏推荐
- 单片机之IIC通信协议「建议收藏」
- Are the registered accounts of the top ten securities companies safe and risky?
- 超纲练习题不超纲
- 炼金术(4): 程序员的心智模型
- [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
- Summary of wuenda's machine learning course (11)_ Support vector machine
- Is it safe for Huatai Securities to open an account online?
- [PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
- RecyclerView实现分组效果,多种实现方式
- 十大券商注册账户安全吗,会有风险吗?
猜你喜欢

How to quote Chinese documents when writing a foreign language?

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

赛尔笔记|视频文本预训练简述

MySQL企业级参数调优实践分享

Character interception triplets of data warehouse: substrb, substr, substring
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence

MATLB|改进的前推回代法求解低压配电网潮流

logging日志的使用

【无标题】

吴恩达《机器学习》课程总结(11)_支持向量机
随机推荐
MySQL分表查询之Merge存储引擎实现
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
ValidateRequest=”false” 是做什么的「建议收藏」
Arduino UNO通过电容的直接检测实现简易触摸开关
Sécurité, économie de carburant et protection de l'environnement chameau
超纲练习题不超纲
[AI application] detailed parameters of Jetson Xavier nx
安全省油环保 骆驼AGM启停电池魅力十足
2022 PMP project management examination agile knowledge points (3)
A summer party
Using two stacks to implement queues [two first in first out is first in first out]
One step forward is excellent, one step backward is ignorant
Feign implements path escape through custom annotations
Request object, response object, session object
SQL报了一个不常见的错误,让新来的实习生懵了
十大券商注册账户安全吗,会有风险吗?
Cornernet understands from simple to profound
PAT乙级1013
Detailed explanation of MATLAB axis coordinate axis related settings
炼金术(6): 可迭代的模型和用例