Skip to content

Commit ec56a8b

Browse files
fix(medusa,utils,test-utils,types,framework,dashboard,admin-vite-plugin,admin-bundler): Fix broken plugin dependencies in development server (medusajs#11720)
**What** - Reworks how admin extensions are loaded from plugins. - Reworks how extensions are managed internally in the dashboard project. **Why** - Previously we loaded extensions from plugins the same way we do for extension found in a users application. This being scanning the source code for possible extensions in `.medusa/server/src/admin`, and including any extensions that were discovered in the final virtual modules. - This was causing issues with how Vite optimizes dependencies, and would lead to CJS/ESM issues. Not sure of the exact cause of this, but the issue was pinpointed to Vite not being able to register correctly which dependencies to optimize when they were loaded through the virtual module from a plugin in `node_modules`. **What changed** - To circumvent the above issue we have changed to a different strategy for loading extensions from plugins. The changes are the following: - We now build plugins slightly different, if a plugin has admin extensions we now build those to `.medusa/server/src/admin/index.mjs` and `.medusa/server/src/admin/index.js` for a ESM and CJS build. - When determining how to load extensions from a source we follow these rules: - If the source has a `medusa-plugin-options.json` or is the root application we determine that it is a `local` extension source, and load extensions as previously through a virtual module. - If it has neither of the above, but has a `./admin` export in its package.json then we determine that it is a `package` extension, and we update the entry point for the dashboard to import the package and pass its extensions a long to the dashboard manager. **Changes required by plugin authors** - The change has no breaking changes, but requires plugin authors to update the `package.json` of their plugins to also include a `./admin` export. It should look like this: ```json { "name": "@medusajs/plugin", "version": "0.0.1", "description": "A starter for Medusa plugins.", "author": "Medusa (https://medusajs.com)", "license": "MIT", "files": [ ".medusa/server" ], "exports": { "./package.json": "./package.json", "./workflows": "./.medusa/server/src/workflows/index.js", "./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js", "./modules/*": "./.medusa/server/src/modules/*/index.js", "./providers/*": "./.medusa/server/src/providers/*/index.js", "./*": "./.medusa/server/src/*.js", "./admin": { "import": "./.medusa/server/src/admin/index.mjs", "require": "./.medusa/server/src/admin/index.js", "default": "./.medusa/server/src/admin/index.js" } }, } ```
1 parent c105741 commit ec56a8b

File tree

135 files changed

+2762
-2418
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2762
-2418
lines changed

.changeset/cold-keys-press.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@medusajs/admin-vite-plugin": patch
3+
"@medusajs/admin-bundler": patch
4+
"@medusajs/test-utils": patch
5+
"@medusajs/dashboard": patch
6+
"@medusajs/framework": patch
7+
"@medusajs/types": patch
8+
"@medusajs/utils": patch
9+
"@medusajs/medusa": patch
10+
---
11+
12+
fix(medusa,utils,test-utils,types,framework,dashboard,admin-vite-plugin,admib-bundler): Fix broken plugin dependencies in development server

packages/admin/admin-bundler/package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Bundler for the Medusa admin dashboard.",
55
"author": "Kasper Kristensen <[email protected]>",
66
"scripts": {
7-
"build": "tsup && copyfiles -f ./src/index.html ./src/entry.tsx ./src/index.css ./dist"
7+
"build": "tsup"
88
},
99
"main": "./dist/index.js",
1010
"types": "./dist/index.d.ts",
@@ -20,7 +20,6 @@
2020
"devDependencies": {
2121
"@medusajs/types": "2.6.1",
2222
"@types/compression": "^1.7.5",
23-
"copyfiles": "^2.4.1",
2423
"express": "^4.21.0",
2524
"tsup": "^8.0.1",
2625
"typescript": "^5.3.3"
@@ -29,19 +28,16 @@
2928
"@medusajs/admin-shared": "2.6.1",
3029
"@medusajs/admin-vite-plugin": "2.6.1",
3130
"@medusajs/dashboard": "2.6.1",
32-
"@rollup/plugin-node-resolve": "^16.0.0",
3331
"@vitejs/plugin-react": "^4.2.1",
3432
"autoprefixer": "^10.4.16",
3533
"compression": "^1.7.4",
3634
"express": "^4.21.0",
3735
"get-port": "^5.1.1",
3836
"glob": "^10.3.10",
37+
"outdent": "^0.8.0",
3938
"postcss": "^8.4.32",
4039
"tailwindcss": "^3.3.6",
4140
"vite": "^5.4.14"
4241
},
43-
"peerDependencies": {
44-
"react-dom": "^18.0.0"
45-
},
4642
"packageManager": "[email protected]"
4743
}

packages/admin/admin-bundler/src/lib/build.ts packages/admin/admin-bundler/src/commands/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { InlineConfig } from "vite"
22
import { BundlerOptions } from "../types"
3-
import { getViteConfig } from "./config"
3+
import { getViteConfig } from "../utils/config"
44

55
export async function build(options: BundlerOptions) {
66
const vite = await import("vite")

packages/admin/admin-bundler/src/lib/develop.ts packages/admin/admin-bundler/src/commands/develop.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "path"
44
import type { InlineConfig, ViteDevServer } from "vite"
55

66
import { BundlerOptions } from "../types"
7-
import { getViteConfig } from "./config"
7+
import { getViteConfig } from "../utils/config"
88

99
const router = express.Router()
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
"@medusajs/js-sdk",
28+
"@medusajs/admin-sdk",
29+
"@tanstack/react-query",
30+
])
31+
32+
const outDir = path.resolve(options.root, options.outDir, "src/admin")
33+
const entryPoint = path.resolve(
34+
options.root,
35+
"src/admin/__admin-extensions__.js"
36+
)
37+
38+
/**
39+
* We need to ensure that the NODE_ENV is set to production,
40+
* otherwise Vite will build the dev version of React.
41+
*/
42+
const originalNodeEnv = process.env.NODE_ENV
43+
process.env.NODE_ENV = "production"
44+
45+
const pluginConfig: UserConfig = {
46+
build: {
47+
lib: {
48+
entry: entryPoint,
49+
formats: ["es", "cjs"],
50+
fileName: "index",
51+
},
52+
emptyOutDir: false,
53+
minify: false,
54+
outDir,
55+
rollupOptions: {
56+
external: (id, importer) => {
57+
// If there's no importer, it's a direct dependency
58+
// Keep the existing external behavior
59+
if (!importer) {
60+
const idParts = id.split("/")
61+
const name = idParts[0]?.startsWith("@")
62+
? `${idParts[0]}/${idParts[1]}`
63+
: idParts[0]
64+
65+
const builtinModulesWithNodePrefix = [
66+
...builtinModules,
67+
...builtinModules.map((modName) => `node:${modName}`),
68+
]
69+
70+
return Boolean(
71+
(name && external.has(name)) ||
72+
(name && builtinModulesWithNodePrefix.includes(name))
73+
)
74+
}
75+
76+
// For transient dependencies (those with importers),
77+
// bundle them if they're not in our external set
78+
const idParts = id.split("/")
79+
const name = idParts[0]?.startsWith("@")
80+
? `${idParts[0]}/${idParts[1]}`
81+
: idParts[0]
82+
83+
return Boolean(name && external.has(name))
84+
},
85+
output: {
86+
preserveModules: false,
87+
interop: "auto",
88+
chunkFileNames: () => {
89+
return `_chunks/[name]-[hash]`
90+
},
91+
},
92+
},
93+
},
94+
plugins: [
95+
react(),
96+
medusa({
97+
pluginMode: true,
98+
sources: [path.resolve(options.root, "src/admin")],
99+
}),
100+
clearPluginBuild({ outDir }),
101+
],
102+
logLevel: "silent",
103+
clearScreen: false,
104+
}
105+
106+
await vite.build(pluginConfig)
107+
108+
/**
109+
* Restore the original NODE_ENV
110+
*/
111+
process.env.NODE_ENV = originalNodeEnv
112+
}

packages/admin/admin-bundler/src/entry.tsx

-15
This file was deleted.

packages/admin/admin-bundler/src/index.css

-5
This file was deleted.

packages/admin/admin-bundler/src/index.html

-16
This file was deleted.
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { build } from "./lib/build"
2-
export { develop } from "./lib/develop"
3-
export { plugin } from "./lib/plugin"
4-
export { serve } from "./lib/serve"
1+
export { build } from "./commands/build"
2+
export { develop } from "./commands/develop"
3+
export { plugin } from "./commands/plugin"
4+
export { serve } from "./commands/serve"
55

66
export * from "./types"

packages/admin/admin-bundler/src/lib/plugin.ts

-105
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { rm } from "node:fs/promises"
2+
import path from "node:path"
3+
import type { Plugin } from "vite"
4+
5+
interface ClearPluginBuildOptions {
6+
outDir: string
7+
}
8+
9+
export const clearPluginBuild = (options: ClearPluginBuildOptions): Plugin => ({
10+
name: "medusa:clear-plugin-build",
11+
buildStart: async () => {
12+
const adminDir = path.join(options.outDir, "admin")
13+
try {
14+
await rm(adminDir, { recursive: true, force: true })
15+
} catch (e) {
16+
// Directory might not exist, ignore
17+
}
18+
},
19+
})

0 commit comments

Comments
 (0)