-
Notifications
You must be signed in to change notification settings - Fork 87
feat: add bun support #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../LICENSE.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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:*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
|
|
||
| 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, | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/1_simple.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/2_control_flow.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/3_streams.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/4_ai.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/5_hooks.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/6_batching.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/98_duplicate_case.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../example/workflows/99_e2e.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { workflowPlugin } from '@workflow/bun'; | ||
|
|
||
| export default workflowPlugin; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code uses
require.resolve()without importing or initializingrequire, which will cause a runtime error in ES modules.View Details
Analysis
Missing
requireinitialization in Bun plugin causes runtime error in ES moduleWhat fails:
packages/bun/src/index.tscallsrequire.resolve('@workflow/swc-plugin')on line 26 without initializingrequire, which causesReferenceError: require is not definedat runtime when the Bun plugin attempts to transform files with workflow directives.How to reproduce:
@workflow/bunpackage (which has"type": "module"in package.json)workflowPlugin()function to register the Bun pluginbuild.onLoad()handler will execute and throw:ReferenceError: require is not definedExpected behavior: The plugin should successfully resolve the SWC plugin path and transform the file. Per Node.js ES Module documentation,
requiremust be explicitly created in ES modules usingcreateRequirefrom the 'module' package.Root cause: The file uses ES module syntax (
importstatements) and declares"type": "module"in package.json, but attempts to use the globalrequirefunction which is only available in CommonJS modules. This contrasts with@workflow/nextwhich uses"type": "commonjs"and can userequiredirectly.Solution: Import
createRequirefrom 'module' and initialize therequirefunction at the module level, following the pattern established inpackages/cli/src/lib/builders/apply-swc-transform.ts.