|
1 | | -import { defineConfig } from "vite"; |
| 1 | +import { defineConfig, loadEnv } from "vite"; |
2 | 2 | import react from "@vitejs/plugin-react"; |
3 | 3 | import tailwindcss from "@tailwindcss/vite"; |
4 | 4 | import tsconfigPaths from "vite-tsconfig-paths"; |
5 | 5 | import UnoCSS from "unocss/vite"; |
6 | 6 |
|
7 | 7 | 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 | + |
8 | 49 | const editorCorePackages = [ |
9 | 50 | "/packages/pm/", |
10 | 51 | "/packages/core/", |
@@ -86,75 +127,79 @@ function includesAny(id: string, patterns: string[]) { |
86 | 127 | return patterns.some((pattern) => id.includes(pattern)); |
87 | 128 | } |
88 | 129 |
|
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("."), ""); |
109 | 132 |
|
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 | + } |
118 | 154 |
|
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 | + } |
127 | 163 |
|
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 | + } |
131 | 172 |
|
132 | | - if (id.includes("/node_modules/katex/")) { |
133 | | - return "math-vendor"; |
134 | | - } |
| 173 | + if (id.includes("/node_modules/marked/")) { |
| 174 | + return "markdown-vendor"; |
| 175 | + } |
135 | 176 |
|
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 | + } |
142 | 180 |
|
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 | + } |
146 | 187 |
|
147 | | - if (includesAny(id, editorCorePackages)) { |
148 | | - return "editor-core"; |
149 | | - } |
| 188 | + if (includesAny(id, editorFeaturePackages)) { |
| 189 | + return "editor-features"; |
| 190 | + } |
150 | 191 |
|
151 | | - return undefined; |
| 192 | + if (includesAny(id, editorCorePackages)) { |
| 193 | + return "editor-core"; |
| 194 | + } |
| 195 | + |
| 196 | + return undefined; |
| 197 | + }, |
152 | 198 | }, |
153 | 199 | }, |
154 | 200 | }, |
155 | | - }, |
156 | | - resolve: { |
157 | | - alias: { |
| 201 | + resolve: { |
| 202 | + alias: { |
158 | 203 | "@mxm-editor/pm": fromRoot("../../packages/pm/src/index.ts"), |
159 | 204 | "@mxm-editor/core": fromRoot("../../packages/core/src/index.ts"), |
160 | 205 | "@mxm-editor/extension-audio": fromRoot( |
@@ -370,6 +415,7 @@ export default defineConfig({ |
370 | 415 | "@mxm-editor/text-style-kit": fromRoot( |
371 | 416 | "../../packages/text-style-kit/src/index.ts", |
372 | 417 | ), |
| 418 | + }, |
373 | 419 | }, |
374 | | - }, |
| 420 | + }; |
375 | 421 | }); |
0 commit comments