Skip to content

Commit cd62576

Browse files
committed
feat: add github atuo deploy
1 parent ee34d68 commit cd62576

3 files changed

Lines changed: 181 additions & 60 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Deploy Playground to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
pages: write
12+
id-token: write
13+
14+
concurrency:
15+
group: github-pages
16+
cancel-in-progress: true
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v5
25+
26+
- name: Setup pnpm
27+
uses: pnpm/action-setup@v4
28+
with:
29+
version: 10.30.3
30+
31+
- name: Setup Node.js
32+
uses: actions/setup-node@v6
33+
with:
34+
node-version: 20
35+
cache: pnpm
36+
37+
- name: Configure GitHub Pages
38+
uses: actions/configure-pages@v5
39+
40+
- name: Install dependencies
41+
run: pnpm install --frozen-lockfile
42+
43+
- name: Build playground
44+
env:
45+
VITE_BASE_PATH: ${{ vars.VITE_BASE_PATH }}
46+
run: pnpm --filter @mxm-editor/playground build
47+
48+
- name: Upload GitHub Pages artifact
49+
uses: actions/upload-pages-artifact@v4
50+
with:
51+
path: apps/playground/dist
52+
53+
deploy:
54+
needs: build
55+
runs-on: ubuntu-latest
56+
environment:
57+
name: github-pages
58+
url: ${{ steps.deployment.outputs.page_url }}
59+
60+
steps:
61+
- name: Deploy to GitHub Pages
62+
id: deployment
63+
uses: actions/deploy-pages@v4

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ pnpm test
2929
pnpm build
3030
```
3131

32+
## 在线预览
33+
34+
仓库已经包含 GitHub Pages 自动部署工作流:
35+
36+
- Workflow 文件:`.github/workflows/deploy-playground-pages.yml`
37+
- 触发方式:推送到 `master`,或手动执行 `workflow_dispatch`
38+
- 构建目标:`apps/playground`
39+
40+
首次启用时,请在 GitHub 仓库设置中把 Pages 的 Source 设为 `GitHub Actions`。之后每次推送到 `master`,Actions 都会自动构建并发布 playground,方便开发者在线预览。
41+
42+
默认情况下,`apps/playground/vite.config.ts` 会根据 `GITHUB_REPOSITORY` 自动推导 Pages 的 `base` 路径;如果你使用自定义域名或想手动覆盖路径,可以在仓库的 GitHub Variables 里设置 `VITE_BASE_PATH`
43+
3244
## 项目架构
3345

3446
当前仓库可以按下面的方式理解:

apps/playground/vite.config.ts

Lines changed: 106 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1-
import { defineConfig } from "vite";
1+
import { defineConfig, loadEnv } from "vite";
22
import react from "@vitejs/plugin-react";
33
import tailwindcss from "@tailwindcss/vite";
44
import tsconfigPaths from "vite-tsconfig-paths";
55
import UnoCSS from "unocss/vite";
66

77
const fromRoot = (path: string) => new URL(path, import.meta.url).pathname;
8+
9+
function normalizeBasePath(basePath: string) {
10+
if (!basePath || basePath === "/") {
11+
return "/";
12+
}
13+
14+
const withLeadingSlash = basePath.startsWith("/") ? basePath : `/${basePath}`;
15+
16+
return withLeadingSlash.endsWith("/")
17+
? withLeadingSlash
18+
: `${withLeadingSlash}/`;
19+
}
20+
21+
function resolveGitHubPagesBase(env: Record<string, string>) {
22+
const explicitBasePath = env.VITE_BASE_PATH?.trim();
23+
24+
if (explicitBasePath) {
25+
return normalizeBasePath(explicitBasePath);
26+
}
27+
28+
if (!env.GITHUB_ACTIONS) {
29+
return "/";
30+
}
31+
32+
const repository = env.GITHUB_REPOSITORY;
33+
34+
if (!repository) {
35+
return "/";
36+
}
37+
38+
const [owner, repo] = repository.split("/");
39+
40+
if (!owner || !repo) {
41+
return "/";
42+
}
43+
44+
return repo === `${owner}.github.io`
45+
? "/"
46+
: `/${repo}/`;
47+
}
48+
849
const editorCorePackages = [
950
"/packages/pm/",
1051
"/packages/core/",
@@ -86,75 +127,79 @@ function includesAny(id: string, patterns: string[]) {
86127
return patterns.some((pattern) => id.includes(pattern));
87128
}
88129

89-
export default defineConfig({
90-
plugins: [
91-
react(),
92-
tailwindcss(),
93-
UnoCSS({
94-
configFile: fromRoot("./uno.config.ts"),
95-
}),
96-
tsconfigPaths(),
97-
],
98-
build: {
99-
rollupOptions: {
100-
output: {
101-
manualChunks(id) {
102-
if (
103-
id.includes("/node_modules/react/")
104-
|| id.includes("/node_modules/react-dom/")
105-
|| id.includes("/node_modules/scheduler/")
106-
) {
107-
return "react-vendor";
108-
}
130+
export default defineConfig(({ mode }) => {
131+
const env = loadEnv(mode, fromRoot("."), "");
109132

110-
if (
111-
id.includes("/node_modules/yjs/")
112-
|| id.includes("/node_modules/y-prosemirror/")
113-
|| id.includes("/node_modules/y-protocols/")
114-
|| id.includes("/node_modules/lib0/")
115-
) {
116-
return "collab-vendor";
117-
}
133+
return {
134+
base: resolveGitHubPagesBase(env),
135+
plugins: [
136+
react(),
137+
tailwindcss(),
138+
UnoCSS({
139+
configFile: fromRoot("./uno.config.ts"),
140+
}),
141+
tsconfigPaths(),
142+
],
143+
build: {
144+
rollupOptions: {
145+
output: {
146+
manualChunks(id) {
147+
if (
148+
id.includes("/node_modules/react/")
149+
|| id.includes("/node_modules/react-dom/")
150+
|| id.includes("/node_modules/scheduler/")
151+
) {
152+
return "react-vendor";
153+
}
118154

119-
if (
120-
id.includes("/node_modules/prosemirror-")
121-
|| id.includes("/node_modules/orderedmap/")
122-
|| id.includes("/node_modules/rope-sequence/")
123-
|| id.includes("/node_modules/w3c-keyname/")
124-
) {
125-
return "prosemirror-vendor";
126-
}
155+
if (
156+
id.includes("/node_modules/yjs/")
157+
|| id.includes("/node_modules/y-prosemirror/")
158+
|| id.includes("/node_modules/y-protocols/")
159+
|| id.includes("/node_modules/lib0/")
160+
) {
161+
return "collab-vendor";
162+
}
127163

128-
if (id.includes("/node_modules/marked/")) {
129-
return "markdown-vendor";
130-
}
164+
if (
165+
id.includes("/node_modules/prosemirror-")
166+
|| id.includes("/node_modules/orderedmap/")
167+
|| id.includes("/node_modules/rope-sequence/")
168+
|| id.includes("/node_modules/w3c-keyname/")
169+
) {
170+
return "prosemirror-vendor";
171+
}
131172

132-
if (id.includes("/node_modules/katex/")) {
133-
return "math-vendor";
134-
}
173+
if (id.includes("/node_modules/marked/")) {
174+
return "markdown-vendor";
175+
}
135176

136-
if (
137-
id.includes("/packages/extension-collaboration/")
138-
|| id.includes("/packages/extension-collaboration-caret/")
139-
) {
140-
return "editor-collaboration";
141-
}
177+
if (id.includes("/node_modules/katex/")) {
178+
return "math-vendor";
179+
}
142180

143-
if (includesAny(id, editorFeaturePackages)) {
144-
return "editor-features";
145-
}
181+
if (
182+
id.includes("/packages/extension-collaboration/")
183+
|| id.includes("/packages/extension-collaboration-caret/")
184+
) {
185+
return "editor-collaboration";
186+
}
146187

147-
if (includesAny(id, editorCorePackages)) {
148-
return "editor-core";
149-
}
188+
if (includesAny(id, editorFeaturePackages)) {
189+
return "editor-features";
190+
}
150191

151-
return undefined;
192+
if (includesAny(id, editorCorePackages)) {
193+
return "editor-core";
194+
}
195+
196+
return undefined;
197+
},
152198
},
153199
},
154200
},
155-
},
156-
resolve: {
157-
alias: {
201+
resolve: {
202+
alias: {
158203
"@mxm-editor/pm": fromRoot("../../packages/pm/src/index.ts"),
159204
"@mxm-editor/core": fromRoot("../../packages/core/src/index.ts"),
160205
"@mxm-editor/extension-audio": fromRoot(
@@ -370,6 +415,7 @@ export default defineConfig({
370415
"@mxm-editor/text-style-kit": fromRoot(
371416
"../../packages/text-style-kit/src/index.ts",
372417
),
418+
},
373419
},
374-
},
420+
};
375421
});

0 commit comments

Comments
 (0)