diff --git a/src/parsing/preserve-named-constant-export.ts b/src/parsing/preserve-named-constant-export.ts index 4c55fe3d..548ed031 100644 --- a/src/parsing/preserve-named-constant-export.ts +++ b/src/parsing/preserve-named-constant-export.ts @@ -14,12 +14,7 @@ * limitations under the License. */ -import { - ExpressionStatement, - AssignmentExpression, - FunctionExpression, - MemberExpression, -} from 'estree'; +import { ExpressionStatement, AssignmentExpression, FunctionExpression, MemberExpression } from 'estree'; import { ExportDetails, Range } from '../types'; import MagicString from 'magic-string'; @@ -37,21 +32,21 @@ function PreserveFunction( const functionExpression = assignmentExpression.right as FunctionExpression; const [memberExpressionObjectStart] = memberExpression.object.range as Range; const functionName = exportInline ? exportDetails.exported : exportDetails.local; - + const functionAsync = functionExpression.async; if (functionExpression.params.length > 0) { const [paramsStart] = functionExpression.params[0].range as Range; // FunctionExpression has parameters. source.overwrite( memberExpressionObjectStart, paramsStart, - `${exportInline ? 'export ' : ''}function ${functionName}(`, + `${exportInline ? 'export ' : ''}${functionAsync ? 'async ' : ''}function ${functionName}(`, ); } else { const [bodyStart] = functionExpression.body.range as Range; source.overwrite( memberExpressionObjectStart, bodyStart, - `${exportInline ? 'export ' : ''}function ${functionName}()`, + `${exportInline ? 'export ' : ''}${functionAsync ? 'async ' : ''}function ${functionName}()`, ); } @@ -74,9 +69,8 @@ function PreserveIdentifier( if (exportInline) { const output = - (exportDetails.exported === 'default' - ? `export default ` - : `export var ${exportDetails.exported}=`) + `${code.substring(rightStart, rightEnd)};`; + (exportDetails.exported === 'default' ? `export default ` : `export var ${exportDetails.exported}=`) + + `${code.substring(rightStart, rightEnd)};`; source.overwrite(ancestorStart, ancestorEnd, output); } else if (exportDetails.source === null && 'name' in right) { // This is a locally defined identifier with a name we can use. @@ -84,11 +78,7 @@ function PreserveIdentifier( source.remove(leftStart, ancestorEnd); return true; } else { - source.overwrite( - ancestorStart, - ancestorEnd, - `var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`, - ); + source.overwrite(ancestorStart, ancestorEnd, `var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`); } return !exportInline; diff --git a/test/export-default/async-function.test.js b/test/export-default/async-function.test.js new file mode 100644 index 00000000..db5401b0 --- /dev/null +++ b/test/export-default/async-function.test.js @@ -0,0 +1,3 @@ +import { generator } from '../generator'; + +generator('export-default', 'async-function', false, ['esm'], { stable: {} }); diff --git a/test/export-default/fixtures/async-function.esm.stable.js b/test/export-default/fixtures/async-function.esm.stable.js new file mode 100644 index 00000000..fd9c18f3 --- /dev/null +++ b/test/export-default/fixtures/async-function.esm.stable.js @@ -0,0 +1 @@ +async function asyncTest(){await Promise.resolve("async-test");console.log("async-test")};async function asyncTestWithArgument(a){await Promise.resolve("async-test-with-argument");console.log(a)};export{asyncTest,asyncTestWithArgument} diff --git a/test/export-default/fixtures/async-function.js b/test/export-default/fixtures/async-function.js new file mode 100644 index 00000000..0d5082bd --- /dev/null +++ b/test/export-default/fixtures/async-function.js @@ -0,0 +1,9 @@ +export async function asyncTest() { + await Promise.resolve('async-test'); + console.log('async-test') +} + +export async function asyncTestWithArgument(argument) { + await Promise.resolve('async-test-with-argument'); + console.log(argument) +} \ No newline at end of file