Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(build): restore SSR noExternal on Vercel server bundles after v8_viteEnvironmentApi (#1593)

Brad Barbin842a96937e81
diff --git a/Dockerfile b/Dockerfile
index 9645d2a183..ede900acff 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -9,6 +9,10 @@ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc turbo.json lingui.co
COPY apps ./apps
COPY packages ./packages
COPY patches ./patches
+# scripts/ is needed by the postinstall (pnpm run generate:mcp → tsx
+# scripts/generate-mcp.ts) and by the //#generate:mcp turbo task that build:${APP}
+# depends on; without it `pnpm install` fails with ERR_MODULE_NOT_FOUND.
+COPY scripts ./scripts
RUN pnpm install --frozen-lockfile
FROM deps AS build
diff --git a/apps/erp/vite.config.ts b/apps/erp/vite.config.ts
index 8caf89e75e..07601f5a50 100644
--- a/apps/erp/vite.config.ts
+++ b/apps/erp/vite.config.ts
@@ -6,9 +6,60 @@ import path from "node:path";
import { defineConfig, PluginOption } from "vite";
import babelMacros from "vite-plugin-babel-macros";
-export default defineConfig(({ isSsrBuild, mode }) => {
+export default defineConfig(({ command, isSsrBuild, mode }) => {
applyDotenvToProcessEnv(mode, __dirname);
+ /**
+ * SSR dependencies that must be bundled into the server output rather than
+ * left as bare `import`s. Defined once and applied to BOTH `ssr.noExternal`
+ * (honored by the dev server and the plain `react-router build`) and
+ * `environments.ssr.resolve.noExternal`.
+ *
+ * The second location is not redundant: with `future.v8_viteEnvironmentApi`
+ * enabled, the Vercel preset builds each route group as a named server-bundle
+ * environment (`ssr_bundle_*`), and React Router's server-bundle environment
+ * resolver merges `viteUserConfig.environments.ssr` — NOT the top-level `ssr`
+ * config. Without this, every entry below is externalized in the Vercel
+ * Lambda, and `zustand` (default-imported by @react-three/fiber, see below)
+ * crashes the function on cold start with "does not provide an export named
+ * 'default'".
+ */
+ const ssrNoExternal = [
+ "react-tweet",
+ "react-dropzone",
+ "react-icons",
+ "react-phone-number-input",
+ "tailwind-merge",
+ /**
+ * [email protected] ships a broken manifest: it `require('react')` and
+ * `require('prop-types')` at runtime but declares neither as a
+ * dependency or peer. Externalized, the SSR server bundle keeps the bare
+ * `require('react')`, which resolves locally via pnpm's hoisted store
+ * but is NOT traced into the Vercel serverless function — so the Lambda
+ * crashes on cold start with "Cannot find module 'react'". Bundling it
+ * inline resolves react/prop-types against the app's copies at build
+ * time, the same reason the react-* packages above are inlined.
+ *
+ * Build-only: react-csv also declares `"jsnext:main": "src/index.js"`,
+ * so when inlined in the dev server Vite picks the untranspiled ESM
+ * source, whose `import { func } from "prop-types"` fails under Node's
+ * CJS interop ("Named export 'func' not found"). In dev the CJS entry is
+ * externalized and `require`d directly, which works via pnpm's store.
+ * (Gated on `command` — not `isSsrBuild` — because under the environment
+ * API the config callback runs once for the whole build, so `isSsrBuild`
+ * is unreliable, whereas `command` is always "build" vs "serve".)
+ */
+ ...(command === "build" ? ["react-csv"] : []),
+ /**
+ * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
+ * its nested zustand v3, while the app uses zustand v5 (no default
+ * export). Externalizing zustand merges both into one bare import that
+ * resolves to v5 at runtime and crashes the server at module load.
+ * Bundling it lets each importer keep its own version.
+ */
+ "zustand",
+ ];
+
return {
build: {
minify: true,
@@ -27,39 +78,14 @@ export default defineConfig(({ isSsrBuild, mode }) => {
global: "globalThis",
},
ssr: {
- noExternal: [
- "react-tweet",
- "react-dropzone",
- "react-icons",
- "react-phone-number-input",
- "tailwind-merge",
- /**
- * [email protected] ships a broken manifest: it `require('react')` and
- * `require('prop-types')` at runtime but declares neither as a
- * dependency or peer. Externalized, the SSR server bundle keeps the bare
- * `require('react')`, which resolves locally via pnpm's hoisted store
- * but is NOT traced into the Vercel serverless function — so the Lambda
- * crashes on cold start with "Cannot find module 'react'". Bundling it
- * inline resolves react/prop-types against the app's copies at build
- * time, the same reason the react-* packages above are inlined.
- *
- * Build-only: react-csv also declares `"jsnext:main": "src/index.js"`,
- * so when inlined in the dev server Vite picks the untranspiled ESM
- * source, whose `import { func } from "prop-types"` fails under Node's
- * CJS interop ("Named export 'func' not found"). In dev the CJS entry is
- * externalized and `require`d directly, which works via pnpm's store.
- */
-
- ...(isSsrBuild ? ["react-csv"] : []),
- /**
- * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
- * its nested zustand v3, while the app uses zustand v5 (no default
- * export). Externalizing zustand merges both into one bare import that
- * resolves to v5 at runtime and crashes the server at module load.
- * Bundling it lets each importer keep its own version.
- */
- "zustand",
- ],
+ noExternal: ssrNoExternal,
+ },
+ environments: {
+ ssr: {
+ resolve: {
+ noExternal: ssrNoExternal,
+ },
+ },
},
server: {
port: 3000,
diff --git a/apps/mes/vite.config.ts b/apps/mes/vite.config.ts
index a06f5b0617..496c357136 100644
--- a/apps/mes/vite.config.ts
+++ b/apps/mes/vite.config.ts
@@ -9,6 +9,35 @@ import babelMacros from "vite-plugin-babel-macros";
export default defineConfig(({ mode, isSsrBuild }) => {
applyDotenvToProcessEnv(mode, __dirname);
+ /**
+ * SSR dependencies that must be bundled into the server output rather than
+ * left as bare `import`s. Applied to BOTH `ssr.noExternal` (honored by the
+ * dev server and the plain `react-router build`) and
+ * `environments.ssr.resolve.noExternal`.
+ *
+ * The second location is not redundant: with `future.v8_viteEnvironmentApi`
+ * enabled, the Vercel preset builds each route group as a named server-bundle
+ * environment (`ssr_bundle_*`), and React Router's server-bundle environment
+ * resolver merges `viteUserConfig.environments.ssr` — NOT the top-level `ssr`
+ * config. Without this, `zustand` (default-imported by @react-three/fiber,
+ * see below) is externalized in the Vercel Lambda and crashes the function on
+ * cold start with "does not provide an export named 'default'".
+ */
+ const ssrNoExternal = [
+ "react-dropzone",
+ "react-icons",
+ "react-phone-number-input",
+ "tailwind-merge",
+ /**
+ * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
+ * its nested zustand v3, while the app uses zustand v5 (no default
+ * export). Externalizing zustand merges both into one bare import that
+ * resolves to v5 at runtime and crashes the server at module load.
+ * Bundling it lets each importer keep its own version.
+ */
+ "zustand",
+ ];
+
return {
build: {
minify: true,
@@ -27,20 +56,14 @@ export default defineConfig(({ mode, isSsrBuild }) => {
global: "globalThis",
},
ssr: {
- noExternal: [
- "react-dropzone",
- "react-icons",
- "react-phone-number-input",
- "tailwind-merge",
- /**
- * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
- * its nested zustand v3, while the app uses zustand v5 (no default
- * export). Externalizing zustand merges both into one bare import that
- * resolves to v5 at runtime and crashes the server at module load.
- * Bundling it lets each importer keep its own version.
- */
- "zustand",
- ],
+ noExternal: ssrNoExternal,
+ },
+ environments: {
+ ssr: {
+ resolve: {
+ noExternal: ssrNoExternal,
+ },
+ },
},
server: {
port: 3001,