From bf1c4fa1add02a63696a561ddb2b82774ff543dd Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 10 Aug 2022 11:49:42 +0200 Subject: [PATCH] place wrapped export default at end of file (Closes #129) this ensures that any static properties are correctly transferred onto the HOC --- src/index.ts | 10 +++++++--- test/pages/static property/code.js | 17 +++++++++++++++++ test/pages/static property/output.js | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/pages/static property/code.js create mode 100644 test/pages/static property/output.js diff --git a/src/index.ts b/src/index.ts index 960cad9..5d2d484 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { addNamed as addNamedImport } from '@babel/helper-module-imports'; import { arrayExpression, callExpression, + exportDefaultSpecifier, ClassDeclaration, classExpression, ExportNamedDeclaration, @@ -20,9 +21,9 @@ import { variableDeclarator, importDeclaration, importDefaultSpecifier, - importSpecifier, identifier, exportDefaultDeclaration, + Program, } from '@babel/types'; import * as nodePath from 'path'; @@ -129,8 +130,11 @@ function wrapExportDefaultDeclaration(path: NodePath) { isClassDeclaration(node.declaration) ) { if (node.declaration.id) { - path.insertBefore(node.declaration); - node.declaration = wrapInHOC(node.declaration.id); + path.replaceInline(node.declaration); + (path.parentPath as NodePath).pushContainer( + 'body', + exportDefaultDeclaration(wrapInHOC(node.declaration.id)) as any + ); } else { if (isFunctionDeclaration(node.declaration)) { node.declaration = wrapInHOC( diff --git a/test/pages/static property/code.js b/test/pages/static property/code.js new file mode 100644 index 0000000..d5a2438 --- /dev/null +++ b/test/pages/static property/code.js @@ -0,0 +1,17 @@ +import Text from '../components/Text'; + +export const getServerSideProps = () => { + return { + props: { + text: 'hi', + }, + }; +}; + +export default function Page() { + return 'foo'; +} + +Page.getLayout = function getLayout(page) { + return 'bar'; +}; diff --git a/test/pages/static property/output.js b/test/pages/static property/output.js new file mode 100644 index 0000000..a9124ff --- /dev/null +++ b/test/pages/static property/output.js @@ -0,0 +1,20 @@ +import { withSuperJSONPage as _withSuperJSONPage } from 'babel-plugin-superjson-next/tools'; +import { withSuperJSONProps as _withSuperJSONProps } from 'babel-plugin-superjson-next/tools'; +import Text from '../components/Text'; +export const getServerSideProps = _withSuperJSONProps(() => { + return { + props: { + text: 'hi', + }, + }; +}, ['smth']); + +function Page() { + return 'foo'; +} + +Page.getLayout = function getLayout(page) { + return 'bar'; +}; + +export default _withSuperJSONPage(Page);