Skip to content

Commit e5c5236

Browse files
authored
Fix default export workflow function transformation in workflow mode (#373)
Fixed a bug where default export workflow functions with names (e.g., `export default async function testWorkflow()`) would fail at runtime with `ReferenceError: $$default is not defined`. The SWC plugin now correctly keeps the default export as-is and simply adds the workflowId assignment after it, since the function name is available as a binding in the module scope. Also adds support for anonymous and arrow function default exports, which previously were not being transformed at all. Fixes #367.
1 parent 1e636e1 commit e5c5236

File tree

21 files changed

+657
-55
lines changed

21 files changed

+657
-55
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/swc-plugin": patch
3+
---
4+
5+
Fix default export workflow function transformation in workflow mode

packages/swc-plugin-workflow/transform/src/lib.rs

Lines changed: 521 additions & 51 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test anonymous default export workflow
2+
export default async function() {
3+
'use workflow';
4+
const result = await someStep();
5+
return result;
6+
}
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test anonymous default export workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
const __default = async function() {
4+
throw new Error("You attempted to execute workflow __default function directly. To start a workflow, use start(__default) from workflow/api");
5+
};
6+
__default.workflowId = "workflow//input.js//__default";
7+
export default __default;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test anonymous default export workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
export default async function() {
4+
'use workflow';
5+
const result = await someStep();
6+
return result;
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test anonymous default export workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
const __default = async function() {
4+
const result = await someStep();
5+
return result;
6+
};
7+
__default.workflowId = "workflow//input.js//__default";
8+
export default __default;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test default export arrow workflow
2+
export default async (data) => {
3+
'use workflow';
4+
const processed = await processData(data);
5+
return processed;
6+
};
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test default export arrow workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
const __default = async (data)=>{
4+
throw new Error("You attempted to execute workflow __default function directly. To start a workflow, use start(__default) from workflow/api");
5+
};
6+
__default.workflowId = "workflow//input.js//__default";
7+
export default __default;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test default export arrow workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
export default (async (data)=>{
4+
'use workflow';
5+
const processed = await processData(data);
6+
return processed;
7+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test default export arrow workflow
2+
/**__internal_workflows{"workflows":{"input.js":{"default":{"workflowId":"workflow//input.js//__default"}}}}*/;
3+
const __default = async (data)=>{
4+
const processed = await processData(data);
5+
return processed;
6+
};
7+
__default.workflowId = "workflow//input.js//__default";
8+
export default __default;

0 commit comments

Comments
 (0)