Skip to content

Commit b1e748f

Browse files
committed
first commit
0 parents  commit b1e748f

File tree

11 files changed

+5310
-0
lines changed

11 files changed

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

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)