|
1
|
import { readFileSync } from "fs" |
|
2
|
import { builtinModules } from "node:module" |
|
3
|
import path from "path" |
|
4
|
import type { UserConfig } from "vite" |
|
5
|
import { clearPluginBuild } from "../plugins/clear-plugin-build" |
|
6
|
|
|
7
|
interface PluginOptions { |
|
8
|
root: string |
|
9
|
outDir: string |
|
10
|
} |
|
11
|
|
|
12
|
export async function plugin(options: PluginOptions) { |
|
13
|
const vite = await import("vite") |
|
14
|
const react = (await import("@vitejs/plugin-react")).default |
|
15
|
const medusa = (await import("@medusajs/admin-vite-plugin")).default |
|
16
|
|
|
17
|
const pkg = JSON.parse( |
|
18
|
readFileSync(path.resolve(options.root, "package.json"), "utf-8") |
|
19
|
) |
|
20
|
const external = new Set([ |
|
21
|
...Object.keys(pkg.dependencies || {}), |
|
22
|
...Object.keys(pkg.peerDependencies || {}), |
|
23
|
...Object.keys(pkg.devDependencies || {}), |
|
24
|
"react", |
|
25
|
"react/jsx-runtime", |
|
26
|
"react-router-dom", |
|
27
|
"react-router", |
|
28
|
"react-router/dom", |
|
29
|
"react-i18next", |
|
30
|
"@medusajs/js-sdk", |
|
31
|
"@medusajs/admin-sdk", |
|
32
|
"@tanstack/react-query", |
|
33
|
]) |
|
34
|
|
|
35
|
const outDir = path.resolve(options.root, options.outDir, "src/admin") |
|
36
|
const entryPoint = path.resolve( |
|
37
|
options.root, |
|
38
|
"src/admin/__admin-extensions__.js" |
|
39
|
) |
|
40
|
|
|
41
|
/** |
|
42
|
* We need to ensure that the NODE_ENV is set to production, |
|
43
|
* otherwise Vite will build the dev version of React. |
|
44
|
*/ |
|
45
|
const originalNodeEnv = process.env.NODE_ENV |
|
46
|
process.env.NODE_ENV = "production" |
|
47
|
|
|
48
|
const pluginConfig: UserConfig = { |
|
49
|
build: { |
|
50
|
/** |
|
51
|
* Must match the target in `utils/config.ts` — plugin admin extensions are |
|
52
|
* loaded by the dashboard bundle, so they need the same browser floor. |
|
53
|
* |
|
54
|
* This also has to be set explicitly: esbuild 0.27+ refuses to lower |
|
55
|
* destructuring for `safari14`, which Vite's old `modules` default |
|
56
|
* included, so leaving it unset breaks `medusa plugin:build` outright. |
|
57
|
*/ |
|
58
|
target: "baseline-widely-available", |
|
59
|
lib: { |
|
60
|
entry: entryPoint, |
|
61
|
formats: ["es", "cjs"], |
|
62
|
fileName: "index", |
|
63
|
}, |
|
64
|
emptyOutDir: false, |
|
65
|
minify: false, |
|
66
|
outDir, |
|
67
|
rollupOptions: { |
|
68
|
external: (id, importer) => { |
|
69
|
// If there's no importer, it's a direct dependency |
|
70
|
// Keep the existing external behavior |
|
71
|
if (!importer) { |
|
72
|
const idParts = id.split("/") |
|
73
|
const name = idParts[0]?.startsWith("@") |
|
74
|
? `${idParts[0]}/${idParts[1]}` |
|
75
|
: idParts[0] |
|
76
|
|
|
77
|
const builtinModulesWithNodePrefix = [ |
|
78
|
...builtinModules, |
|
79
|
...builtinModules.map((modName) => `node:${modName}`), |
|
80
|
] |
|
81
|
|
|
82
|
return Boolean( |
|
83
|
(name && external.has(name)) || |
|
84
|
(name && builtinModulesWithNodePrefix.includes(name)) |
|
85
|
) |
|
86
|
} |
|
87
|
|
|
88
|
// For transient dependencies (those with importers), |
|
89
|
// bundle them if they're not in our external set |
|
90
|
const idParts = id.split("/") |
|
91
|
const name = idParts[0]?.startsWith("@") |
|
92
|
? `${idParts[0]}/${idParts[1]}` |
|
93
|
: idParts[0] |
|
94
|
|
|
95
|
return Boolean(name && external.has(name)) |
|
96
|
}, |
|
97
|
output: { |
|
98
|
preserveModules: false, |
|
99
|
interop: "auto", |
|
100
|
chunkFileNames: () => { |
|
101
|
return `_chunks/[name]-[hash]` |
|
102
|
}, |
|
103
|
}, |
|
104
|
}, |
|
105
|
}, |
|
106
|
plugins: [ |
|
107
|
react(), |
|
108
|
medusa({ |
|
109
|
pluginMode: true, |
|
110
|
sources: [path.resolve(options.root, "src/admin")], |
|
111
|
}), |
|
112
|
clearPluginBuild({ outDir }), |
|
113
|
], |
|
114
|
logLevel: "silent", |
|
115
|
clearScreen: false, |
|
116
|
} |
|
117
|
|
|
118
|
await vite.build(pluginConfig) |
|
119
|
|
|
120
|
/** |
|
121
|
* Restore the original NODE_ENV |
|
122
|
*/ |
|
123
|
process.env.NODE_ENV = originalNodeEnv |
|
124
|
} |