diff --git a/packages/react-email/src/utils/preview/start-dev-server.ts b/packages/react-email/src/utils/preview/start-dev-server.ts index 49d4c160ad..7768654fd3 100644 --- a/packages/react-email/src/utils/preview/start-dev-server.ts +++ b/packages/react-email/src/utils/preview/start-dev-server.ts @@ -1,4 +1,6 @@ +import { existsSync, promises as fs } from 'node:fs'; import http from 'node:http'; +import os from 'node:os'; import path from 'node:path'; import url from 'node:url'; import { createJiti } from 'jiti'; @@ -135,6 +137,24 @@ export const startDevServer = async ( conf.get('resendApiKey'), ), }; + if (!process.env.ESBUILD_BINARY_PATH) { + try { + const esbuild = createJiti(previewServer.esmResolve('esbuild')); + const platformPackage = `@esbuild/${process.platform}-${os.arch()}`; + const subpath = + process.platform === 'win32' ? 'esbuild.exe' : 'bin/esbuild'; + const candidateBinaryPath = path.join( + await fs.realpath(esbuild.esmResolve(platformPackage)), + subpath, + ); + if (existsSync(candidateBinaryPath)) { + process.env.ESBUILD_BINARY_PATH = + await fs.realpath(candidateBinaryPath); + } + } catch (exception) { + console.error(exception); + } + } const next = await previewServer.import( 'next', @@ -194,21 +214,21 @@ const makeExitHandler = | { shouldKillProcess: false } | { shouldKillProcess: true; killWithErrorCode: boolean }, ) => - (codeSignalOrError: number | NodeJS.Signals | Error) => { - if (typeof devServer !== 'undefined') { - console.log('\nshutting down dev server'); - devServer.close(); - devServer = undefined; - } + (codeSignalOrError: number | NodeJS.Signals | Error) => { + if (typeof devServer !== 'undefined') { + console.log('\nshutting down dev server'); + devServer.close(); + devServer = undefined; + } - if (codeSignalOrError instanceof Error) { - console.error(codeSignalOrError); - } + if (codeSignalOrError instanceof Error) { + console.error(codeSignalOrError); + } - if (options?.shouldKillProcess) { - process.exit(options.killWithErrorCode ? 1 : 0); - } - }; + if (options?.shouldKillProcess) { + process.exit(options.killWithErrorCode ? 1 : 0); + } + }; // do something when app is closing process.on('exit', makeExitHandler());