Raw
1 /**
2 * Asserts that a plugin's admin extension bundle — produced by `medusa
3 * plugin:build`, i.e. @medusajs/admin-bundler's Vite `build.lib` path — is
4 * shaped correctly.
5 *
6 * `build:plugin` already runs in CI as part of the root `yarn build`, but a
7 * zero exit code only proves Vite did not throw. It does not prove that both
8 * output formats were emitted, that externals were left external instead of
9 * being inlined, or that the CJS output is actually loadable — which is what
10 * `build.rollupOptions.output.interop` governs. Those are the parts most
11 * likely to break on a Vite major upgrade.
12 *
13 * Usage: node ./scripts/assert-plugin-admin-bundle.mjs <plugin-dir>
14 */
15 import { existsSync, readFileSync, statSync } from "node:fs"
16 import { createRequire } from "node:module"
17 import path from "node:path"
18
19 const require = createRequire(import.meta.url)
20
21 const pluginDir = process.argv[2]
22
23 if (!pluginDir) {
24 console.error(
25 "Usage: node ./scripts/assert-plugin-admin-bundle.mjs <plugin-dir>"
26 )
27 process.exit(1)
28 }
29
30 const adminDir = path.resolve(pluginDir, ".medusa/server/src/admin")
31
32 /** `build.lib` emits the ESM output as .mjs and the CJS output as .js here. */
33 const ESM_OUTPUT = path.join(adminDir, "index.mjs")
34 const CJS_OUTPUT = path.join(adminDir, "index.js")
35
36 /**
37 * A representative slice of admin-bundler's hardcoded `external` set. These
38 * must stay external: bundling them would give the plugin its own copy of
39 * React / the router and break hooks at runtime.
40 */
41 const MUST_BE_EXTERNAL = [
42 "react",
43 "react/jsx-runtime",
44 "react-router-dom",
45 "@tanstack/react-query",
46 "@medusajs/ui",
47 "@medusajs/js-sdk",
48 ]
49
50 /** The module kinds admin-vite-plugin's generated plugin entry must export. */
51 const EXPECTED_MODULES = [
52 "widgetModule",
53 "routeModule",
54 "menuItemModule",
55 "formModule",
56 "displayModule",
57 "i18nModule",
58 "cellRendererModule",
59 "layoutModule",
60 ]
61
62 /**
63 * Vite `define` globals that the dashboard's prebuilt chunks reference at
64 * module scope. They only exist once Vite has bundled the admin app, so they
65 * must be stubbed to require the plugin bundle outside that context.
66 */
67 const DEFINE_GLOBALS = {
68 __BASE__: "/",
69 __BACKEND_URL__: "/",
70 __STOREFRONT_URL__: "/",
71 __AUTH_TYPE__: "session",
72 __JWT_TOKEN_STORAGE_KEY__: "medusa_auth_token",
73 __MAX_UPLOAD_FILE_SIZE__: 1024 * 1024,
74 }
75
76 const failures = []
77
78 function assertOutput(label, file) {
79 if (!existsSync(file)) {
80 failures.push(`${label} output missing: ${file}`)
81 return null
82 }
83
84 const { size } = statSync(file)
85 if (size < 1024) {
86 failures.push(
87 `${label} output is suspiciously small (${size} bytes): ${file}`
88 )
89 return null
90 }
91
92 return readFileSync(file, "utf-8")
93 }
94
95 const esm = assertOutput("ESM", ESM_OUTPUT)
96 const cjs = assertOutput("CJS", CJS_OUTPUT)
97
98 // Externals must survive as bare specifiers rather than being inlined.
99 if (esm) {
100 for (const dependency of MUST_BE_EXTERNAL) {
101 if (!new RegExp(`from\\s*["']${escape(dependency)}["']`).test(esm)) {
102 failures.push(
103 `ESM output does not import "${dependency}" as an external — it was ` +
104 `likely inlined, which duplicates it inside the plugin bundle.`
105 )
106 }
107 }
108 }
109
110 if (cjs) {
111 for (const dependency of MUST_BE_EXTERNAL) {
112 if (
113 !new RegExp(`require\\(\\s*["']${escape(dependency)}["']\\s*\\)`).test(
114 cjs
115 )
116 ) {
117 failures.push(
118 `CJS output does not require "${dependency}" as an external — it was ` +
119 `likely inlined, which duplicates it inside the plugin bundle.`
120 )
121 }
122 }
123 }
124
125 // admin-vite-plugin writes this entry in `buildStart` and removes it in
126 // `buildEnd`. A leftover means `buildEnd` never fired.
127 const generatedEntry = path.resolve(
128 pluginDir,
129 "src/admin/__admin-extensions__.js"
130 )
131 if (existsSync(generatedEntry)) {
132 failures.push(
133 `Generated entry survived the build: ${generatedEntry}. ` +
134 `admin-vite-plugin's buildEnd hook did not run.`
135 )
136 }
137
138 // Load the CJS output to prove the emitted interop is usable.
139 if (cjs) {
140 Object.assign(globalThis, DEFINE_GLOBALS)
141
142 try {
143 const required = require(CJS_OUTPUT)
144 const plugin = required?.default ?? required
145
146 const missing = EXPECTED_MODULES.filter((key) => !(key in plugin))
147 if (missing.length) {
148 failures.push(
149 `CJS output loaded but is missing expected exports: ${missing.join(
150 ", "
151 )}`
152 )
153 }
154 } catch (error) {
155 failures.push(
156 `CJS output could not be required (this is what output.interop governs): ${error.message}`
157 )
158 }
159 }
160
161 function escape(value) {
162 return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
163 }
164
165 if (failures.length) {
166 console.error(
167 [
168 `[assert-plugin-admin-bundle] ${pluginDir} admin bundle is malformed:`,
169 ...failures.map((failure) => ` - ${failure}`),
170 ].join("\n")
171 )
172 process.exit(1)
173 }
174
175 console.log(
176 `[assert-plugin-admin-bundle] OK — ${pluginDir} emitted loadable ESM + CJS ` +
177 `admin bundles with externals preserved.`
178 )