test(admin-vite-plugin): React Invalid hook call on Windows (#9647)
This commit is contained in:
1 parent
a0b747e117
commit
82e32f9da4
5 files changed
+17
-87
No files matched your search
@@ -28,7 +28,14 @@ export async function getViteConfig(
|
|||||||
outDir: path.resolve(process.cwd(), options.outDir),
|
outDir: path.resolve(process.cwd(), options.outDir),
|
||||||
},
|
},
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
include: ["react-dom/client", "@medusajs/ui", "@medusajs/dashboard"],
|
include: [
|
||||||
|
"react",
|
||||||
|
"react/jsx-runtime",
|
||||||
|
"react-dom/client",
|
||||||
|
"react-router-dom",
|
||||||
|
"@medusajs/ui",
|
||||||
|
"@medusajs/dashboard",
|
||||||
|
],
|
||||||
exclude: [...VIRTUAL_MODULES],
|
exclude: [...VIRTUAL_MODULES],
|
||||||
},
|
},
|
||||||
define: {
|
define: {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { describe, expect, it, vi } from "vitest"
|
import { describe, expect, it, vi } from "vitest"
|
||||||
|
|
||||||
import { Stats } from "fs"
|
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
import * as utils from "../../utils"
|
import * as utils from "../../utils"
|
||||||
import { generateRoutes } from "../generate-routes"
|
import { generateRoutes } from "../generate-routes"
|
||||||
@@ -55,27 +54,10 @@ const expectedRoutesWithoutLoaders = `
|
|||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
Component: RouteComponent0,
|
Component: RouteComponent0,
|
||||||
loader: undefined,
|
|
||||||
path: "/one",
|
path: "/one",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Component: RouteComponent1,
|
Component: RouteComponent1,
|
||||||
loader: undefined,
|
|
||||||
path: "/two",
|
|
||||||
}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
|
|
||||||
const expectedRoutesWithLoaders = `
|
|
||||||
routes: [
|
|
||||||
{
|
|
||||||
Component: RouteComponent0,
|
|
||||||
loader: RouteLoader0,
|
|
||||||
path: "/one",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Component: RouteComponent1,
|
|
||||||
loader: RouteLoader1,
|
|
||||||
path: "/two",
|
path: "/two",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -102,26 +84,6 @@ describe("generateRoutes", () => {
|
|||||||
utils.normalizeString(expectedRoutesWithoutLoaders)
|
utils.normalizeString(expectedRoutesWithoutLoaders)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
it("should generate routes with loaders", async () => {
|
|
||||||
const mockFiles = [
|
|
||||||
"Users/user/medusa/src/admin/routes/one/page.tsx",
|
|
||||||
"Users/user/medusa/src/admin/routes/two/page.tsx",
|
|
||||||
]
|
|
||||||
vi.mocked(utils.crawl).mockResolvedValue(mockFiles)
|
|
||||||
|
|
||||||
vi.mocked(fs.readFile).mockImplementation(async (file) =>
|
|
||||||
Promise.resolve(mockFileContents[mockFiles.indexOf(file as string)])
|
|
||||||
)
|
|
||||||
|
|
||||||
vi.mocked(fs.stat).mockResolvedValue({} as Stats) // We just want to mock that the check passes
|
|
||||||
|
|
||||||
const result = await generateRoutes(
|
|
||||||
new Set(["Users/user/medusa/src/admin"])
|
|
||||||
)
|
|
||||||
expect(utils.normalizeString(result.code)).toEqual(
|
|
||||||
utils.normalizeString(expectedRoutesWithLoaders)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
it("should handle windows paths", async () => {
|
it("should handle windows paths", async () => {
|
||||||
const mockFiles = [
|
const mockFiles = [
|
||||||
"C:\\medusa\\src\\admin\\routes\\one\\page.tsx",
|
"C:\\medusa\\src\\admin\\routes\\one\\page.tsx",
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import { getRoute } from "./helpers"
|
|||||||
|
|
||||||
type Route = {
|
type Route = {
|
||||||
Component: string
|
Component: string
|
||||||
loader?: string
|
|
||||||
path: string
|
path: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +45,6 @@ function generateCode(results: RouteResult[]): string {
|
|||||||
function formatRoute(route: Route): string {
|
function formatRoute(route: Route): string {
|
||||||
return `{
|
return `{
|
||||||
Component: ${route.Component},
|
Component: ${route.Component},
|
||||||
loader: ${route.loader ? route.loader : "undefined"},
|
|
||||||
path: "${route.path}",
|
path: "${route.path}",
|
||||||
}`
|
}`
|
||||||
}
|
}
|
||||||
@@ -77,11 +75,10 @@ async function parseFile(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const loaderPath = await getLoader(file)
|
|
||||||
const routePath = getRoute(file)
|
const routePath = getRoute(file)
|
||||||
|
|
||||||
const imports = generateImports(file, loaderPath, index)
|
const imports = generateImports(file, index)
|
||||||
const route = generateRoute(routePath, loaderPath, index)
|
const route = generateRoute(routePath, index)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
imports,
|
imports,
|
||||||
@@ -103,45 +100,19 @@ async function isValidRouteFile(file: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLoader(file: string): Promise<string | null> {
|
function generateImports(file: string, index: number): string[] {
|
||||||
const loaderExtensions = ["ts", "js", "tsx", "jsx"]
|
|
||||||
for (const ext of loaderExtensions) {
|
|
||||||
const loaderPath = file.replace(/\/page\.(tsx|jsx)/, `/loader.${ext}`)
|
|
||||||
const exists = await fs.stat(loaderPath).catch(() => null)
|
|
||||||
if (exists) {
|
|
||||||
return loaderPath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateImports(
|
|
||||||
file: string,
|
|
||||||
loader: string | null,
|
|
||||||
index: number
|
|
||||||
): string[] {
|
|
||||||
const imports: string[] = []
|
const imports: string[] = []
|
||||||
const route = generateRouteComponentName(index)
|
const route = generateRouteComponentName(index)
|
||||||
const importPath = normalizePath(file)
|
const importPath = normalizePath(file)
|
||||||
|
|
||||||
imports.push(`import ${route} from "${importPath}"`)
|
imports.push(`import ${route} from "${importPath}"`)
|
||||||
|
|
||||||
if (loader) {
|
|
||||||
const loaderName = generateRouteLoaderName(index)
|
|
||||||
imports.push(`import ${loaderName} from "${normalizePath(loader)}"`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return imports
|
return imports
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateRoute(
|
function generateRoute(route: string, index: number): Route {
|
||||||
route: string,
|
|
||||||
loader: string | null,
|
|
||||||
index: number
|
|
||||||
): Route {
|
|
||||||
return {
|
return {
|
||||||
Component: generateRouteComponentName(index),
|
Component: generateRouteComponentName(index),
|
||||||
loader: loader ? generateRouteLoaderName(index) : undefined,
|
|
||||||
path: route,
|
path: route,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,7 +120,3 @@ function generateRoute(
|
|||||||
function generateRouteComponentName(index: number): string {
|
function generateRouteComponentName(index: number): string {
|
||||||
return `RouteComponent${index}`
|
return `RouteComponent${index}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateRouteLoaderName(index: number): string {
|
|
||||||
return `RouteLoader${index}`
|
|
||||||
}
|
|
||||||
@@ -55,7 +55,6 @@ export type VirtualModule =
|
|||||||
| typeof ROUTE_VIRTUAL_MODULE
|
| typeof ROUTE_VIRTUAL_MODULE
|
||||||
| typeof MENU_ITEM_VIRTUAL_MODULE
|
| typeof MENU_ITEM_VIRTUAL_MODULE
|
||||||
| typeof WIDGET_VIRTUAL_MODULE
|
| typeof WIDGET_VIRTUAL_MODULE
|
||||||
|
|
||||||
const resolvedVirtualModuleIds = {
|
const resolvedVirtualModuleIds = {
|
||||||
link: RESOLVED_LINK_VIRTUAL_MODULE,
|
link: RESOLVED_LINK_VIRTUAL_MODULE,
|
||||||
form: RESOLVED_FORM_VIRTUAL_MODULE,
|
form: RESOLVED_FORM_VIRTUAL_MODULE,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ComponentType } from "react"
|
import { ComponentType } from "react"
|
||||||
import { LoaderFunction, RouteObject } from "react-router-dom"
|
import { RouteObject } from "react-router-dom"
|
||||||
import { ErrorBoundary } from "../../components/utilities/error-boundary"
|
import { ErrorBoundary } from "../../components/utilities/error-boundary"
|
||||||
import { RouteExtension, RouteModule } from "../types"
|
import { RouteExtension, RouteModule } from "../types"
|
||||||
|
|
||||||
@@ -30,8 +30,7 @@ export const createRouteMap = (
|
|||||||
const addRoute = (
|
const addRoute = (
|
||||||
pathSegments: string[],
|
pathSegments: string[],
|
||||||
Component: ComponentType,
|
Component: ComponentType,
|
||||||
currentLevel: RouteObject[],
|
currentLevel: RouteObject[]
|
||||||
loader?: LoaderFunction
|
|
||||||
) => {
|
) => {
|
||||||
if (!pathSegments.length) {
|
if (!pathSegments.length) {
|
||||||
return
|
return
|
||||||
@@ -51,26 +50,22 @@ export const createRouteMap = (
|
|||||||
path: "",
|
path: "",
|
||||||
ErrorBoundary: ErrorBoundary,
|
ErrorBoundary: ErrorBoundary,
|
||||||
async lazy() {
|
async lazy() {
|
||||||
if (loader) {
|
|
||||||
return { Component, loader }
|
|
||||||
}
|
|
||||||
|
|
||||||
return { Component }
|
return { Component }
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
route.children ||= []
|
route.children ||= []
|
||||||
addRoute(remainingSegments, Component, route.children, loader)
|
addRoute(remainingSegments, Component, route.children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
routes.forEach(({ path, Component, loader }) => {
|
routes.forEach(({ path, Component }) => {
|
||||||
// Remove the ignore segment from the path if it is provided
|
// Remove the ignore segment from the path if it is provided
|
||||||
const cleanedPath = ignore
|
const cleanedPath = ignore
|
||||||
? path.replace(ignore, "").replace(/^\/+/, "")
|
? path.replace(ignore, "").replace(/^\/+/, "")
|
||||||
: path.replace(/^\/+/, "")
|
: path.replace(/^\/+/, "")
|
||||||
const pathSegments = cleanedPath.split("/").filter(Boolean)
|
const pathSegments = cleanedPath.split("/").filter(Boolean)
|
||||||
addRoute(pathSegments, Component, root, loader)
|
addRoute(pathSegments, Component, root)
|
||||||
})
|
})
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|||||||
Reference in new issue
Block a user