Skip to content

Commit 5083525

Browse files
authored
fix: loader support for server-only (#6383)
## Description Allows `server-only` to work within the Payload loader. Fixes payloadcms/payload-3.0-demo#218
1 parent e418525 commit 5083525

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const specifiersToIgnore = ['server-only']

packages/payload/src/bin/loader/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath, pathToFileURL } from 'url'
55

66
import { CLIENT_EXTENSIONS } from './clientExtensions.js'
77
import { compile } from './compile.js'
8+
import { specifiersToIgnore } from './ignores.js'
89
import { resolveOriginalPath } from './resolveOriginalPath.js'
910

1011
interface ResolveContext {
@@ -55,13 +56,15 @@ const TS_EXTENSIONS: string[] = [
5556
export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
5657
const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext))
5758
const isClient = CLIENT_EXTENSIONS.some((ext) => specifier.endsWith(ext))
59+
const shouldIgnore = specifiersToIgnore.includes(specifier)
5860

59-
// If a client file is resolved, we'll set `format: client`
61+
// If it's a client file, or a file to be ignored is resolved,
62+
// we'll set `format: 'ignore'`
6063
// and short circuit, so the load step
6164
// will return source code of empty object
62-
if (isClient) {
65+
if (isClient || shouldIgnore) {
6366
return {
64-
format: 'client',
67+
format: 'ignore',
6568
shortCircuit: true,
6669
// No need to resolve the URL using nextResolve. We ignore client files anyway.
6770
// Additionally, nextResolve throws an error if the URL is a TS path, so we'd have to use TypeScript's resolveModuleName to resolve the URL, which is unnecessary
@@ -147,7 +150,7 @@ const swcOptions = {
147150
paths: undefined,
148151
}
149152
export const load: LoadFn = async (url, context, nextLoad) => {
150-
if (context.format === 'client') {
153+
if (context.format === 'ignore') {
151154
const rawSource = 'export default {}'
152155

153156
return {

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/loader/int.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ describe('Loader', () => {
1111
expect(code).toStrictEqual(0)
1212
})
1313

14+
it('should import server-only without breaking', async () => {
15+
const code = await startChildProcess('./server-only-test.ts')
16+
expect(code).toStrictEqual(0)
17+
})
18+
1419
it('should import configs that rely on custom components', async () => {
1520
const code = await startChildProcess('../admin/config.ts')
1621
expect(code).toStrictEqual(0)

test/loader/server-only-test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'server-only'
2+
3+
console.log('woo')

test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"execa": "5.1.1",
4949
"http-status": "1.6.2",
5050
"payload": "workspace:*",
51+
"server-only": "^0.0.1",
5152
"tempy": "^1.0.1",
5253
"ts-essentials": "7.0.3",
5354
"typescript": "5.4.5",

0 commit comments

Comments
 (0)