Skip to content

Commit

Permalink
place wrapped export default at end of file (Closes #129)
Browse files Browse the repository at this point in the history
this ensures that any static properties are correctly transferred onto the HOC
  • Loading branch information
Skn0tt committed Aug 10, 2022
1 parent 0652c62 commit bf1c4fa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addNamed as addNamedImport } from '@babel/helper-module-imports';
import {
arrayExpression,
callExpression,
exportDefaultSpecifier,
ClassDeclaration,
classExpression,
ExportNamedDeclaration,
Expand All @@ -20,9 +21,9 @@ import {
variableDeclarator,
importDeclaration,
importDefaultSpecifier,
importSpecifier,
identifier,
exportDefaultDeclaration,
Program,
} from '@babel/types';
import * as nodePath from 'path';

Expand Down Expand Up @@ -129,8 +130,11 @@ function wrapExportDefaultDeclaration(path: NodePath<any>) {
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<Program>).pushContainer(
'body',
exportDefaultDeclaration(wrapInHOC(node.declaration.id)) as any
);
} else {
if (isFunctionDeclaration(node.declaration)) {
node.declaration = wrapInHOC(
Expand Down
17 changes: 17 additions & 0 deletions test/pages/static property/code.js
Original file line number Diff line number Diff line change
@@ -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';
};
20 changes: 20 additions & 0 deletions test/pages/static property/output.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit bf1c4fa

Please sign in to comment.