Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(admin-bundler): Fix Vite imports to prevent deprecation warnings #11086

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-books-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/admin-bundler": patch
---

fix(admin-bundler): Fix Vite imports to prevent deprecation warnings
4 changes: 2 additions & 2 deletions packages/admin/admin-bundler/src/lib/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import react from "@vitejs/plugin-react"
import { readFileSync } from "fs"
import { rm } from "fs/promises"
import { glob } from "glob"
import path from "path"
import { UserConfig } from "vite"
import type { UserConfig } from "vite"

interface PluginOptions {
root: string
Expand All @@ -12,6 +11,7 @@ interface PluginOptions {

export async function plugin(options: PluginOptions) {
const vite = await import("vite")
const react = (await import("@vitejs/plugin-react")).default
const entries = await glob(`${options.root}/src/admin/**/*.{ts,tsx,js,jsx}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: how come dynamic import fix it? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats how they want people to import Vite going forward if you need to use CJS. This way it will still work when they no longer have a CJS export in their package, whenever they do decide to not include it anymore

Dynamically import Vite: If you need to keep using CJS, you can dynamically import Vite using import('vite') instead. This requires your code to be written in an async context, but should still be manageable as Vite's API is mostly asynchronous.

https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated

The React plugin imports Vite, so we also need to import it dynamically the ensure Vite is only ever imported that way.


/**
Expand Down
Loading