@@ -14,15 +14,17 @@ export type TransformWrapExportFilter = (
1414 meta : ExportMeta ,
1515) => boolean
1616
17+ export type TransformWrapExportOptions = {
18+ runtime : ( value : string , name : string , meta : ExportMeta ) => string
19+ ignoreExportAllDeclaration ?: boolean
20+ rejectNonAsyncFunction ?: boolean
21+ filter ?: TransformWrapExportFilter
22+ }
23+
1724export function transformWrapExport (
1825 input : string ,
1926 ast : Program ,
20- options : {
21- runtime : ( value : string , name : string , meta : ExportMeta ) => string
22- ignoreExportAllDeclaration ?: boolean
23- rejectNonAsyncFunction ?: boolean
24- filter ?: TransformWrapExportFilter
25- } ,
27+ options : TransformWrapExportOptions ,
2628) : {
2729 exportNames : string [ ]
2830 output : MagicString
@@ -81,8 +83,15 @@ export function transformWrapExport(
8183 )
8284 }
8385
84- function validateNonAsyncFunction ( node : Node , ok ?: boolean ) {
85- if ( options . rejectNonAsyncFunction && ! ok ) {
86+ function validateNonAsyncFunction ( node : Node ) {
87+ if ( ! options . rejectNonAsyncFunction ) return
88+ if (
89+ node . type === 'ClassDeclaration' ||
90+ ( ( node . type === 'FunctionDeclaration' ||
91+ node . type === 'FunctionExpression' ||
92+ node . type === 'ArrowFunctionExpression' ) &&
93+ ! node . async )
94+ ) {
8695 throw Object . assign ( new Error ( `unsupported non async function` ) , {
8796 pos : node . start ,
8897 } )
@@ -100,11 +109,7 @@ export function transformWrapExport(
100109 /**
101110 * export function foo() {}
102111 */
103- validateNonAsyncFunction (
104- node ,
105- node . declaration . type === 'FunctionDeclaration' &&
106- node . declaration . async ,
107- )
112+ validateNonAsyncFunction ( node . declaration )
108113 const name = node . declaration . id . name
109114 wrapSimple ( node . start , node . declaration . start , [
110115 { name, meta : { isFunction : true , declName : name } } ,
@@ -113,14 +118,11 @@ export function transformWrapExport(
113118 /**
114119 * export const foo = 1, bar = 2
115120 */
116- validateNonAsyncFunction (
117- node ,
118- node . declaration . declarations . every (
119- ( decl ) =>
120- decl . init ?. type === 'ArrowFunctionExpression' &&
121- decl . init . async ,
122- ) ,
123- )
121+ for ( const decl of node . declaration . declarations ) {
122+ if ( decl . init ) {
123+ validateNonAsyncFunction ( decl . init )
124+ }
125+ }
124126 if ( node . declaration . kind === 'const' ) {
125127 output . update (
126128 node . declaration . start ,
@@ -201,14 +203,7 @@ export function transformWrapExport(
201203 * export default () => {}
202204 */
203205 if ( node . type === 'ExportDefaultDeclaration' ) {
204- validateNonAsyncFunction (
205- node ,
206- // TODO: somehow identifier is allowed in next.js?
207- // (see packages/react-server/examples/next/app/actions/server/actions.ts)
208- node . declaration . type === 'Identifier' ||
209- ( node . declaration . type === 'FunctionDeclaration' &&
210- node . declaration . async ) ,
211- )
206+ validateNonAsyncFunction ( node . declaration as Node )
212207 let localName : string
213208 let isFunction = false
214209 let declName : string | undefined
0 commit comments