Skip to content

Commit b77e66a

Browse files
authored
fix(cli): Call the CJS build of api-server for CJS projects (#431)
1 parent 9924656 commit b77e66a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/cli/src/commands/__tests__/serve.test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { vi, describe, afterEach, it, expect } from 'vitest'
1+
import { vi, describe, afterEach, beforeEach, it, expect } from 'vitest'
22
import yargs from 'yargs/yargs'
33

44
import * as apiServerCLIConfig from '@cedarjs/api-server/apiCliConfig'
55
import * as bothServerCLIConfig from '@cedarjs/api-server/bothCliConfig'
6+
import * as apiServerCLIConfigHandler from '@cedarjs/api-server/cjs/apiCliConfigHandler'
67

78
import { builder } from '../serve.js'
89

910
globalThis.__dirname = __dirname
1011

12+
const mocks = vi.hoisted(() => ({
13+
isEsm: true,
14+
}))
15+
1116
// We mock these to skip the check for web/dist and api/dist
1217
vi.mock('@cedarjs/project-config', async (importOriginal) => {
1318
const originalProjectConfig = await importOriginal()
@@ -31,6 +36,7 @@ vi.mock('@cedarjs/project-config', async (importOriginal) => {
3136
api: {},
3237
}
3338
},
39+
projectIsEsm: () => mocks.isEsm,
3440
}
3541
})
3642

@@ -58,6 +64,11 @@ vi.mock('@cedarjs/api-server/apiCliConfig', async (importOriginal) => {
5864
handler: vi.fn(),
5965
}
6066
})
67+
vi.mock('@cedarjs/api-server/cjs/apiCliConfigHandler', async () => {
68+
return {
69+
handler: vi.fn(),
70+
}
71+
})
6172
vi.mock('@cedarjs/api-server/bothCliConfig', async (importOriginal) => {
6273
const originalBothCLIConfig = await importOriginal()
6374
return {
@@ -74,6 +85,10 @@ vi.mock('execa', () => ({
7485
}))
7586

7687
describe('yarn rw serve', () => {
88+
beforeEach(() => {
89+
mocks.isEsm = true
90+
})
91+
7792
afterEach(() => {
7893
vi.clearAllMocks()
7994
})
@@ -91,6 +106,21 @@ describe('yarn rw serve', () => {
91106
)
92107
})
93108

109+
it('Should proxy serve api with params to api-server handler for CJS projects', async () => {
110+
mocks.isEsm = false
111+
112+
const parser = yargs().command('serve [side]', false, builder)
113+
114+
await parser.parse('serve api --port 5555 --apiRootPath funkyFunctions')
115+
116+
expect(apiServerCLIConfigHandler.handler).toHaveBeenCalledWith(
117+
expect.objectContaining({
118+
port: 5555,
119+
apiRootPath: expect.stringMatching(/^\/?funkyFunctions\/?$/),
120+
}),
121+
)
122+
})
123+
94124
it('Should proxy serve api with params to api-server handler (alias and slashes in path)', async () => {
95125
const parser = yargs().command('serve [side]', false, builder)
96126

@@ -106,6 +136,23 @@ describe('yarn rw serve', () => {
106136
)
107137
})
108138

139+
it('Should proxy serve api with params to api-server handler (alias and slashes in path) for CJS projects', async () => {
140+
mocks.isEsm = false
141+
142+
const parser = yargs().command('serve [side]', false, builder)
143+
144+
await parser.parse(
145+
'serve api --port 5555 --rootPath funkyFunctions/nested/',
146+
)
147+
148+
expect(apiServerCLIConfigHandler.handler).toHaveBeenCalledWith(
149+
expect.objectContaining({
150+
port: 5555,
151+
rootPath: expect.stringMatching(/^\/?funkyFunctions\/nested\/$/),
152+
}),
153+
)
154+
})
155+
109156
it('Should proxy rw serve with params to appropriate handler', async () => {
110157
const parser = yargs().command('serve [side]', false, builder)
111158

packages/cli/src/commands/serve.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { terminalLink } from 'termi-link'
66
import * as apiServerCLIConfig from '@cedarjs/api-server/apiCliConfig'
77
import * as bothServerCLIConfig from '@cedarjs/api-server/bothCliConfig'
88
import { recordTelemetryAttributes } from '@cedarjs/cli-helpers'
9+
import { projectIsEsm } from '@cedarjs/project-config'
910
import * as webServerCLIConfig from '@cedarjs/web-server'
1011

1112
import c from '../lib/colors.js'
@@ -69,7 +70,14 @@ export const builder = async (yargs) => {
6970
const { apiServerFileHandler } = await import('./serveApiHandler.js')
7071
await apiServerFileHandler(argv)
7172
} else {
72-
await apiServerCLIConfig.handler(argv)
73+
if (!projectIsEsm()) {
74+
const { handler } = await import(
75+
'@cedarjs/api-server/cjs/apiCliConfigHandler'
76+
)
77+
await handler(argv)
78+
} else {
79+
await apiServerCLIConfig.handler(argv)
80+
}
7381
}
7482
},
7583
})
@@ -89,6 +97,8 @@ export const builder = async (yargs) => {
8997
if (streamingEnabled) {
9098
await webSsrServerHandler(rscEnabled)
9199
} else {
100+
// @cedarjs/web-server is still built as CJS only, so we don't need
101+
// the same solution here as we do for the api side
92102
await webServerCLIConfig.handler(argv)
93103
}
94104
},

0 commit comments

Comments
 (0)