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

chore(filepath): Use node:url to convert to file path #10475

Merged
merged 4 commits into from
Apr 18, 2024
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
35 changes: 26 additions & 9 deletions packages/vite/src/middleware/createMiddlewareRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createMiddlewareRouter } from './register'

vi.mock('@redwoodjs/project-config', async () => {
const mockWin32Paths = {
web: {
base: 'C:\\proj\\web',
dist: 'C:\\proj\\web\\dist',
distEntryServer: 'C:\\proj\\web\\dist\\entry-server.mjs',
entryServer: 'C:\\proj\\web\\entry-server.tsx',
},
}
const mockUnixPaths = {
web: {
base: '/proj/web',
dist: '/proj/web/dist',
distEntryServer: '/proj/web/dist/entry-server.mjs',
entryServer: '/proj/web/entry-server.tsx',
},
}

return {
getPaths: () => {
return {
web: {
base: '/proj/web',
dist: '/proj/web/dist',
distEntryServer: '/proj/web/dist/entry-server.mjs',
entryServer: '/proj/web/entry-server.tsx',
},
}
return process.platform === 'win32' ? mockWin32Paths : mockUnixPaths
},
}
})
Expand All @@ -24,6 +34,11 @@ vi.mock('/proj/web/dist/entry-server.mjs', () => {
registerMiddleware: distRegisterMwMock,
}
})
vi.mock('/C:/proj/web/dist/entry-server.mjs', () => {
return {
registerMiddleware: distRegisterMwMock,
}
})

describe('createMiddlewareRouter', () => {
beforeEach(() => {
Expand All @@ -42,7 +57,9 @@ describe('createMiddlewareRouter', () => {
await createMiddlewareRouter(mockVite)

expect(mockVite.ssrLoadModule).toHaveBeenCalledWith(
'/proj/web/entry-server.tsx',
// '/proj/web/entry-server.tsx'
// 'C:\proj\web\entry-server.tsx'
expect.stringMatching(/^.?.?[/\\]proj[/\\]web[/\\]entry-server.tsx$/),
)
})

Expand Down
11 changes: 8 additions & 3 deletions packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { pathToFileURL } from 'node:url'

import type { ViteDevServer } from 'vite'

import { getPaths } from '@redwoodjs/project-config'
Expand All @@ -24,9 +26,12 @@ export function ensureProcessDirWeb(webDir: string = getPaths().web.base) {
}
}

export function makeFilePath(path: string): string {
// Without this, absolute paths can't be imported on Windows
return 'file:///' + path
/**
* Converts a file path to a URL path (file://...)
* Without this, absolute paths can't be imported on Windows
*/
export function makeFilePath(path: string) {
return pathToFileURL(path).href
}

export async function ssrLoadEntryServer(viteDevServer: ViteDevServer) {
Expand Down
Loading