|
1
|
import { VIRTUAL_MODULES } from "@medusajs/admin-shared" |
|
2
|
import path from "path" |
|
3
|
import type { HmrOptions, InlineConfig } from "vite" |
|
4
|
import { injectTailwindCSS } from "../plugins/inject-tailwindcss" |
|
5
|
import { writeStaticFiles } from "../plugins/write-static-files" |
|
6
|
import { BundlerOptions } from "../types" |
|
7
|
|
|
8
|
export async function getViteConfig( |
|
9
|
options: BundlerOptions |
|
10
|
): Promise<InlineConfig> { |
|
11
|
const { searchForWorkspaceRoot, mergeConfig } = await import("vite") |
|
12
|
const { default: react } = await import("@vitejs/plugin-react") |
|
13
|
const { default: medusa } = await import("@medusajs/admin-vite-plugin") |
|
14
|
|
|
15
|
const getPort = await import("get-port") |
|
16
|
const hmrPort = process.env.HMR_PORT |
|
17
|
? parseInt(process.env.HMR_PORT) |
|
18
|
: await getPort.default() |
|
19
|
const hmrOptions = getHmrConfig(hmrPort) |
|
20
|
const allowedHosts = getAllowedHosts() |
|
21
|
|
|
22
|
const root = path.resolve(process.cwd(), ".medusa/client") |
|
23
|
|
|
24
|
const backendUrl = options.backendUrl ?? "" |
|
25
|
const storefrontUrl = options.storefrontUrl ?? "" |
|
26
|
const authType = process.env.ADMIN_AUTH_TYPE ?? undefined |
|
27
|
const jwtTokenStorageKey = |
|
28
|
process.env.ADMIN_JWT_TOKEN_STORAGE_KEY ?? undefined |
|
29
|
|
|
30
|
const baseConfig: InlineConfig = { |
|
31
|
root, |
|
32
|
base: options.path, |
|
33
|
resolve: { |
|
34
|
dedupe: ["react", "react-dom"], |
|
35
|
}, |
|
36
|
build: { |
|
37
|
emptyOutDir: true, |
|
38
|
outDir: path.resolve(process.cwd(), options.outDir), |
|
39
|
/** |
|
40
|
* Pinned rather than left to Vite's default. Vite 7 changed the default |
|
41
|
* from `modules` (Chrome 87 / Firefox 78 / Safari 14 / Edge 88) to |
|
42
|
* `baseline-widely-available` (Chrome 107 / Firefox 104 / Safari 16 / |
|
43
|
* Edge 107). Setting it explicitly keeps the admin's supported browser |
|
44
|
* matrix stable across future Vite majors, which move this default. |
|
45
|
*/ |
|
46
|
target: "baseline-widely-available", |
|
47
|
}, |
|
48
|
optimizeDeps: { |
|
49
|
include: [ |
|
50
|
"react", |
|
51
|
"react/jsx-runtime", |
|
52
|
"react-dom/client", |
|
53
|
"react-router-dom", |
|
54
|
"react-router", |
|
55
|
"react-router/dom", |
|
56
|
"react-i18next", |
|
57
|
"@medusajs/ui", |
|
58
|
"@medusajs/dashboard", |
|
59
|
"@medusajs/js-sdk", |
|
60
|
"@tanstack/react-query", |
|
61
|
], |
|
62
|
exclude: [...VIRTUAL_MODULES], |
|
63
|
}, |
|
64
|
define: { |
|
65
|
__BASE__: JSON.stringify(options.path), |
|
66
|
__BACKEND_URL__: JSON.stringify(backendUrl), |
|
67
|
__AUTH_TYPE__: JSON.stringify(authType), |
|
68
|
__JWT_TOKEN_STORAGE_KEY__: JSON.stringify(jwtTokenStorageKey), |
|
69
|
__STOREFRONT_URL__: JSON.stringify(storefrontUrl), |
|
70
|
__MAX_UPLOAD_FILE_SIZE__: |
|
71
|
options.maxUploadFileSize === Infinity |
|
72
|
? "Infinity" |
|
73
|
: JSON.stringify(options.maxUploadFileSize ?? 1024 * 1024), |
|
74
|
}, |
|
75
|
server: { |
|
76
|
fs: { |
|
77
|
allow: [searchForWorkspaceRoot(process.cwd())], |
|
78
|
}, |
|
79
|
hmr: hmrOptions, |
|
80
|
...(allowedHosts && { allowedHosts }), |
|
81
|
}, |
|
82
|
plugins: [ |
|
83
|
writeStaticFiles({ |
|
84
|
plugins: options.plugins, |
|
85
|
}), |
|
86
|
injectTailwindCSS({ |
|
87
|
entry: root, |
|
88
|
sources: options.sources, |
|
89
|
plugins: options.plugins, |
|
90
|
}), |
|
91
|
react(), |
|
92
|
medusa({ |
|
93
|
sources: options.sources, |
|
94
|
}), |
|
95
|
], |
|
96
|
} |
|
97
|
|
|
98
|
// Inject plugin environment variables with vite define |
|
99
|
const pluginEnv: Record<string, string | undefined> = { |
|
100
|
BACKEND_URL: backendUrl, |
|
101
|
} |
|
102
|
for (const [key, value] of Object.entries(process.env)) { |
|
103
|
if (key.startsWith("PLUGIN_")) { |
|
104
|
pluginEnv[key.replace(/^PLUGIN_/, "")] = value |
|
105
|
} |
|
106
|
} |
|
107
|
baseConfig.define!["process.env"] = JSON.stringify(pluginEnv) |
|
108
|
|
|
109
|
let finalConfig = baseConfig |
|
110
|
if (options.vite) { |
|
111
|
const customConfig = options.vite(baseConfig) |
|
112
|
finalConfig = mergeConfig(baseConfig, customConfig) |
|
113
|
} |
|
114
|
|
|
115
|
// Handle HMR_BIND_HOST after merge to detect conflicts |
|
116
|
if (process.env.HMR_BIND_HOST) { |
|
117
|
if ( |
|
118
|
finalConfig.server?.hmr && |
|
119
|
typeof finalConfig.server.hmr === "object" && |
|
120
|
finalConfig.server.hmr.server |
|
121
|
) { |
|
122
|
console.warn( |
|
123
|
"HMR_BIND_HOST is set but a custom hmr.server is already configured. HMR_BIND_HOST will be ignored." |
|
124
|
) |
|
125
|
} else { |
|
126
|
const { createServer } = require("http") |
|
127
|
const hmrServer = createServer() |
|
128
|
hmrServer.listen(hmrPort, process.env.HMR_BIND_HOST) |
|
129
|
if (!finalConfig.server) { |
|
130
|
finalConfig.server = {} |
|
131
|
} |
|
132
|
if ( |
|
133
|
!finalConfig.server.hmr || |
|
134
|
typeof finalConfig.server.hmr !== "object" |
|
135
|
) { |
|
136
|
finalConfig.server.hmr = {} |
|
137
|
} |
|
138
|
finalConfig.server.hmr.server = hmrServer |
|
139
|
} |
|
140
|
} |
|
141
|
|
|
142
|
return finalConfig |
|
143
|
} |
|
144
|
|
|
145
|
function getAllowedHosts(): string[] | undefined { |
|
146
|
const hosts = process.env.__MEDUSA_ADMIN_ADDITIONAL_ALLOWED_HOSTS |
|
147
|
if (!hosts) { |
|
148
|
return undefined |
|
149
|
} |
|
150
|
return hosts.split(",").map((host) => host.trim()) |
|
151
|
} |
|
152
|
|
|
153
|
function getHmrConfig(hmrPort: number): HmrOptions | boolean { |
|
154
|
const options: HmrOptions = { |
|
155
|
port: hmrPort, |
|
156
|
} |
|
157
|
|
|
158
|
if (process.env.HMR_PROTOCOL) { |
|
159
|
options.protocol = process.env.HMR_PROTOCOL |
|
160
|
} |
|
161
|
if (process.env.HMR_HOST) { |
|
162
|
options.host = process.env.HMR_HOST |
|
163
|
} |
|
164
|
if (process.env.HMR_CLIENT_PORT) { |
|
165
|
options.clientPort = parseInt(process.env.HMR_CLIENT_PORT) |
|
166
|
} |
|
167
|
|
|
168
|
return options |
|
169
|
} |