Skip to content

Commit bc98fa0

Browse files
committed
first commit
0 parents  commit bc98fa0

File tree

11 files changed

+5326
-0
lines changed

11 files changed

+5326
-0
lines changed

.github/workflows/release.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Release
2+
3+
permissions:
4+
contents: write
5+
6+
on:
7+
push:
8+
tags:
9+
- 'v*'
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Set node
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: lts/*
23+
24+
- run: npx changelogithub
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/unit-test.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Unit Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Install pnpm
17+
uses: pnpm/action-setup@v4.1.0
18+
19+
- name: Set node LTS
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: lts/*
23+
cache: pnpm
24+
25+
- name: Install
26+
run: pnpm install
27+
28+
- name: Build
29+
run: pnpm run build
30+
31+
- name: Lint
32+
run: pnpm run lint
33+
34+
- name: Typecheck
35+
run: pnpm run typecheck
36+
37+
- name: Test
38+
run: pnpm run test

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
dist
3+
*.log
4+
.DS_Store
5+
.vscode
6+
.capacitor

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
以下是为您整理的完整 `README.md` 源码,包含**快速配置****使用案例**,采用纯文本格式方便直接复制:
2+
3+
```markdown
4+
# vite-plugin-vite-capacitor
5+
6+
[![npm version](https://img.shields.io/npm/v/vite-plugin-vite-capacitor.svg)](https://www.npmjs.com/package/vite-plugin-vite-capacitor)
7+
[![license](https://img.shields.io/github/license/author/library.svg)](./LICENSE)
8+
9+
🚀 **自动同步 Vite 开发服务器环境到 Capacitor 配置。**
10+
11+
在进行 Capacitor 真机调试时,手动查找本机局域网 IP 并修改 `server.url` 非常繁琐。本插件通过自动化这一过程,实现了“启动即调试”的移动端开发体验。
12+
13+
---
14+
15+
## ✨ 特性
16+
17+
- **智能 IP 探测**:自动识别局域网 IPv4 地址,优先匹配物理网卡(192.168.x, 10.x, 172.x),自动排除 VPN 和虚拟网关。
18+
- **双模式自动化**
19+
- **开发模式**:自动配置 `server.url``cleartext: true`,支持真机实时热更新 (HMR)。
20+
- **生产模式**:自动同步 Vite 的 `outDir` 到 Capacitor 的 `webDir`
21+
- **类型安全**:通过 TypeScript 模块补丁,在 `vite.config.ts` 中直接获得 Capacitor 配置的智能提示。
22+
- **零依赖**:仅利用 Node.js 内置模块处理网络和文件系统。
23+
24+
---
25+
26+
## 📦 安装
27+
28+
```bash
29+
# 使用 pnpm
30+
pnpm add vite-plugin-vite-capacitor -D
31+
32+
# 使用 npm
33+
npm install vite-plugin-vite-capacitor -D
34+
```
35+
36+
---
37+
38+
## 🛠️ 快速配置
39+
40+
### 1. 配置 Vite 插件
41+
42+
`vite.config.ts` 中引入并使用插件。
43+
44+
```typescript
45+
// vite.config.ts
46+
import { defineConfig } from 'vite'
47+
import { viteCapacitor } from 'vite-plugin-vite-capacitor'
48+
49+
export default defineConfig({
50+
plugins: [
51+
viteCapacitor()
52+
],
53+
// 插件扩展了类型,你可以在这里定义 Capacitor 基础属性
54+
capacitor: {
55+
appId: 'com.example.app',
56+
appName: 'MyCapApp',
57+
},
58+
server: {
59+
host: true, // 必须开启 host
60+
}
61+
})
62+
```
63+
64+
### 2. 桥接 Capacitor 配置
65+
66+
修改项目根目录的 `capacitor.config.ts`,调用 `loadConfig` 来动态加载生成的配置。
67+
68+
```typescript
69+
// capacitor.config.ts
70+
import { loadConfig } from 'vite-plugin-vite-capacitor'
71+
72+
export default loadConfig()
73+
```
74+
75+
### 3. 更新 .gitignore
76+
77+
插件生成的临时配置文件建议不要提交到仓库。
78+
79+
```ignore
80+
# .gitignore
81+
.capacitor/
82+
83+
```
84+
85+
## 📖 使用案例 (Usage Example)
86+
87+
### 场景 A:真机实时调试 (Development)
88+
89+
1. **执行启动**: 运行 `npm run dev`
90+
* 插件检测到你的电脑 IP 是 `192.168.1.50`,Vite 端口是 `5173`
91+
* 插件自动在 `.capacitor/config.dev.json` 中生成包含 `http://192.168.1.50:5173` 的配置。
92+
93+
2. **运行 App**: 运行 `npx cap run ios``npx cap run android`
94+
* **效果**: 手机 App 启动后会自动连接到你电脑上的 Vite 服务。当你修改代码并保存时,手机屏幕会**立即同步更新 (HMR)**
95+
96+
### 场景 B:构建发布 (Production)
97+
98+
1. **执行构建**: 运行 `npm run build`
99+
* 插件识别到 Vite 的构建输出目录为 `dist`
100+
* 插件自动在 `.capacitor/config.prod.json` 中将 `webDir` 设置为 `dist`
101+
102+
2. **同步资源**: 运行 `npx cap copy`
103+
* **效果**: Capacitor 会自动将编译好的 `dist` 静态资源拷贝到原生工程中,确保 App 在离线状态下也能正常运行。
104+
105+
## 📂 项目结构
106+
107+
```text
108+
.
109+
├── .capacitor/ # 插件生成的临时配置目录
110+
│ ├── config.dev.json # 开发环境配置 (含动态 IP URL)
111+
│ └── config.prod.json # 生产环境配置 (含 webDir)
112+
├── vite.config.ts # 配置 Capacitor 基础属性
113+
└── capacitor.config.ts # 动态消费生成的配置
114+
115+
```

eslint.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import antfu from '@antfu/eslint-config'
2+
3+
export default antfu()

package.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "vite-capacitor",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"description": "Automate Capacitor configuration (IP & webDir) sync with Vite.",
6+
"author": "BaiYanchen <bycrxemail@gmail.com>",
7+
"license": "MIT",
8+
"homepage": "https://github.com/YanChenBai/vite-capacitor#readme",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/YanChenBai/vite-capacitor.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/YanChenBai/vite-capacitor/issues"
15+
},
16+
"keywords": [
17+
"vite",
18+
"vite-plugin",
19+
"capacitor",
20+
"ios",
21+
"android",
22+
"mobile"
23+
],
24+
"exports": {
25+
".": "./dist/index.mjs",
26+
"./package.json": "./package.json"
27+
},
28+
"main": "./dist/index.mjs",
29+
"module": "./dist/index.mjs",
30+
"types": "./dist/index.d.mts",
31+
"files": [
32+
"README.md",
33+
"dist",
34+
"types.d.ts"
35+
],
36+
"scripts": {
37+
"build": "tsdown",
38+
"dev": "tsdown --watch",
39+
"test": "vitest",
40+
"typecheck": "tsc --noEmit",
41+
"prepublishOnly": "pnpm lint && pnpm typecheck && pnpm build",
42+
"release": "pnpm prepublishOnly && bumpp && npm publish",
43+
"lint": "eslint .",
44+
"lint:fix": "nr lint --fix"
45+
},
46+
"devDependencies": {
47+
"@antfu/eslint-config": "^7.0.1",
48+
"@capacitor/cli": "^8.0.1",
49+
"@types/node": "^25.0.3",
50+
"bumpp": "^10.3.2",
51+
"eslint": "^9.39.2",
52+
"esno": "^4.8.0",
53+
"tsdown": "^0.18.1",
54+
"typescript": "^5.9.3",
55+
"vite": "7.3.1",
56+
"vitest": "^4.0.16"
57+
},
58+
"publishConfig": {
59+
"access": "public"
60+
}
61+
}

0 commit comments

Comments
 (0)