Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Update build and serve to handle the absence of the api side #10265

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changesets/10265.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- fix(cli): Update build and serve to handle the absence of the api side (#10265) by @Josh-Walker-GM

This change allows you to successfully build your app with `yarn rw build` even when you choose to totally delete you api side. Previously you would have seen errors related to both Prisma and GraphQL. It also improves the error messages produced by `yarn rw serve` in this case.
2 changes: 1 addition & 1 deletion __fixtures__/test-project/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"autoprefixer": "^10.4.18",
"postcss": "^8.4.36",
"postcss": "^8.4.37",
"postcss-loader": "^8.1.1",
"prettier-plugin-tailwindcss": "^0.5.12",
"tailwindcss": "^3.4.1"
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/commands/__tests__/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ vi.mock('@redwoodjs/project-config', async (importOriginal) => {
return {
api: {
dist: '/mocked/project/api/dist',
dbSchema: '/mocked/project/api/db/schema.prisma',
},
web: {
dist: '/mocked/project/web/dist',
Expand All @@ -23,6 +24,22 @@ vi.mock('@redwoodjs/project-config', async (importOriginal) => {
}
})

vi.mock('fs-extra', async () => {
const actualFs = await vi.importActual('fs-extra')
return {
default: {
...actualFs,
// Mock the existence of the Prisma schema file
existsSync: (path) => {
if (path === '/mocked/project/api/db/schema.prisma') {
return true
}
return actualFs.existsSync(path)
},
},
}
})

import { Listr } from 'listr2'
import { vi, afterEach, test, expect } from 'vitest'

Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/commands/buildHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ export const handler = async ({
return
}

const prismaSchemaExists = fs.existsSync(rwjsPaths.api.dbSchema)
const prerenderRoutes =
prerender && side.includes('web') ? detectPrerenderRoutes() : []
const shouldGeneratePrismaClient =
prisma && (side.includes('api') || prerenderRoutes.length > 0)
prisma &&
prismaSchemaExists &&
(side.includes('api') || prerenderRoutes.length > 0)

const tasks = [
shouldGeneratePrismaClient && {
Expand Down Expand Up @@ -208,7 +211,7 @@ export const handler = async ({
await timedTelemetry(process.argv, { type: 'build' }, async () => {
await jobs.run()

if (side.includes('web') && prerender) {
if (side.includes('web') && prerender && prismaSchemaExists) {
// This step is outside Listr so that it prints clearer, complete messages
await triggerPrerender()
}
Expand Down
64 changes: 42 additions & 22 deletions packages/cli/src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,50 @@ export const builder = async (yargs) => {
process.exit(1)
}

if (
positionalArgs.includes('api') &&
!fs.existsSync(path.join(getPaths().api.dist))
) {
console.error(
c.error(
'\n Please run `yarn rw build api` before trying to serve api. \n',
),
)
process.exit(1)
const apiSideExists = fs.existsSync(getPaths().api.base)
if (positionalArgs.includes('api')) {
if (!apiSideExists) {
console.error(
c.error(
'\n Unable to serve the api side as no `api` folder exists. \n',
),
)
process.exit(1)
}

if (!fs.existsSync(path.join(getPaths().api.dist))) {
console.error(
c.error(
'\n Please run `yarn rw build api` before trying to serve api. \n',
),
)
process.exit(1)
}
}

if (
// serve both
positionalArgs.length === 1 &&
(!fs.existsSync(path.join(getPaths().api.dist)) ||
!fs.existsSync(path.join(getPaths().web.dist), 'index.html'))
) {
console.error(
c.error(
'\n Please run `yarn rw build` before trying to serve your redwood app. \n',
),
)
process.exit(1)
// serve both
if (positionalArgs.length === 1) {
if (!apiSideExists) {
console.error(
c.error(
'\n Unable to serve the both sides as no `api` folder exists. Please use `yarn rw serve web` instead. \n',
),
)
process.exit(1)
}

// We need the web side to have been built
if (
!fs.existsSync(path.join(getPaths().api.dist)) ||
!fs.existsSync(path.join(getPaths().web.dist), 'index.html')
) {
console.error(
c.error(
'\n Please run `yarn rw build` before trying to serve your redwood app. \n',
),
)
process.exit(1)
}
}

// Set NODE_ENV to production, if not set
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export default function redwoodPluginVite(): PluginOption[] {

// If realtime is enabled, we want to include the sseLink in the bundle.
// Right now the only way we have of telling is if the package is installed on the api side.
const realtimeEnabled = fs
.readFileSync(path.join(rwPaths.api.base, 'package.json'), 'utf-8')
.includes('@redwoodjs/realtime')
const apiPackageJsonPath = path.join(rwPaths.api.base, 'package.json')
const realtimeEnabled =
fs.existsSync(apiPackageJsonPath) &&
fs.readFileSync(apiPackageJsonPath, 'utf-8').includes('@redwoodjs/realtime')

const streamingEnabled = rwConfig.experimental.streamingSsr.enabled

Expand Down
Loading