当前位置:网站首页>Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
2022-06-28 00:25:00 【InfoQ】
1. Preface
jQueryunderscorelodashvuexsentryaxiosreduxkoavue-devtoolsvuex4koa-composevue 3.2 Release vue-thiscreate-vue toy vite1. The computer actually helped me open the browser principle and source code implementation
2. Learn how to use Node.js Powerful child_process modular
3. Learn to debug and learn the source code
4. wait
2. Use
2.1 stay webpack Use in
module.exports = {
//...
devServer: {
open: true,
},
};
npx webpack serve --open
npx webpack serve --no-open
webpack2.2 stay vue-cli Use
npx @vue/cli create vue3-project
# my open-analysis In the project vue3-project Folder
# npm i -g yarn
# yarn serve The browser will not open automatically
yarn serve
# --open Parameter will automatically open the browser
yarn serve --open
2.3 stay create-react-app Use
npx create-react-app react-project
# my open-analysis In the project react-project Folder
# npm i -g yarn
# The browser is automatically opened by default
yarn start

window terminalwebpackvue-clicreate-react-appopen- webpack-dev-server
- vue-cli
- create-react-app
open3. principle
npmopen Actively maintain .
Support application parameters .
More secure , Because it uses spawn instead of exec.
Fixed most of node-open The problem of .
Include for Linux The latest xdg-open Script .
Support Windows Application's WSL route .
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. Preparation before reading the source code
# Recommend cloning my project , Keep up with the article , At the same time, the test documents are complete
git clone https://github.com/lxchuan12/open-analysis.git
# npm i -g yarn
cd open && yarn
# Or clone the official project
git clone https://github.com/sindresorhus/open.git
# npm i -g yarn
cd open && yarn
4.1 Write an example , Easy to debug source code
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 function
// 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 Basic open function
// open/index.js
const childProcess = require('child_process');
const localXdgOpenPath = path.join(__dirname, 'xdg-open');
const {platform, arch} = process;
// During debugging, we can adjust and modify the platform by ourselves , It is easy to debug the similarities and differences of various platforms , such as mac、win、linux
// const {arch} = process;
// mac
// const platform = 'darwin';
// win
// const platform = 'win32';
// const platform = ' other ';
const baseOpen = async options => {
options = {
wait: false,
background: false,
newInstance: false,
allowNonzeroExitCode: false,
...options
};
// Omitted code
// command
let command;
// Command line arguments
const cliArguments = [];
// Subprocess options
const childProcessOptions = {};
if (platform === 'darwin') {
command = 'open';
// Omit mac Part of the code
} else if (platform === 'win32' || (isWsl && !isDocker())) {
// Omit window perhaps window Subsystem code
const encodedArguments = ['Start'];
} else {
const useSystemXdgOpen = process.versions.electron ||
platform === 'android' || isBundled || !exeLocalXdgOpen;
command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
// Omit linux Code
}
// Omitted code
const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
// Omitted code
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. summary
openNode.jschild_processspawnwebpackvue-clicreate-react-appVSCodeopen边栏推荐
- 什么是cookie,以及v-htm的安全性隐患
- RNA SEQ introduction practice (I): upstream data download, format conversion and quality control cleaning
- MATLB|改进的前推回代法求解低压配电网潮流
- Eliminate gaps around El image images
- Which securities company is better and safer to open an account for stock speculation
- What are the ways to combine the points system with marketing activities
- [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
- 剑指 Offer 61. 扑克牌中的顺子
- 线程池实现:信号量也可以理解成小等待队列
- internship:业务流程初识
猜你喜欢

Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)
用两个栈实现队列[两次先进后出便是先进先出]

Understand the basic structure of wechat applet project

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

数据仓库入门介绍

Character interception triplets of data warehouse: substrb, substr, substring
![软件工程作业设计(1): [个人项目] 实现一个日志查看页面](/img/95/0c3f0dde16d220ddecb5758a4c31e7.png)
软件工程作业设计(1): [个人项目] 实现一个日志查看页面

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

Instructions for vivado FFT IP
![[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
随机推荐
Is it safe for Huatai Securities to open an account online?
100 questions for an enterprise architect interview
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
MATLB|基于复杂网络的配电系统微电网优化配置
Flutter series: Transformers in flutter
logging日志的使用
股票投资交流群安全吗?入群免费开户靠谱嘛?
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
What is promise
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
Promise是什么
Chapter 2 integrated mp
Learning notes for qstringlist
翻译(4): 文本自动完成的匹配规则
Local visualization tool connects to redis of Alibaba cloud CentOS server
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
[idea] idea formatting code skills
Arduino uno realizes simple touch switch through direct detection of capacitance
Matlb| optimal configuration of microgrid in distribution system based on complex network