Skip to content

Commit

Permalink
fix: loader support for server-only (#6383)
Browse files Browse the repository at this point in the history
## Description

Allows `server-only` to work within the Payload loader.

Fixes payloadcms/payload-3.0-demo#218
  • Loading branch information
jmikrut committed May 17, 2024
1 parent e418525 commit 5083525
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/payload/src/bin/loader/ignores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const specifiersToIgnore = ['server-only']
11 changes: 7 additions & 4 deletions packages/payload/src/bin/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath, pathToFileURL } from 'url'

import { CLIENT_EXTENSIONS } from './clientExtensions.js'
import { compile } from './compile.js'
import { specifiersToIgnore } from './ignores.js'
import { resolveOriginalPath } from './resolveOriginalPath.js'

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

// If a client file is resolved, we'll set `format: client`
// If it's a client file, or a file to be ignored is resolved,
// we'll set `format: 'ignore'`
// and short circuit, so the load step
// will return source code of empty object
if (isClient) {
if (isClient || shouldIgnore) {
return {
format: 'client',
format: 'ignore',
shortCircuit: true,
// No need to resolve the URL using nextResolve. We ignore client files anyway.
// 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
Expand Down Expand Up @@ -147,7 +150,7 @@ const swcOptions = {
paths: undefined,
}
export const load: LoadFn = async (url, context, nextLoad) => {
if (context.format === 'client') {
if (context.format === 'ignore') {
const rawSource = 'export default {}'

return {
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/loader/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe('Loader', () => {
expect(code).toStrictEqual(0)
})

it('should import server-only without breaking', async () => {
const code = await startChildProcess('./server-only-test.ts')
expect(code).toStrictEqual(0)
})

it('should import configs that rely on custom components', async () => {
const code = await startChildProcess('../admin/config.ts')
expect(code).toStrictEqual(0)
Expand Down
3 changes: 3 additions & 0 deletions test/loader/server-only-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'server-only'

console.log('woo')
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"execa": "5.1.1",
"http-status": "1.6.2",
"payload": "workspace:*",
"server-only": "^0.0.1",
"tempy": "^1.0.1",
"ts-essentials": "7.0.3",
"typescript": "5.4.5",
Expand Down

0 comments on commit 5083525

Please sign in to comment.