Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/bun/LICENSE.md
3 changes: 3 additions & 0 deletions packages/bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# workflow/bun

The docs have moved! Refer to them [here](https://useworkflow.dev/)
40 changes: 40 additions & 0 deletions packages/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@workflow/bun",
"version": "4.0.1-beta.1",
"description": "Bun integration for Workflow DevKit",
"type": "module",
"main": "dist/index.js",
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/vercel/workflow.git",
"directory": "packages/bun"
},
"exports": {
".": "./dist/index.js"
},
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"clean": "tsc --build --clean && rm -rf dist"
},
"dependencies": {
"@swc/core": "1.11.24",
"@workflow/cli": "workspace:*",
"@workflow/core": "workspace:*",
"@workflow/swc-plugin": "workspace:*",
"exsolve": "^1.0.7",
"pathe": "^2.0.3"
},
"devDependencies": {
"@types/bun": "^1.3.1",
"@types/node": "catalog:",
"@workflow/tsconfig": "workspace:*"
}
}
79 changes: 79 additions & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { transform } from '@swc/core';
import { BaseBuilder } from '@workflow/cli/dist/lib/builders/base-builder';
import type { WorkflowConfig } from '@workflow/cli/dist/lib/config/types';
import type { BunPlugin } from 'bun';

Comment on lines +3 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { transform } from '@swc/core';
import { BaseBuilder } from '@workflow/cli/dist/lib/builders/base-builder';
import type { WorkflowConfig } from '@workflow/cli/dist/lib/config/types';
import type { BunPlugin } from 'bun';
import { createRequire } from 'module';
import { transform } from '@swc/core';
import { BaseBuilder } from '@workflow/cli/dist/lib/builders/base-builder';
import type { WorkflowConfig } from '@workflow/cli/dist/lib/config/types';
import type { BunPlugin } from 'bun';
const require = createRequire(import.meta.filename);

The code uses require.resolve() without importing or initializing require, which will cause a runtime error in ES modules.

View Details

Analysis

Missing require initialization in Bun plugin causes runtime error in ES module

What fails: packages/bun/src/index.ts calls require.resolve('@workflow/swc-plugin') on line 26 without initializing require, which causes ReferenceError: require is not defined at runtime when the Bun plugin attempts to transform files with workflow directives.

How to reproduce:

  1. Import the @workflow/bun package (which has "type": "module" in package.json)
  2. Call the workflowPlugin() function to register the Bun plugin
  3. Process any TypeScript/JavaScript file containing "use workflow" or "use step" directives
  4. The plugin's build.onLoad() handler will execute and throw: ReferenceError: require is not defined

Expected behavior: The plugin should successfully resolve the SWC plugin path and transform the file. Per Node.js ES Module documentation, require must be explicitly created in ES modules using createRequire from the 'module' package.

Root cause: The file uses ES module syntax (import statements) and declares "type": "module" in package.json, but attempts to use the global require function which is only available in CommonJS modules. This contrasts with @workflow/next which uses "type": "commonjs" and can use require directly.

Solution: Import createRequire from 'module' and initialize the require function at the module level, following the pattern established in packages/cli/src/lib/builders/apply-swc-transform.ts.

export function workflowPlugin(): BunPlugin {
return {
name: 'workflow-transform',
async setup(build) {
// Build workflows on startup
await new LocalBuilder().build();

build.onLoad({ filter: /\.(ts|tsx|js|jsx)$/ }, async (args) => {
const source = await Bun.file(args.path).text();
// Optimization: Skip files that do not have any directives
if (!source.match(/(use step|use workflow)/)) {
return { contents: source };
}
const result = await transform(source, {
filename: args.path,
jsc: {
experimental: {
plugins: [
[require.resolve('@workflow/swc-plugin'), { mode: 'client' }],
],
},
},
});
return { contents: result.code, loader: 'ts' };
});
},
};
}

const CommonBuildOptions = {
buildTarget: 'next' as const, // unused in base
stepsBundlePath: '', // unused in base
workflowsBundlePath: '', // unused in base
webhookBundlePath: '', // unused in base
};

export class LocalBuilder extends BaseBuilder {
#outDir: string;
constructor(config?: Partial<WorkflowConfig>) {
const outDir = join(config?.workingDir ?? process.cwd(), 'workflow');
super({
...CommonBuildOptions,
dirs: config?.dirs ?? ['./workflows'],
workingDir: config?.workingDir ?? process.cwd(),
});
this.#outDir = outDir;
}

override async build(): Promise<void> {
const inputFiles = await this.getInputFiles();
await mkdir(this.#outDir, { recursive: true });

await this.createWorkflowsBundle({
outfile: join(this.#outDir, 'workflows.mjs'),
bundleFinalOutput: false,
format: 'esm',
inputFiles,
});

await this.createStepsBundle({
outfile: join(this.#outDir, 'steps.mjs'),
externalizeNonSteps: true,
format: 'esm',
inputFiles,
});

await this.createWebhookBundle({
outfile: join(this.#outDir, 'webhook.mjs'),
bundle: false,
});
}
}
12 changes: 12 additions & 0 deletions packages/bun/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@workflow/tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"module": "preserve",
"baseUrl": ".",
"moduleResolution": "bundler"
},
"include": ["src"],
"exclude": ["node_modules", "**/*.test.ts"]
}
15 changes: 15 additions & 0 deletions packages/bun/workflows/0_demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// import { FatalError } from 'workflow';

export async function calc(n: number) {
'use workflow';
console.log('Simple workflow started');
n = await pow(n);
console.log('Simple workflow finished');
return n;
}

async function pow(a: number): Promise<number> {
'use step';
console.log('Running step pow with arg:', a);
return a * a;
}
1 change: 1 addition & 0 deletions packages/bun/workflows/1_simple.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/2_control_flow.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/3_streams.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/4_ai.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/5_hooks.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/6_batching.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/98_duplicate_case.ts
1 change: 1 addition & 0 deletions packages/bun/workflows/99_e2e.ts
10 changes: 7 additions & 3 deletions packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"default": "./dist/index.js"
},
"./api": "./dist/api.js",
"./bun": "./dist/bun.js",
"./internal/errors": "./dist/internal/errors.js",
"./internal/serialization": "./dist/internal/serialization.js",
"./internal/builtins": "./dist/internal/builtins.js",
Expand All @@ -45,13 +46,16 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@types/bun": "^1.3.1",
"@workflow/bun": "workspace:*",
"@workflow/cli": "workspace:*",
"@workflow/core": "workspace:*",
"@workflow/errors": "workspace:*",
"@workflow/typescript-plugin": "workspace:*",
"ms": "2.1.3",
"@workflow/next": "workspace:*",
"@workflow/nitro": "workspace:*"
"@workflow/nitro": "workspace:*",
"@workflow/typescript-plugin": "workspace:*",
"bun": "^1.3.0",
"ms": "2.1.3"
},
"devDependencies": {
"@types/ms": "^2.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/workflow/src/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { workflowPlugin } from '@workflow/bun';

export default workflowPlugin;
Loading
Loading