Files
payloadcms/packages/payload/bin.js
Alessio Gravili cb7fa00a6f fix: use tsx instead of swc as default bin script transpiler, as swc errors when it encounters 'next/cache' (#7681)
Fixes https://github.com/payloadcms/payload/issues/7677

- Payload bin scripts were not properly working on windows
- Use tsx by default instead of swc, as swc does not handle next/cache
imports without the .js at the end
- Support other node runtimes through --disable-transpile flag
2024-08-14 16:40:31 -04:00

55 lines
1.4 KiB
JavaScript
Executable File

#!/usr/bin/env node --no-deprecation
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
const useSwc = process.argv.includes('--use-swc')
const disableTranspile = process.argv.includes('--disable-transpile')
if (disableTranspile) {
// Remove --disable-transpile from arguments
process.argv = process.argv.filter((arg) => arg !== '--disable-transpile')
const start = async () => {
const { bin } = await import('./dist/bin/index.js')
await bin()
}
void start()
} else {
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const url = pathToFileURL(dirname).toString() + '/'
if (!useSwc) {
const start = async () => {
// Use tsx
let tsImport = (await import('tsx/esm/api')).tsImport
const { bin } = await tsImport('./dist/bin/index.js', url)
await bin()
}
void start()
} else if (useSwc) {
const { register } = await import('node:module')
// Remove --use-swc from arguments
process.argv = process.argv.filter((arg) => arg !== '--use-swc')
try {
register('@swc-node/register/esm', url)
} catch (_) {
console.error(
'@swc-node/register is not installed. Please install @swc-node/register in your project, if you want to use swc in payload run.',
)
}
const start = async () => {
const { bin } = await import('./dist/bin/index.js')
await bin()
}
void start()
}
}