-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathconfig.ts
More file actions
97 lines (87 loc) · 2.53 KB
/
Copy pathconfig.ts
File metadata and controls
97 lines (87 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { VIRTUAL_MODULES } from "@medusajs/admin-shared"
import path from "path"
import type { HmrOptions, InlineConfig } from "vite"
import { injectTailwindCSS } from "../plugins/inject-tailwindcss"
import { writeStaticFiles } from "../plugins/write-static-files"
import { BundlerOptions } from "../types"
export async function getViteConfig(
options: BundlerOptions
): Promise<InlineConfig> {
const { searchForWorkspaceRoot, mergeConfig } = await import("vite")
const { default: react } = await import("@vitejs/plugin-react")
const { default: medusa } = await import("@medusajs/admin-vite-plugin")
const getPort = await import("get-port")
const hmrPort = process.env.HMR_PORT
? parseInt(process.env.HMR_PORT)
: await getPort.default()
const hmrOptions = getHmrConfig(hmrPort)
const root = path.resolve(process.cwd(), ".medusa/client")
const backendUrl = options.backendUrl ?? ""
const storefrontUrl = options.storefrontUrl ?? ""
const baseConfig: InlineConfig = {
root,
base: options.path,
build: {
emptyOutDir: true,
outDir: path.resolve(process.cwd(), options.outDir),
},
optimizeDeps: {
include: [
"react",
"react/jsx-runtime",
"react-dom/client",
"react-router-dom",
"@medusajs/ui",
"@medusajs/dashboard",
"@medusajs/js-sdk",
"@tanstack/react-query",
],
exclude: [...VIRTUAL_MODULES],
},
define: {
__BASE__: JSON.stringify(options.path),
__BACKEND_URL__: JSON.stringify(backendUrl),
__STOREFRONT_URL__: JSON.stringify(storefrontUrl),
},
server: {
fs: {
allow: [searchForWorkspaceRoot(process.cwd())],
},
hmr: hmrOptions,
},
plugins: [
writeStaticFiles({
plugins: options.plugins,
}),
injectTailwindCSS({
entry: root,
sources: options.sources,
plugins: options.plugins,
}),
react(),
medusa({
sources: options.sources,
}),
],
}
if (options.vite) {
const customConfig = options.vite(baseConfig)
return mergeConfig(baseConfig, customConfig)
}
return baseConfig
}
function getHmrConfig(hmrPort: number): HmrOptions | boolean {
const options: HmrOptions = {
port: hmrPort,
}
if (process.env.HMR_PROTOCOL) {
options.protocol = process.env.HMR_PROTOCOL
}
if (process.env.HMR_HOST) {
options.host = process.env.HMR_HOST
}
if (process.env.HMR_CLIENT_PORT) {
options.clientPort = parseInt(process.env.HMR_CLIENT_PORT)
}
return options
}