diff --git a/.changeset/polite-animals-write.md b/.changeset/polite-animals-write.md new file mode 100644 index 0000000000..f35c79ae44 --- /dev/null +++ b/.changeset/polite-animals-write.md @@ -0,0 +1,5 @@ +--- +"workflow": patch +--- + +Expose `dirs` option in `workflows` config object in `withWorkflow()` diff --git a/docs/content/docs/api-reference/workflow-next/with-workflow.mdx b/docs/content/docs/api-reference/workflow-next/with-workflow.mdx index 5c64284577..384bb7e291 100644 --- a/docs/content/docs/api-reference/workflow-next/with-workflow.mdx +++ b/docs/content/docs/api-reference/workflow-next/with-workflow.mdx @@ -48,4 +48,42 @@ export default async function config( } return nextConfig; } -``` +``` + +## Configuration + +The second argument to `withWorkflow` accepts the following options: + +### `workflows.dirs` + +Directories to scan for workflows and steps. If provided, this completely overrides the defaults. + +- **Type:** `string[]` +- **Default:** `['pages', 'app', 'src/pages', 'src/app']` + +```typescript title="next.config.ts" lineNumbers +import { withWorkflow } from "workflow/next"; +import type { NextConfig } from "next"; + +const nextConfig: NextConfig = {}; + +export default withWorkflow(nextConfig, { + workflows: { + dirs: ['workflows', 'src/workflows'], // [!code highlight] + }, +}); +``` + +### `workflows.local.port` + +Port for the local workflow server during development. + +- **Type:** `number` +- **Default:** Uses the `PORT` environment variable + +### `workflows.local.dataDir` + +Directory for storing local workflow data during development. + +- **Type:** `string` +- **Default:** `'.next/workflow-data'` diff --git a/docs/content/docs/getting-started/next.mdx b/docs/content/docs/getting-started/next.mdx index 4fc9213043..bf7052c639 100644 --- a/docs/content/docs/getting-started/next.mdx +++ b/docs/content/docs/getting-started/next.mdx @@ -257,6 +257,45 @@ Check the [Deploying](/docs/deploying) section to learn how your workflows can b ## Troubleshooting +### Out of Memory (OOM) during build + +When using `withWorkflow()` in a large Next.js application, the "Discovering workflow directives" phase may consume excessive memory, causing builds to fail with OOM errors on standard build machines (e.g., 16 GB RAM). + +**Solution:** Use the `workflows.dirs` option to limit the directories scanned for workflow directives. By default, `withWorkflow` scans `['pages', 'app', 'src/pages', 'src/app']`, which in large applications can include thousands of files. + +If your workflows are located in a specific directory, configure `dirs` to only scan that directory: + +```typescript title="next.config.ts" lineNumbers +import { withWorkflow } from "workflow/next"; +import type { NextConfig } from "next"; + +const nextConfig: NextConfig = {}; + +export default withWorkflow(nextConfig, { + workflows: { + // Only scan the workflows directory instead of the entire app + dirs: ['workflows'], // [!code highlight] + }, +}); +``` + +If you need workflows in both app routes and a dedicated directory: + +```typescript title="next.config.ts" lineNumbers +import { withWorkflow } from "workflow/next"; +import type { NextConfig } from "next"; + +const nextConfig: NextConfig = {}; + +export default withWorkflow(nextConfig, { + workflows: { + dirs: ['app/api', 'workflows'], // [!code highlight] + }, +}); +``` + +This significantly reduces memory usage and build times by avoiding scanning unrelated application code. + ### Next.js 16.1+ compatibility If you see this error when upgrading to Next.js 16.1 or later: diff --git a/packages/next/src/index.ts b/packages/next/src/index.ts index 0fc8e4d846..b70951436f 100644 --- a/packages/next/src/index.ts +++ b/packages/next/src/index.ts @@ -2,6 +2,12 @@ import type { NextConfig } from 'next'; import semver from 'semver'; import { getNextBuilder } from './builder.js'; +/** + * Default directories to scan for workflows and steps. + * These are the standard Next.js app directories. + */ +const DEFAULT_WORKFLOW_DIRS = ['pages', 'app', 'src/pages', 'src/app']; + export function withWorkflow( nextConfigOrFn: | NextConfig @@ -17,6 +23,13 @@ export function withWorkflow( port?: number; dataDir?: string; }; + /** + * Directories to scan for workflows and steps. + * If provided, this completely overrides the defaults. + * + * @default ['pages', 'app', 'src/pages', 'src/app'] + */ + dirs?: string[]; }; } = {} ) { @@ -121,8 +134,7 @@ export function withWorkflow( const NextBuilder = await getNextBuilder(); const workflowBuilder = new NextBuilder({ watch: shouldWatch, - // discover workflows from pages/app entries - dirs: ['pages', 'app', 'src/pages', 'src/app'], + dirs: workflows?.dirs ?? DEFAULT_WORKFLOW_DIRS, workingDir: process.cwd(), buildTarget: 'next', workflowsBundlePath: '', // not used in base