Skip to content

Commit

Permalink
Fix to not build custom server during blitz start (#2408)
Browse files Browse the repository at this point in the history
(patch)
  • Loading branch information
mabadir authored Jun 8, 2021
1 parent 22a845e commit 1ef378b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {copy} from "fs-extra"
import {resolve} from "path"
import {saveBlitzVersion} from "./blitz-version"
import {normalize, ServerConfig} from "./config"
import {nextBuild} from "./next-utils"
import {buildCustomServer, customServerExists, nextBuild} from "./next-utils"
import {configureStages} from "./stages"

export async function build(config: ServerConfig) {
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function build(config: ServerConfig) {
})

await saveBlitzVersion(buildFolder)

if (customServerExists()) await buildCustomServer({watch})
await nextBuild(nextBin, buildFolder, manifest, config)

const rootNextFolder = resolve(rootFolder, ".next")
Expand Down
67 changes: 44 additions & 23 deletions packages/server/src/next-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,46 @@ interface CustomServerOptions {
watch?: boolean
}

const getEsbuildOptions = (): esbuild.BuildOptions => {
const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))
return {
entryPoints: [getCustomServerPath()],
outfile: getCustomServerBuildPath(),
format: "cjs",
bundle: true,
platform: "node",
external: [
"blitz",
"next",
...Object.keys(require("blitz/package").dependencies),
...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
],
}
}

export function buildCustomServer({watch}: CustomServerOptions = {}) {
const esbuildOptions = getEsbuildOptions()
if (watch) {
esbuildOptions.watch = {
onRebuild(error) {
if (error) {
log.error("Failed to re-build custom server")
} else {
log.newline()
log.progress("Custom server changed - rebuilding...")
}
},
}
}
return esbuild.build(esbuildOptions)
}

export function startCustomServer(
cwd: string,
config: ServerConfig,
{watch}: CustomServerOptions = {},
) {
const serverSrcPath = getCustomServerPath()
const serverBuildPath = getCustomServerBuildPath()

let spawnEnv = getSpawnEnv(config)
Expand Down Expand Up @@ -283,24 +317,9 @@ export function startCustomServer(
})
}

const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))

const esbuildOptions: esbuild.BuildOptions = {
entryPoints: [serverSrcPath],
outfile: getCustomServerBuildPath(),
format: "cjs",
bundle: true,
platform: "node",
external: [
"blitz",
"next",
...Object.keys(require("blitz/package").dependencies),
...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
],
}

if (watch) {
// Handle build & Starting server
const esbuildOptions = getEsbuildOptions()
esbuildOptions.watch = {
onRebuild(error) {
if (error) {
Expand All @@ -315,11 +334,13 @@ export function startCustomServer(
}
},
}
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
esbuild.build(esbuildOptions).then(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
esbuild.build(esbuildOptions).then(() => {
spawnServer()
})
} else {
// No build required, Start server
spawnServer()
})
}
})
}
2 changes: 2 additions & 0 deletions packages/server/test/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {multiMock} from "./utils/multi-mock"
const mocks = multiMock(
{
"next-utils": {
customServerExists: jest.fn().mockReturnValue(Boolean),
buildCustomServer: jest.fn().mockReturnValue(Promise.resolve()),
nextBuild: jest.fn().mockReturnValue(Promise.resolve()),
},
"resolve-bin-async": {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/test/vercel-now.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const mocks = multiMock(
{
"next-utils": {
nextStartDev: jest.fn().mockReturnValue(Promise.resolve()),
customServerExists: jest.fn().mockReturnValue(Boolean),
buildCustomServer: jest.fn().mockReturnValue(Promise.resolve()),
nextBuild: jest.fn().mockReturnValue(Promise.resolve()),
},
"resolve-bin-async": {
Expand Down

0 comments on commit 1ef378b

Please sign in to comment.