Skip to content

Commit

Permalink
fix: Passes correct path to import modules on Windows started with fi…
Browse files Browse the repository at this point in the history
…le:// (#5919)
  • Loading branch information
r1tsuu authored Apr 19, 2024
1 parent 3af3a91 commit 2729728
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/payload/src/utilities/importWithoutClientFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { register } from 'node:module'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { URL, fileURLToPath, pathToFileURL } from 'node:url'
import path from 'path'

import type { SanitizedConfig } from '../config/types.js'
Expand All @@ -8,10 +8,11 @@ const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

export const importWithoutClientFiles = async <T = unknown>(filePath: string) => {
const url = pathToFileURL(filePath).toString()
const { href: filePathUrl } = pathToFileURL(filePath)
const { href: loaderUrl } = pathToFileURL(path.resolve(dirname, '../../dist/bin/loader/index.js'))

register(path.resolve(dirname, '../../dist/bin/loader/index.js'), url)
const result = await import(filePath)
register(loaderUrl, filePathUrl)
const result = await import(filePathUrl)
return result as T
}

Expand All @@ -25,7 +26,12 @@ export const importConfig = async (configPath: string) => {
return await config.default
}

const callerDir = path.dirname(getCallerInfo()[1].getFileName()).replace('file://', '')
const callerFilename = getCallerInfo()[1].getFileName()

const url = new URL(callerFilename)

const callerDir = path.dirname(fileURLToPath(url))

const fullConfigPath = path.resolve(callerDir, configPath)

const config = await importWithoutClientFiles<{ default: Promise<SanitizedConfig> }>(
Expand Down

0 comments on commit 2729728

Please sign in to comment.